2024年6月7日发(作者:)
用JavaSocket开发高并发小型服务器
一、表示Socket服务端的类:
Java代码
public class Server extends Thread{
public static int port = 6789;
public static String host = "10.60.1.127";
private static ServerSocket server = null;
public void run() {
if(server == null){
try{
//1、新建ServerSocket实例
server = new ServerSocket(port);
}catch(IOException e){
tackTrace();
}
}
n("服务器启动...");
while(true){
try{
//2、访问ServerSocket实例的accept方法取得一个客户端
Socket对象
Socket client = ();
if(client == null) continue;
new SocketConnection(client, "D:").start();
}catch(IOException ex){
tackTrace();
}
}
}
public static void main(String[] args) {
new Server().start();
}
}
Socket客户端处理类:
Java代码
public class SocketConnection extends Thread{
private Socket client;


发布评论