gpt4 book ai didi

java - 如何让 ServerSocket 接受来自同一 IP 的多个连接?

转载 作者:行者123 更新时间:2023-12-02 06:41:52 26 4
gpt4 key购买 nike

我有一个服务器套接字,它接受连接并将它们传递给工作线程。当连接来自不同的 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

最佳答案

允许 ServerSocket 接受来自同一 IP 的多个请求并没有什么神奇之处。事实上,它会自然发生......而且看起来你的代码会做到这一点。

但是,您的代码(如图所示)正在创建线程,但没有启动它们。除非您启动一个线程,否则它的 run() 方法将不会运行。

(我假设 WorkerThreadThread 的子类。)

换句话说,您已经接受请求连接,但它是否正在读取和处理请求值得怀疑。

<小时/>

无论如何,通过扩展 Thread 类来编写工作逻辑是一个糟糕的想法。最好实现 Runnable 接口(interface),并将其作为构造函数参数传递给普通的 Thread 对象。或者更好的方法仍然是使用带有有限线程池的 ThreadPoolExecutor 之类的东西来管理线程的创建和请求的排队。

关于java - 如何让 ServerSocket 接受来自同一 IP 的多个连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com