先说一下大概的思路:
应用多线程来实现服务器与多客户端之间的通信
1、服务器端创建ServerSocket,循环调用accept()等待客户端连接;
2、客户端创建一个Socket并请求与服务器端连接;
3、服务器端接受客户端请求,创建Socket与该客户端简历专线连接;
4、建立连接的两个Socket在一个单独的线程上对话;
5、服务器端继续等待其他新的连接。
闲话不多说,直接上代码:
package net;/** * 1、客户端 */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class Client { public static void main(String[] args) { try { //1、创建Socket,与服务器建立连接 Socket socket = new Socket("localhost", 8889); //2、向服务器端发送消息 OutputStream os = socket.getOutputStream(); PrintWriter pwriter = new PrintWriter(os); //将Socket的输出字节流包装成打印流 pwriter.write("用户名:sysker ; 密码:sys154"); //刷新流 pwriter.flush(); //3、关闭资源 socket.shutdownOutput(); //3、获取输入流 InputStream inputStream = socket.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String getMsage=null; while((getMsage=bufferedReader.readLine())!=null){ System.out.println("客户端正在等待响应,服务器说:"+getMsage); } //socket.shutdownInput(); bufferedReader.close(); inputStream.close(); inputStreamReader.close(); pwriter.close(); os.close(); socket.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
package net;/** * 2、服务器端 */import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ManyClientServer { public static void main(String[] args) { Socket socket = null; int clientCount = 0; try { ServerSocket serverSocket = new ServerSocket(8889); while(true){ socket = serverSocket.accept(); ManyClientThread manyClientThread = new ManyClientThread(socket); manyClientThread.start(); clientCount++; System.out.println("客户端数量"+clientCount); System.out.println("客户端的IP地址:"+socket.getInetAddress().getHostAddress()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
package net;/** * 3、服务器通信线程 */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;public class ManyClientThread extends Thread{ Socket socket = null; BufferedReader bufferedReader = null; public ManyClientThread(Socket socket){ this.socket = socket; } public void run(){ try { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(socket.getInputStream())); String mString = null; while((mString = bufferedReader.readLine())!=null){ System.out.println("我是服务器,客户端说"+mString); } bufferedReader.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(bufferedReader!=null){ try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}