2013-02-07 40 views
0

我有一个开放的社会容器,我需要在该容器中放置一个小程序,该小程序需要充当tcpServer,我需要一些指导,我该如何做到这一点?TCP服务器小程序

+0

基本上我需要一些指导做好OON如何设计一个java TCP服务器小程序 –

回答

0

networking tutorial详细解释了如何创建TCP服务器套接字并接受来自它的连接。

下面是它的要点:

ServerSocket serverSocket = null; 
    try { 
     serverSocket = new ServerSocket(4444); 
    } catch (IOException e) { 
     System.err.println("Could not listen on port: 4444."); 
     System.exit(1); 
    } 

    Socket clientSocket = null; 
    try { 
     clientSocket = serverSocket.accept(); 
    } catch (IOException e) { 
     System.err.println("Accept failed."); 
     System.exit(1); 
    } 

    // now get the input and output streams from the client socket 
    // and use them to read and write to the TCP connection 

    clientSocket.close(); 
    serverSocket.close();