Java套接字:同一台机器上同一端口上的多个客户端线程?
我是 Java 套接字编程的新手,并试图了解下面的代码是否是错误的做法。我的问题是:
我可以在每个线程上有多个客户端尝试连接到同一程序中的服务器实例,并期望服务器在客户端之间隔离的情况下读取和写入数据吗?
public class Client extends Thread
{
...
void run()
{
Socket socket = new Socket("localhost", 1234);
doIO(socket);
}
}
public class Server extends Thread
{
...
void run()
{
// serverSocket on "localhost", 1234
Socket clientSock = serverSocket.accept();
executor.execute(new ClientWorker(clientSock));
}
}
现在我可以在不同线程上有多个客户端实例尝试连接在当前机器的同一端口上
?
Server s = new Server("localhost", 1234);
s.start();
Client[] c = new Client[10];
for (int i = 0; i < c.length; ++i)
{
c.start();
}
I am new to Socket programming in Java and was trying to understand if the below code is not a wrong thing to do. My question is:
Can I have multiple clients on each thread trying to connect to a server instance in the same program and expect the server to read and write data with isolation between clients"
public class Client extends Thread
{
...
void run()
{
Socket socket = new Socket("localhost", 1234);
doIO(socket);
}
}
public class Server extends Thread
{
...
void run()
{
// serverSocket on "localhost", 1234
Socket clientSock = serverSocket.accept();
executor.execute(new ClientWorker(clientSock));
}
}
Now can I have multiple Client instances on different threads trying to connect on the same port of the current machine?
For example,
Server s = new Server("localhost", 1234);
s.start();
Client[] c = new Client[10];
for (int i = 0; i < c.length; ++i)
{
c.start();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,但是按照所写,每个线程执行只有一个客户端能够连接。
您可以将服务器 run() 放入 while true 循环中,以让多个客户端连接。
根据执行者的不同,它们将串行或并行执行。
Yes, however only one client will be able to connect per thread execution as written.
You can just put your server run() inside a while true loop to let multiple clients connect.
Depending on the executor, they will execute either in series or parallel.
只要只有一个对象尝试绑定端口进行侦听,那么多个客户端连接就没有问题。
As long as you only have one object trying to bind the port for listening, then there's no problem with multiple clients connecting.
在此示例中,您的
服务器
一次接受并处理一个客户端连接。您可以尝试连接任意多个客户端
,但一次只会处理一个。由于您没有提供实现,因此您的执行器逻辑是否是多线程的并不明显。如果执行器委托给线程池或类似的东西,您需要确保您的 ClientWorker 是线程安全的,因为您将有多个实例并行执行。
我当然假设您的客户端也是线程安全的,因为您的问题仅涉及服务器。
In this example, your
Server
accepts and handles one client connection at a time. You can have as manyClient
s as you want attempting to connect, but only one at a time will be handled.It is not apparent whether your executor logic is multithreaded, since you didn't provide the implementation. If the executor delegates to a threadpool or something like that, you would need to make sure that your
ClientWorker
is thread-safe, as you will have multiple instances executing in parallel.I am of course assuming that your
Client
is thread-safe as well, since your question is only concerning theServer
.是的,您的客户端是本地还是远程并不重要。在您的示例中,重要的是 ClientWorker 是线程安全的,因为您的服务器将具有该类的多个实例(每个客户端连接一个)。
Yes, it doesn't matter whether your clients are local or remote. The important thing in your example is that ClientWorker is thread-safe, as your server will have multiple instances of that class (one for each client connection).
所以。首先:
您可以使用一个服务器套接字接受更多客户端,因为您在
run
方法中只接受一个客户端。您只需再次调用accept()
即可。然后,在 for 循环中:首先,每次都必须创建一个新的
Client
对象。然后您可以调用c[i].start();
而不是c.start()
。是的,你可以。只需创建新线程并运行它们即可。这应该可以完美地工作。
您可以利用基本 IO 技术的经验,例如文件 io:
并使用 BufferedReader 进行读取。
So. To begin:
You can accept more clients with one serversocket, because you accept only one in the
run
-method. You have just to callaccept()
a second time.Then, you in your for loop: first you have to create each time a new
Client
object. Then you can callc[i].start();
and notc.start()
.Yes you can. Just create new Threads and run them. This should work perfectly.
You can use your experience of the basic IO techniques like with file-io:
And for reading use a BufferedReader.
你可以尝试一下这些行
You can try something on these lines