java socket编程多线程处理多客户端
2017-04-08 13:45
615 查看
一、服务器端启动程序
二、服务器端接收客户端发送信息的线程
三、客户端启动程序
四、客户端接收服务器发送数据的线程
package com.chhuang.main; import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import com.chhuang.thread.AcceptRunner; public class App { public static final int serverPort = 8808; public static ServerSocket serverSocket = null; public static void main(String[] args) { try { serverSocket = new ServerSocket(serverPort); System.out.println("\n服务器已启动"); System.out.println("服务器IP:"+InetAddress.getLocalHost().getHostAddress()); System.out.println("端口号:"+serverPort); System.out.println("等待客户端登录......\n"); //接收客户端连接 while(true){ Socket clientSocket = serverSocket.accept(); String clientIp = clientSocket.getInetAddress().getHostAddress();//读取客户端ip int clientPort = clientSocket.getPort();//读取客户端端口号 System.out.println("\n客户端"+clientIp+":"+clientPort+"上线\n"); //服务器为客户端打开线程接收客户端数据 new Thread(new AcceptRunner(clientSocket)).start(); //测试 test(clientSocket); } } catch (IOException e) { e.printStackTrace(); } } /** * 测试 * @param clientSocket * @throws IOException */ private static void test(Socket clientSocket) throws IOException{ //测试 for(int i=0; i<10; i++){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } if(!clientSocket.isClosed()){ PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true); out.println("hi"+i); }else{ break; } } } }
二、服务器端接收客户端发送信息的线程
package com.chhuang.thread; import java.io.IOException; import java.net.Socket; import java.util.Scanner; public class AcceptRunner implements Runnable { private Socket clientSocket; public AcceptRunner(Socket clientSocket){ super(); this.clientSocket = clientSocket; } @Override public void run() { if(clientSocket==null){ return; } Scanner in = null; try { in = new Scanner(clientSocket.getInputStream()); while(in.hasNextLine()){ String content = in.nextLine(); System.out.println(content); } } catch (IOException e) { e.printStackTrace(); } finally { if(in!=null) in.close(); String clientIp = clientSocket.getInetAddress().getHostAddress();//读取客户端ip int clientPort = clientSocket.getPort();//读取客户端端口号 System.out.println("\n客户端"+clientIp+":"+clientPort+"已离线\n"); } } }
三、客户端启动程序
package com.chhuang.main; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import com.chhuang.thread.AcceptRunner; public class App { public static final String serverIp = "192.168.1.100"; public static final int serverPort = 8808; public static void main(String[] args) { try { Socket clientSocket = new Socket(serverIp, serverPort); String ip = clientSocket.getInetAddress().getHostAddress(); int port = clientSocket.getLocalPort(); System.out.println("客户端"+ip+":"+port+"启动成功"); //接收服务器发来的信息 new Thread(new AcceptRunner(clientSocket)).start(); } catch (UnknownHostException e) { System.out.println("服务器已停止运行"); e.printStackTrace(); } catch (IOException e) { System.out.println("服务器已停止运行"); e.printStackTrace(); } } }
四、客户端接收服务器发送数据的线程
package com.chhuang.thread; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class AcceptRunner implements Runnable { private Socket clientSocket; public AcceptRunner(Socket clientSocket){ super(); this.clientSocket = clientSocket; } @Override public void run() { if(clientSocket==null){ return; } Scanner in = null; try { in=new Scanner(clientSocket.getInputStream()); while(in.hasNextLine()){ String content = in.nextLine(); System.out.println(content); //接收到hi,回复hello if(content!=null && content.startsWith("hi")){ //向服务器发送 PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true); out.println("hello "+content); } } } catch (IOException e) { e.printStackTrace(); } finally { if(in!=null) in.close(); } } }
相关文章推荐
- Java Socket 多线程编程,处理百万级的数据并发。
- java使用多线程编程,实现socket服务器客户端交互
- 网络编程_TCP_Socket通信_聊天室_客户端多线程_群聊JAVA191-192
- [零散篇]Java学习笔记---Java的Socket网络编程以及多线程
- Java Socket多线程服务端、客户端
- 【Java的Socket网络编程以及多线程】
- Java Socket Server-client端多线程处理代码示例
- java网络编程实例1——用socket类写http客户端
- C++多线程编程以及epoll处理socket通信时多端口问题
- java网络编程--socket服务端客户端(1:1)
- Socket 编程经验谈---如何处理socket连接后服务器端或客户端的断开
- c语言socket多线程编程限制客户端连接数
- 18天Java学习---Java的Socket网络编程以及多线程
- Java网络编程实例---------------------------------基本HTTP客户端及服务端多线程
- Java网络编程从入门到精通(16):客户端套接字(Socket)的超时
- Java Socket多线程服务端、客户端
- socket ( java ) 简单多个客户端、服务端通信(多线程)
- Java Socket多线程编程、通信模型及socket协议详解
- Lunix网络编程之socket(客户端发送请求,服务器处理例如:排序,两人联机五子棋)
- java socket编程,客户端发出检索字段,服务端查询数据并返回结果集