网络编程_参考答案.pdf
1、编写一服务器端程序,实现读取客户端发送过来的一组整数,表现为一组数与数之间用空格隔开的字符串。对这组整数进行排序处理后,返回相应的字符串给客户端,如果数据格式不正确,则返回错误信息,以本机作为服务器。public class Server public static void main(String args)BufferedReader br=null;String clientStr=;try .);.);while(true)Socket s=();发送请求);(s);One o=new One();Thread t=new Thread(o);();catch(IOException e)etInputStream();str=();客户端+()+说:+str);catch(IOException e)/TODO Auto-generated catch block +用户下线);public class SendAll implements Runnable Scanner in=new Scanner;BufferedReader br=new BufferedReader(new InputStreamReader);public void run()PrintWriter out=null;try while(true)String str=();for(Socket s:out=new PrintWriter(new OutputStreamWriter(),true);(str);catch(Exception e)/TODO Auto-generated catch block ();finally ();public class Client public static void main(String args)BufferedReader br=null;String clientStr=;try /建立通信端口,并向服务器端发送通信请求 Socket s=new Socket(,4001);与服务器端建立连接);Send send=new Send(s);Thread t1=new Thread(send);();while(true)/获取客户端通信的信息 br=new BufferedReader(new InputStreamReader();clientStr=();服务器端说:+clientStr);catch(UnknownHostException e)/TODO Auto-generated catch block ();catch(IOException e)/TODO Auto-generated catch block ();public class Send implements Runnable Socket s;Scanner in=new Scanner;BufferedReader br=new BufferedReader(new InputStreamReader);public Send(Socket s)=s;public void run()/TODO Auto-generated method stub PrintWriter out=null;try while(true)out=new PrintWriter(new OutputStreamWriter(),true);String str=();(str);catch(Exception e)/TODO Auto-generated catch block ();finally ();