服务器单独显示客户信息
在上星期实验4.3的基础上,修改服务端程序,使得服务器在接受到客户连接后弹出如下所示对话框,并将所有客户端发回的信息显示在这个对话框里。
代码:
EchoClient.java
package cn.edu.sise;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.nio.charset.Charset;public class EchoClient { public static void main(String[] args) { Socket clientSocket = null; BufferedReader in = null; BufferedWriter out = null; try { //1.创建客户机套接字 clientSocket = new Socket(); SocketAddress remoteAddr=new InetSocketAddress("172.16.28.19",5000); System.out.println("1.创建客户机套接字成功!"); //2.连接服务器 clientSocket.connect(remoteAddr); System.out.println("2.客户机连接服务器localhost端口5000成功!"); System.out.println("客户机使用的地址和端口:"+clientSocket.getLocalSocketAddress()); //与服务器会话 in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); out = new BufferedWriter( new OutputStreamWriter( clientSocket.getOutputStream())); BufferedReader localIn = new BufferedReader( new InputStreamReader( System.in)); String sendStr=localIn.readLine(); out.write(sendStr); //向服务器发送字符串 out.newLine(); out.flush(); System.out.println("3.1向服务器发送字符串成功!"+sendStr); String recvStr=in.readLine(); //从服务器接收字符串 System.out.println("3.2从服务器接收回送字符串成功!"+recvStr); } catch (IOException ex) { System.out.println("异常信息:"+ex.getMessage()); } //关闭套接字和流 try { if (in != null) in.close(); if (out != null) out.close(); if (clientSocket != null) clientSocket.close(); System.out.println("4.关闭套接字和流成功!"); } catch (IOException ex) { System.out.println("异常信息:"+ex.getMessage()); } }}
EchoServer.java
package cn.edu.sise;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketAddress;import java.nio.charset.Charset;public class EchoServer { public static void main(String[] args) { //1.启动服务器 ServerSocket listenSocket = null; Socket clientSocket = null; BufferedReader in = null; BufferedWriter out = null; try { listenSocket = new ServerSocket(); SocketAddress serverAddr=new InetSocketAddress(InetAddress.getByName("172.16.28.19"),5000); listenSocket.bind(serverAddr); System.out.println("1.服务器启动成功!开始在localhost的5000端口侦听连接..."); //2.处理连接 clientSocket = listenSocket.accept(); System.out.println("2.客户机连接成功!客户机地址和端口:"+clientSocket.getRemoteSocketAddress()); //3.与客户机会话 in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); out = new BufferedWriter( new OutputStreamWriter( clientSocket.getOutputStream())); String recvStr=in.readLine(); //从客户机接收字符串 System.out.println("3.1 服务器收到字符串:"+recvStr); out.write(recvStr); //向客户机回送字符串 out.newLine(); out.flush(); System.out.println("3.2 服务器回送字符串成功:"+recvStr); } catch (IOException ex) { System.out.println("异常信息:"+ex.getMessage()); } //4.关闭套接字和流 try { if (in != null)in.close(); if (out != null) out.close(); if (listenSocket != null) listenSocket.close(); if (clientSocket != null) clientSocket.close(); System.out.println("4.关闭套接字和流成功!"); } catch (IOException ex) { System.out.println("异常信息"+ex.getMessage()); } }}
TestBuffered.java
package cn.edu.sise;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Date;public class TestBuffered { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //InputStream fis = new FileInputStream("file");//������52�룩 InputStream fis =new BufferedInputStream( new FileInputStream("file"));//������1�룩 System.out.println(new Date() + ": ���Կ�ʼ..."); while (fis.read()!=-1) { //��ȡ�����ļ������� } System.out.println(new Date() + ": ���Խ���"); fis.close(); }}
TestInputStream.java
package cn.edu.sise;import java.io.BufferedInputStream;import java.io.IOException;public class TestInputStream { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedInputStream bis = new BufferedInputStream(System.in); int data = bis.read(); while (data!=-1){ System.out.println(data + " " + (char)data); if(data==13) break; data = bis.read(); } bis.close(); }}
TestReader.java
package cn.edu.sise;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class TestReader { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader bis = new BufferedReader(new InputStreamReader(System.in)); int data = bis.read(); while (data!=-1){ System.out.println(data + " " + (char)data); if(data==13) break; data = bis.read(); } bis.close(); }}