我有一个接受连接并将它们传递给工作线程的服务器套接字。当连接来自不同的 IP 地址时,这同时工作。在该客户端关闭之前的连接之前,它不会接受来自任何给定机器(IP 地址)的新连接。
我怎样才能使这项工作?
提前致谢。
编辑:来自同一 ip 客户端的请求将阻塞,直到前一个完成。
private ConcurrentLinkedQueue<WorkerThread> queueList = new ConcurrentLinkedQueue<WorkerThread>();
...
public void ServerSocketLoop()
{
try
{
ServerSocket sk = new ServerSocket(iPort);
while (!sk.isClosed())
{
Logger.Log(this, Logger.CONNECTION_ONLY, "Loop: ");
int cnt = getQueueList().size();
Socket clientSocket = sk.accept();
String clIp = clientSocket.getInetAddress().getHostAddress();
boolean match=false;
if(!allowedIps.isEmpty())
{
for(String alIp : allowedIps.keySet())
{
if(clIp.startsWith(alIp))
{
match=true;
break;
}
}
}
else
match=true;
if(match)
{
try
{
Logger.Log(this, Logger.CONNECTION_ONLY, "Accepting connection from: " + clIp);
WorkerThread scs = new WorkerThread(clientSocket, this);
getQueueList().add(scs);
} catch (Exception e)
{
Logger.Log(this, e);
}
} else
{
Logger.Log(this, Logger.CONNECTION_ONLY, "Refused connection from: " + clIp);
}
}
}
catch(Exception e)
{
Logger.Log(this, e);
}
}
编辑:更多信息。
工作线程一旦启动读取(客户端)套接字输入流,使用进程构建器执行命令,通过套接字读取的数据通过套接字的输出流写回结果,然后关闭套接字。
日志输出:注意20:06:10.305从192.168.1.32连接
[2013-09-28 20:04:51.181] Accepting connection from: /0:0:0:0:0:0:0:1%0:49460
[2013-09-28 20:04:51.497] Accepted socket
[2013-09-28 20:04:51.498] In: one
[2013-09-28 20:04:51.499] Looking for wrkr4237846575505157705.out
[2013-09-28 20:04:51.499] Start process
[2013-09-28 20:05:07.564] Accepting connection from: /192.168.1.32:32864
[2013-09-28 20:05:07.880] Accepted socket
[2013-09-28 20:05:07.881] In: one
[2013-09-28 20:05:07.881] Looking for wrkr2936790469462792093.out
[2013-09-28 20:05:07.881] Start process
[2013-09-28 20:05:24.600] Finished
[2013-09-28 20:05:24.601] Out: wrkr4237846575505157705 Sat Sep 28 20:04:51 CDT 2013
[2013-09-28 20:05:24.602] Out: wrkr4237846575505157705 Sat Sep 28 20:04:54 CDT 2013
[2013-09-28 20:05:24.603] Out: wrkr4237846575505157705 Sat Sep 28 20:04:57 CDT 2013
[2013-09-28 20:05:24.603] Out: wrkr4237846575505157705 Sat Sep 28 20:05:00 CDT 2013
[2013-09-28 20:05:24.604] Out: wrkr4237846575505157705 Sat Sep 28 20:05:03 CDT 2013
[2013-09-28 20:05:24.604] Out: wrkr4237846575505157705 Sat Sep 28 20:05:06 CDT 2013
[2013-09-28 20:05:24.605] Out: wrkr4237846575505157705 Sat Sep 28 20:05:09 CDT 2013
[2013-09-28 20:05:24.605] Out: wrkr4237846575505157705 Sat Sep 28 20:05:12 CDT 2013
[2013-09-28 20:05:24.605] Out: wrkr4237846575505157705 Sat Sep 28 20:05:15 CDT 2013
[2013-09-28 20:05:24.606] Out: wrkr4237846575505157705 Sat Sep 28 20:05:18 CDT 2013
[2013-09-28 20:05:24.607] Out: wrkr4237846575505157705 Sat Sep 28 20:05:21 CDT 2013
[2013-09-28 20:05:24.628] Accepting connection from: /0:0:0:0:0:0:0:1%0:49464
[2013-09-28 20:05:24.993] Accepted socket
[2013-09-28 20:05:24.995] In: two
[2013-09-28 20:05:24.996] Looking for wrkr1946064549539591675.out
[2013-09-28 20:05:24.996] Start process
[2013-09-28 20:05:40.984] Finished
[2013-09-28 20:05:40.985] Out: wrkr2936790469462792093 Sat Sep 28 20:05:07 CDT 2013
[2013-09-28 20:05:40.985] Out: wrkr2936790469462792093 Sat Sep 28 20:05:10 CDT 2013
[2013-09-28 20:05:40.986] Out: wrkr2936790469462792093 Sat Sep 28 20:05:13 CDT 2013
[2013-09-28 20:05:40.986] Out: wrkr2936790469462792093 Sat Sep 28 20:05:16 CDT 2013
[2013-09-28 20:05:40.987] Out: wrkr2936790469462792093 Sat Sep 28 20:05:19 CDT 2013
[2013-09-28 20:05:40.987] Out: wrkr2936790469462792093 Sat Sep 28 20:05:22 CDT 2013
[2013-09-28 20:05:40.988] Out: wrkr2936790469462792093 Sat Sep 28 20:05:25 CDT 2013
[2013-09-28 20:05:40.988] Out: wrkr2936790469462792093 Sat Sep 28 20:05:28 CDT 2013
[2013-09-28 20:05:40.988] Out: wrkr2936790469462792093 Sat Sep 28 20:05:31 CDT 2013
[2013-09-28 20:05:40.989] Out: wrkr2936790469462792093 Sat Sep 28 20:05:34 CDT 2013
[2013-09-28 20:05:40.989] Out: wrkr2936790469462792093 Sat Sep 28 20:05:37 CDT 2013
[2013-09-28 20:05:58.109] Finished
[2013-09-28 20:05:58.110] Out: wrkr1946064549539591675 Sat Sep 28 20:05:25 CDT 2013
[2013-09-28 20:05:58.110] Out: wrkr1946064549539591675 Sat Sep 28 20:05:28 CDT 2013
[2013-09-28 20:05:58.110] Out: wrkr1946064549539591675 Sat Sep 28 20:05:31 CDT 2013
[2013-09-28 20:05:58.111] Out: wrkr1946064549539591675 Sat Sep 28 20:05:34 CDT 2013
[2013-09-28 20:05:58.111] Out: wrkr1946064549539591675 Sat Sep 28 20:05:37 CDT 2013
[2013-09-28 20:05:58.111] Out: wrkr1946064549539591675 Sat Sep 28 20:05:40 CDT 2013
[2013-09-28 20:05:58.112] Out: wrkr1946064549539591675 Sat Sep 28 20:05:43 CDT 2013
[2013-09-28 20:05:58.112] Out: wrkr1946064549539591675 Sat Sep 28 20:05:46 CDT 2013
[2013-09-28 20:05:58.112] Out: wrkr1946064549539591675 Sat Sep 28 20:05:49 CDT 2013
[2013-09-28 20:05:58.112] Out: wrkr1946064549539591675 Sat Sep 28 20:05:52 CDT 2013
[2013-09-28 20:05:58.113] Out: wrkr1946064549539591675 Sat Sep 28 20:05:55 CDT 2013
[2013-09-28 20:05:58.160] Accepting connection from: /0:0:0:0:0:0:0:1%0:49470
[2013-09-28 20:05:58.477] Accepted socket
[2013-09-28 20:05:58.479] In: three
[2013-09-28 20:05:58.480] Looking for wrkr7376110979239359337.out
[2013-09-28 20:05:58.480] Start process
[2013-09-28 20:06:10.305] Accepting connection from: /192.168.1.32:32870
[2013-09-28 20:06:10.469] Accepted socket
[2013-09-28 20:06:10.470] In: laurie
[2013-09-28 20:06:10.470] Looking for wrkr1695309805515169887.out
[2013-09-28 20:06:10.470] Start process
[2013-09-28 20:06:31.591] Finished
[2013-09-28 20:06:31.592] Out: wrkr7376110979239359337 Sat Sep 28 20:05:58 CDT 2013
[2013-09-28 20:06:31.592] Out: wrkr7376110979239359337 Sat Sep 28 20:06:01 CDT 2013
[2013-09-28 20:06:31.593] Out: wrkr7376110979239359337 Sat Sep 28 20:06:04 CDT 2013
[2013-09-28 20:06:31.593] Out: wrkr7376110979239359337 Sat Sep 28 20:06:07 CDT 2013
[2013-09-28 20:06:31.593] Out: wrkr7376110979239359337 Sat Sep 28 20:06:10 CDT 2013
[2013-09-28 20:06:31.594] Out: wrkr7376110979239359337 Sat Sep 28 20:06:13 CDT 2013
[2013-09-28 20:06:31.594] Out: wrkr7376110979239359337 Sat Sep 28 20:06:16 CDT 2013
[2013-09-28 20:06:31.594] Out: wrkr7376110979239359337 Sat Sep 28 20:06:19 CDT 2013
[2013-09-28 20:06:31.595] Out: wrkr7376110979239359337 Sat Sep 28 20:06:22 CDT 2013
[2013-09-28 20:06:31.595] Out: wrkr7376110979239359337 Sat Sep 28 20:06:25 CDT 2013
[2013-09-28 20:06:31.595] Out: wrkr7376110979239359337 Sat Sep 28 20:06:28 CDT 2013
[2013-09-28 20:06:31.642] Accepting connection from: /0:0:0:0:0:0:0:1%0:49472
[2013-09-28 20:06:31.958] Accepted socket
[2013-09-28 20:06:31.959] In: four
[2013-09-28 20:06:31.960] Looking for wrkr1689149287441159516.out
[2013-09-28 20:06:31.960] Start process
[2013-09-28 20:06:43.581] Finished
[2013-09-28 20:06:43.582] Out: wrkr1695309805515169887 Sat Sep 28 20:06:10 CDT 2013
[2013-09-28 20:06:43.582] Out: wrkr1695309805515169887 Sat Sep 28 20:06:13 CDT 2013
[2013-09-28 20:06:43.583] Out: wrkr1695309805515169887 Sat Sep 28 20:06:16 CDT 2013
[2013-09-28 20:06:43.583] Out: wrkr1695309805515169887 Sat Sep 28 20:06:19 CDT 2013
[2013-09-28 20:06:43.583] Out: wrkr1695309805515169887 Sat Sep 28 20:06:22 CDT 2013
[2013-09-28 20:06:43.584] Out: wrkr1695309805515169887 Sat Sep 28 20:06:25 CDT 2013
[2013-09-28 20:06:43.584] Out: wrkr1695309805515169887 Sat Sep 28 20:06:28 CDT 2013
[2013-09-28 20:06:43.584] Out: wrkr1695309805515169887 Sat Sep 28 20:06:31 CDT 2013
[2013-09-28 20:06:43.584] Out: wrkr1695309805515169887 Sat Sep 28 20:06:34 CDT 2013
[2013-09-28 20:06:43.585] Out: wrkr1695309805515169887 Sat Sep 28 20:06:37 CDT 2013
[2013-09-28 20:06:43.585] Out: wrkr1695309805515169887 Sat Sep 28 20:06:40 CDT 2013
[2013-09-28 20:07:05.061] Finished
[2013-09-28 20:07:05.062] Out: wrkr1689149287441159516 Sat Sep 28 20:06:31 CDT 2013
[2013-09-28 20:07:05.062] Out: wrkr1689149287441159516 Sat Sep 28 20:06:34 CDT 2013
[2013-09-28 20:07:05.062] Out: wrkr1689149287441159516 Sat Sep 28 20:06:37 CDT 2013
[2013-09-28 20:07:05.063] Out: wrkr1689149287441159516 Sat Sep 28 20:06:40 CDT 2013
[2013-09-28 20:07:05.063] Out: wrkr1689149287441159516 Sat Sep 28 20:06:44 CDT 2013
[2013-09-28 20:07:05.063] Out: wrkr1689149287441159516 Sat Sep 28 20:06:47 CDT 2013
[2013-09-28 20:07:05.064] Out: wrkr1689149287441159516 Sat Sep 28 20:06:50 CDT 2013
[2013-09-28 20:07:05.064] Out: wrkr1689149287441159516 Sat Sep 28 20:06:53 CDT 2013
[2013-09-28 20:07:05.064] Out: wrkr1689149287441159516 Sat Sep 28 20:06:56 CDT 2013
[2013-09-28 20:07:05.065] Out: wrkr1689149287441159516 Sat Sep 28 20:06:59 CDT 2013
[2013-09-28 20:07:05.065] Out: wrkr1689149287441159516 Sat Sep 28 20:07:02 CDT 2013
[2013-09-28 20:07:05.093] Accepting connection from: /0:0:0:0:0:0:0:1%0:49524
[2013-09-28 20:07:05.409] Accepted socket
[2013-09-28 20:07:05.411] In: five
[2013-09-28 20:07:05.411] Looking for wrkr8081329232671108201.out
[2013-09-28 20:07:05.411] Start process
[2013-09-28 20:07:38.515] Finished
[2013-09-28 20:07:38.516] Out: wrkr8081329232671108201 Sat Sep 28 20:07:05 CDT 2013
[2013-09-28 20:07:38.516] Out: wrkr8081329232671108201 Sat Sep 28 20:07:08 CDT 2013
[2013-09-28 20:07:38.517] Out: wrkr8081329232671108201 Sat Sep 28 20:07:11 CDT 2013
[2013-09-28 20:07:38.517] Out: wrkr8081329232671108201 Sat Sep 28 20:07:14 CDT 2013
[2013-09-28 20:07:38.517] Out: wrkr8081329232671108201 Sat Sep 28 20:07:17 CDT 2013
[2013-09-28 20:07:38.518] Out: wrkr8081329232671108201 Sat Sep 28 20:07:20 CDT 2013
[2013-09-28 20:07:38.518] Out: wrkr8081329232671108201 Sat Sep 28 20:07:23 CDT 2013
[2013-09-28 20:07:38.518] Out: wrkr8081329232671108201 Sat Sep 28 20:07:26 CDT 2013
[2013-09-28 20:07:38.519] Out: wrkr8081329232671108201 Sat Sep 28 20:07:29 CDT 2013
[2013-09-28 20:07:38.519] Out: wrkr8081329232671108201 Sat Sep 28 20:07:32 CDT 2013
[2013-09-28 20:07:38.519] Out: wrkr8081329232671108201 Sat Sep 28 20:07:35 CDT 2013