2017-10-17 94 views
-1

我使用多端口和多线程服务器时,我发送文件从客户端到服务器像波纹管代码或从服务器到客户端。但是我不能在同一时间为不同的端口发送数据。我认为我必须使用paralel编程,你有任何解决方案的建议,或者你可以给我这个主题的来源或互联网网站的建议。多线程服务器多端口发送文件同时

服务器端

SendFiletoClient(DataSocket1, "test1.txt"); 
SendFiletoClient(DataSocket2, "test2.txt"); 
SendFiletoClient(DataSocket3, "test3.txt"); 
SendFiletoClient(DataSocket4, "test4.txt"); 

客户端

addFileToClient(Socket1, "test1.txt"); 
addFileToClient(Socket2, "test2.txt"); 
addFileToClient(Socket3, "test3.txt"); 
addFileToClient(Socket4, "test4.txt"); 
+0

“我知道我必须使用serilazable端口”,是吧?什么是“serilazable端口”? – Kayaman

回答

1

为多线程服务器的模式是一些伪代码为:

//server side 
while (true) { 
    accept a connection; 
    create a thread to deal with the client; 
} 

或者更建设性的方式:

while(this.isRunning) { 
    Socket clientSocket = null; 
    try { 
     clientSocket = this.serverSocket.accept(); 
    } catch (IOException e) { 
     //handle exception on accept client socket 
    } 
    if(clientSocket != null) { 
     Thread workerThread = new Thread(
      new YourWorkerRunnable(clientSocket /*, remain arguments */)); 
     workerThread.start(); 
    } 
} 

此模式的变体包括workerThreads池以改善服务器端资源分配管理。

请参考to this link了解更多关于编码服务器套接字的信息。