2011-12-28 26 views
3

我想了解如何使用Java在Java中创建一个TCP服务器。但我没有得到我真正需要的东西。我是否必须将密钥文件导入到java中,而我呢,怎么做呢?或者我只需要将Socket的类型从Socket更改为SSLSocket?我读过一些文章,但找不到有用的东西,因为他们都只是采取http进行沟通。我需要它为我自己的协议。在我的情况下,这将是一个这样的程序:如何用java做一个SSL tcp服务器?

int port = 4444; 
    ServerSocket serverSocket = new ServerSocket(port); 
    System.err.println("Started server on port " + port); 

    // repeatedly wait for connections, and process 
    while (true) { 

     // a "blocking" call which waits until a connection is requested 
     Socket clientSocket = serverSocket.accept(); 
     System.err.println("Accepted connection from client"); 

     // open up IO streams 
     In in = new In (clientSocket); 
     Out out = new Out(clientSocket); 

     // waits for data and reads it in until connection dies 
     // readLine() blocks until the server receives a new line from client 
     String s; 
     while ((s = in.readLine()) != null) { 
      out.println(s); 
     } 

     // close IO streams, then socket 
     System.err.println("Closing connection with client"); 
     out.close(); 
     in.close(); 
     clientSocket.close(); 
    } 

使用SSL连接。那么如何做到这一点?

感谢, 托马斯

回答

2

我发现这一个快速谷歌搜索。

Here