السلام عليكــم ورحمـة الله وبركاتــه
ممكن أن تتفضلوا بشرح فكرة البرنامجين التاليين و كيفية تشغيلهما حيث أنني جربت كثيرا و لم أفلح فهل أكتب username أولا ثم أضغظ okأم أكتب جميع الحقول ثم أضغطok أم كيف؟؟؟
و أرجو إن كان هناك خظأ في برنامج ال client أن ترشدوني لتصحيحه
أرجوكم أن تستجيبوا لطلبي و تفرجوا همي
جزاكم الله كل خير ووفقكم
import java.net.*;
import java.io.*;
public class FTPServer{
Socket sDataSocket;
DataOutputStream outtodatasocket;
ServerSocket connect;
Socket sControlSocket;
DataOutputStream outtocontrolsocket;
BufferedReader infromcontrolsocket;
String temp="";
String tempo="";
String username="";
String pass="";
String ack;
String suser,spass;
int portnum=0;
public static void main(String[] args) throws Exception
{
if(args.length<2){System.out.println("USAGE: FTPserver [username] [password]");
System.exit(0);
};
new FTPServer(args[0],args[1]);
}//end of main
public FTPServer(String a,String B) throws Exception{
suser=a;
spass=b;
connect= new ServerSocket (21);
BufferedReader infromuser = new BufferedReader(new InputStreamReader(System.in));
String filename=null;
String received=null;
while(true){
//initialization
//control socket :............................................................................
System.out.println("Waiting behind The port 21\n");
sControlSocket= connect.accept();
outtocontrolsocket = new DataOutputStream(sControlSocket.getOutputStream());
infromcontrolsocket = new BufferedReader(new InputStreamReader(sControlSocket.getInputStream()));
// receiving user name
received=infromcontrolsocket.readLine();
received=infromcontrolsocket.readLine();
System.out.println("I receive request from : "+sControlSocket.getInetAddress().getHostName()+"\n");
if(received.startsWith("USER "))
{
int index1=received.indexOf(" ");
username=received.substring(index1+1);
if(username.equals(suser))
{ ack="231 user name ok\n";
outtocontrolsocket.writeBytes(ack);
}
else
{
ack="user name is wrong\n";
outtocontrolsocket.writeBytes(ack);
System.exit(0);
}
}
else
{
ack="error in sending user name\n";
outtocontrolsocket.writeBytes(ack);
System.exit(0);
}
// receiving password
received=infromcontrolsocket.readLine();
if(received.startsWith("PASS "))
{
int index1=received.indexOf(" ");
pass=received.substring(index1+1);
if(!pass.equals(spass))
{
ack="password is wrong\n";
outtocontrolsocket.writeBytes(ack);
System.exit(0);
}
else
{
outtocontrolsocket.writeBytes("230 password ok\n");
}
}
else
{
ack="error in sending password\n";
outtocontrolsocket.writeBytes(ack);
System.exit(0);
}
// receiving port number
received=infromcontrolsocket.readLine();
if(received.startsWith("PORT "))
{
int index1=received.indexOf(" ");
String portnumber=received.substring(index1+1);
portnum=Integer.parseInt(portnumber);
}
outtocontrolsocket.writeBytes("200 command ok\n");
// receiving file name
received=infromcontrolsocket.readLine();
int index1=received.indexOf(" ");
filename=received.substring(index1+1);
readfile(filename);
// initializing datasocket
//replace remote host ip with localhost
System.out.print("*********************************"+"\n"
+"sending the file : "+filename+" has done"+"\n"+"*********************************"+"\n");
sControlSocket.close();
sDataSocket.close();
}//end while loop
}//end of FTPServer
public void readfile(String filename) throws Exception
{
sDataSocket = new Socket("localhost",portnum);
outtodatasocket = new DataOutputStream(sDataSocket.getOutputStream());
BufferedReader infromdatasocket = new BufferedReader(new InputStreamReader(sDataSocket.getInputStream()));
try{
BufferedReader fr = new BufferedReader(new FileReader(filename));}
catch(Exception ex){ack="cann't find the file\n";
outtocontrolsocket.writeBytes(ack);
System.out.println("error occurred\n");
System.exit(0);
}
outtocontrolsocket.writeBytes("150 all is ok\n");
try{
BufferedReader fr = new BufferedReader(new FileReader(filename));
boolean done=false;
while (!done) {
temp=fr.readLine();
if (temp!=null){
outtodatasocket.write(temp.getBytes());
outtodatasocket.writeBytes("\n");
}
else
{ done=true;}
}
fr.close();
temp="";
}
catch(Exception ex){System.out.println("error occured\n");
System.exit(0);}
};
}//end of classبرنامج الكلاينت
import javax.swing.JButton;
import javax.swing.JFrame;
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
/*
* FTPFrameClient.java
*/
public class FTPFrameClient extends javax.swing.JFrame {
/** Creates new form FTPFrameClient */
public String username,password,filename,datareceived,filereceived,file,transfared,ip,add;
public FileWriter fw;
public Socket controlsocket,datasocket;
boolean done;
int portnum,port;
// Define Constructor Function
public FTPFrameClient() {
initComponents();
}
// Start Of Main
public static void main(String args[]) throws Exception
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new FTPFrameClient().setVisible(true);
}
catch (Exception ex) {
ex.printStackTrace();
}}});
}
// End Of Main
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jTextField3 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
jTextArea4 = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
jTextArea5 = new javax.swing.JTextArea();
jTextArea6 = new javax.swing.JTextArea();
jTextArea3 = new javax.swing.JTextArea();
jTextArea2 = new javax.swing.JTextArea();
jTextArea1 = new javax.swing.JTextArea();
jTextArea7 = new javax.swing.JTextArea();
jLabel6 = new javax.swing.JLabel();
jTextField4 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("User Name");
jLabel2.setText("Password");
jLabel3.setText("Port Number");
jLabel4.setText("File Name");
jButton1.setText("OK");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jScrollPane4.setFont(new java.awt.Font("Lucida Sans", 1, 14));
jTextArea4.setColumns(20);
jTextArea4.setEditable(false);
jTextArea4.setLineWrap(true);
jTextArea4.setRows(5);
jScrollPane4.setViewportView(jTextArea4);
jLabel5.setFont(new java.awt.Font("Britannic Bold", 1, 12));
jLabel5.setForeground(new java.awt.Color(10, 40, 204));
jLabel5.setText("my Program");
jTextArea5.setBackground(new java.awt.Color(204, 204, 204));
jTextArea5.setColumns(20);
jTextArea5.setEditable(false);
jTextArea5.setFont(new java.awt.Font("Arial Narrow", 1, 14));
jTextArea5.setRows(5);
jTextArea6.setBackground(new java.awt.Color(204, 204, 204));
jTextArea6.setColumns(20);
jTextArea6.setEditable(false);
jTextArea6.setFont(new java.awt.Font("Arial Narrow", 1, 14));
jTextArea6.setRows(5);
jTextArea3.setBackground(new java.awt.Color(204, 204, 204));
jTextArea3.setColumns(20);
jTextArea3.setEditable(false);
jTextArea3.setFont(new java.awt.Font("Arial Narrow", 1, 14));
jTextArea3.setRows(5);
jTextArea2.setBackground(new java.awt.Color(204, 204, 204));
jTextArea2.setColumns(20);
jTextArea2.setEditable(false);
jTextArea2.setFont(new java.awt.Font("Arial Narrow", 1, 14));
jTextArea2.setRows(5);
jTextArea1.setBackground(new java.awt.Color(204, 204, 204));
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setFont(new java.awt.Font("Arial Narrow", 1, 14));
jTextArea1.setRows(5);
jTextArea7.setBackground(new java.awt.Color(204, 204, 204));
jTextArea7.setColumns(20);
jTextArea7.setEditable(false);
jTextArea7.setFont(new java.awt.Font("Arial Narrow", 1, 14));
jTextArea7.setRows(5);
jLabel6.setText("IP Address");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPasswordField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)))
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jTextField4)
.addGap(21, 21, 21)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(51, 51, 51)))
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextArea1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
.addComponent(jTextArea5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
.addComponent(jTextArea3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
.addComponent(jTextArea6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
.addComponent(jTextArea7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
.addComponent(jTextArea2, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)))
.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jTextArea2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(5, 5, 5)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(jTextArea3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addComponent(jTextArea5, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jTextArea7, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jTextArea6, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(44, 44, 44)
.addComponent(jLabel5))
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
username=jTextField1.getText();
password=jPasswordField1.getText();
filename=jTextField3.getText();
portnum=Integer.parseInt(jTextField2.getText());
ip=jTextField4.getText();
port= 0;
add="";
datareceived="";
filereceived="";
file="";
transfared="";
done=false;
//index=0;
try {
controlsocket = new Socket("localhost",21);
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
// Building Control Socket
BufferedReader infromuser = new BufferedReader(new InputStreamReader(System.in));
BufferedReader inputcontstream= null;
DataOutputStream outputcontstream = null;
try {
outputcontstream = new DataOutputStream(controlsocket.getOutputStream());
inputcontstream = new BufferedReader(new InputStreamReader(controlsocket.getInputStream()));
// Sending User Name
outputcontstream.writeBytes("USER "+username+"\n");
// Checking user Name
datareceived=inputcontstream.readLine();
if(datareceived.startsWith("331")){jTextArea1.setText(datareceived);}
else{jTextArea1.setText(datareceived);
JOptionPane.showMessageDialog(null,"WRONG User Name","error",JOptionPane.ERROR_MESSAGE);
System.exit(0);}
// Sending Password
outputcontstream.writeBytes("PASS "+password+"\n");
// Checking Password
datareceived=inputcontstream.readLine();
if(datareceived.startsWith("230")){jTextArea2.setText(datareceived);}
else{jTextArea2.setText(datareceived);
JOptionPane.showMessageDialog(null,"Password Is Wrong","error",JOptionPane.ERROR_MESSAGE);
System.exit(0);}
// Sending Port Number
add=ip.replace(".",",");
outputcontstream.writeBytes("PORT "+add+",7,"+portnum+"\n");
// Checking Port Number
datareceived=inputcontstream.readLine();
if(datareceived.startsWith("200")){jTextArea3.setText(datareceived);}
else{jTextArea3.setText(datareceived);
JOptionPane.showMessageDialog(null,"Error With The Port Num","error",JOptionPane.ERROR_MESSAGE);
System.exit(0);}
// Sending File Name
outputcontstream.writeBytes("RETR "+filename+"\n");
jTextArea5.setText("Sending File Name..");
// Checking File Name
datareceived=inputcontstream.readLine();
if(datareceived.startsWith("150")){jTextArea5.setText(datareceived);}
else{jTextArea5.setText(datareceived);
JOptionPane.showMessageDialog(null,"Error With The File Name","error",JOptionPane.ERROR_MESSAGE);
System.exit(0);}
} catch (IOException ex) {
ex.printStackTrace();
}
///////////////////////////////////////
// Building Data Socket
BufferedReader inputdatastream= null;
DataOutputStream outputdatastream = null;
ServerSocket connectingsocket = null;
try {
port=7*256+portnum;
jTextArea7.setText("Connecting To Server On Port "+port+"\n");
connectingsocket = new ServerSocket(port);
datasocket= connectingsocket.accept();
outputdatastream = new DataOutputStream(datasocket.getOutputStream());
inputdatastream = new BufferedReader(new InputStreamReader(datasocket.getInputStream()));
// Receiving File
while(!done){
if (filereceived !=null){
filereceived=inputdatastream.readLine();
file=file+filereceived;}
else done=true; }
//index=filename.indexOf(".");
//o=filename.substring(index);
jTextArea4.setText(file);
jTextArea6.setText("Transfaring File: "+filename+"\n\rhas been done!!\n");
fw = new FileWriter(filename);
fw.write(file);
fw.close();
//filereceived="";
//datareceived="";
//fw= null;
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
private javax.swing.JTextArea jTextArea4;
private javax.swing.JTextArea jTextArea5;
private javax.swing.JTextArea jTextArea6;
private javax.swing.JTextArea jTextArea7;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}