服务器线程
按照老师的做法,为什么我启动了服务端,和客户端,服务器端显示了客户端连接1,也会立即显示客户端连接2、
public class Server { public static void main(String[] args){ try { //1.创建服务器端socket,并指定端口号监听 ServerSocket serversocket = new ServerSocket(8888); //2.开始监听客户端请求 Socket socket = null; System.out.println("***服务器即将启动,等待客户端的连接***"); int count =0; while(true){ socket = serversocket.accept(); ServerThread st = new ServerThread(socket); st.start(); count++; System.out.println("客户端连接数:"+count);
//InetAddress address = socket.getInetAddress(); //System.out.println("来自客户端地址:"+address.getHostAddress()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }