📄 echoclient.java
字号:
import java.io.*;
import java.net.*;
import java.util.*;
import sun.net.ftp.*;
import sun.net.*;
class EchoClient
{
public static void main(String[] args) throws IOException
{
Socket echoSocket=null;//声明SOCKET类。
BufferedReader in=null;//输入流。
PrintWriter out=null;//输出流。
try{
echoSocket=new Socket("localhost",1111);//连接服务器。
in=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
out=new PrintWriter(echoSocket.getOutputStream(),true);
}
catch(UnknownHostException e){
System.err.println("do not know about host:localhost.");//若连接不上服务器,则报错。
System.exit(1);
}
ClientSocketProtocol protocol=new ClientSocketProtocol();//声明ClientServerProtocol类。
//System.out.println("****************");
out.println(protocol.send());//向服务器发出下载申请。
out.flush();
//System.out.println("+++++++++++++++++");
a: while(true){
String userget=null;
StringTokenizer get;
String head,type,infor,g;
//while(userget==null)
userget=in.readLine();
System.out.println(userget);
//分割从服务器来的信息。
get=new StringTokenizer(userget);
if(userget!=null){
if(userget.substring(0,3).equalsIgnoreCase("kdc")){
head=get.nextToken();
type=get.nextToken();
infor=get.nextToken();
g=protocol.action(head,type,infor);//对信息进行处理,并将返回值赋给g。
out.println(g);//向服务器端发送信息。
out.flush();
System.out.println("action has excuted.");
System.out.println("client: "+g);
if(g.equals("Ftp success!"))
break a;
}
else{
System.out.println("wrong! please check the configration.then retry");
break a;
}
}
try{
Thread.sleep(3000);
}catch(Exception e){}
}
out.close();
in.close();
echoSocket.close();
}
}
//声明ClientSocketProtocol类。
class ClientSocketProtocol{
//声明私有成员。
private String head_out;
private String type_out;
private String infor_out;
private static final String AskForDownload="1";
private static final String WaitForDownload="2";
private static final String AllowDownload="81";
private static final String DownloadFile="82";
private static final String USERNAME="th";
private String ftppath;
private String pwd;
//声明成员函数。
public ClientSocketProtocol(){}
public String makeMessage(String head,String type,String infor){
String temp;
temp="kdc";
temp+=" "+type;
temp+=" "+infor;
return temp;
}
public String action(String head,String type,String infor){//对信息进行处理。
if(type.equals(AllowDownload)){
System.out.println("Server: AllowDownload");
this.ftppath=infor;
this.head_out="kdc";
this.type_out="2";
this.infor_out="#";
return this.makeMessage(this.head_out,this.type_out,this.infor_out);
//out.println(this.makeMessage(this.head_out,this.type_out,this.infor_out));
//out.flush();
}
else if(type.equalsIgnoreCase(DownloadFile)){
System.out.println("Server: DownloadFile");
this.pwd=infor;
return this.Download(this.ftppath,USERNAME,this.pwd);
}
else{
return "invaild type!hzyth";
//out.println("invaild type!client");
//out.flush();
}
}
public String send(){//生成向服务器发送下载请求的信息。
return this.makeMessage("kdc","1",USERNAME);
//out.println(this.makeMessage("kdc","1",USERNAME));
//out.flush();
}
//
public String Download(String filename,String uid,String pwd){ //从服务器下载指定文件。
// try
//
// {
// //连接服务器FTP。
// FtpClient ftpClient=new FtpClient();
// ftpClient.openServer("202.197.124.112");
// ftpClient.login(uid, pwd);
// //if (path.length()!=0) ftpClient.cd(filename);
// ftpClient.binary();
// //从FTP服务器上下载文件,将其稵elnetInputStream流中。
// TelnetInputStream is=ftpClient.get(filename);
// File file_out=new File("d:\\"+filename);//打开本机器上的文件。
// FileOutputStream os=new
// FileOutputStream(file_out);//声明文件输出流。
// byte[] bytes=new byte[1024];
// int c;
// //将FTP上的文件写到本机的文件上。
// while ((c=is.read(bytes))!=-1)
//
// {
// os.write(bytes,0,c);
// }
// is.close();
// os.close();
// ftpClient.closeServer();
// }
// catch(FtpLoginException e){
// System.out.println("Hava no permission!");
// return "Ftp Failed! For have no permission.";
// }
// catch (IOException ex) {
// System.out.println("Connect failed");
// return "Ftp Failed.For connect failed.";}
// catch(SecurityException e){
// System.out.println("password incorrect");
// return "Ftp Failed!For password incorrect.";
// }
// System.out.println("Ftp success!");
return "Ftp success!";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -