首页 > 代码库 > socket编程3-EchoServer

socket编程3-EchoServer

package Chapter3;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {
    private ServerSocket server;
    private int port = 8000;
    private Socket socket;
    public EchoServer() {
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            System.out.println("未启动ServerSocket");
        }
    }
public void service() {
    while(true){
        try {
        System.out.println("EchoServer等待客户端连接");
        socket=server.accept();
        Thread workThread=new Thread(new Handler(socket));
        workThread.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}
    public static void main(String[] args) {
        new EchoServer().service();
    }

}

socket编程3-EchoServer