ServerSocket如何处理来自多个客户端的同时连接?

huangapple go评论68阅读模式
英文:

How ServerSocket deal with multiple connection from clients at the same time?

问题

好的,让我们澄清一下问题...

我正在学习Java中的套接字(Socket),根据我目前的理解,与这个主题相关的内容包括:

  • 要使多个客户端连接到服务器的同一地址(端口),则需要将每个客户端连接分配给另一个线程。

基于此,我对某些事情感到困惑,到目前为止在这里或在谷歌上找不到令人满意的答案。

  1. 如果套接字是同步的,如果两个客户端同时尝试连接会发生什么情况,服务器如何决定谁会先连接?

  2. 服务器如何处理来自一个客户端的多个消息?我的意思是,它是否按顺序处理?返回有序的消息吗?

  3. 相同的问题,但涉及来自多个客户端的多个消息时呢?

  4. 如果消息是无序的,如何实现有序处理?(在Java中)

抱歉有这么多问题,但对我来说所有这些问题都是相关的...

编辑:
正如评论中所说,我误解了同步的概念,所以更改了那部分。
朋友们,我们在这里提问是为了学习,而不是为了被其他人批评,请在给出负评之前考虑一下,好吗?

英文:

Ok, so let´s clarify the questions...

I'm studing Sockets in Java, from my understood until now, related to this subject are:

  • To make multiple clients to connect to only one address in the server (port), then it is necessary to assign each client connection to another thread

Based on that I got confused about somethings AND could not find any acceptable answer here or at Google until now.

  1. If Socket is synchronous, what happens if 2 clients try to connect AT THE SAME TIME and how the server decides who will connect first?

  2. How the server process multiple messages from one client? I mean, does it process in order? Return ordered?

  3. Same question above BUT with multiple messages from multiple clients?

  4. If the messages are not ordered, how to achieve that? (in java)

Sorry about all those questions but for me all of them are related...

Edit:
As the comment said, I misunderstood the concept of synchronization, so changed that part.
Guys we ask here to LEARN not to get judged by other SO think about that before giving -1 vote ok.

答案1

得分: 2

  • 当两个客户端尝试同时连接时,这是不可能的:网络基础设施能够保证这一点。两个请求同时发生被称为冲突(维基百科),网络会以某种方式处理它:可以通过检测或通过避免来处理。

  • 服务器如何处理来自一个客户端的多个消息?我的意思是,它按顺序处理吗?
    是的。Socket类API使用TCP/IP协议,该协议在每个数据段中包括序列号,并重新排序数据段,以便按照它们发送的顺序进行处理,这可能与它们接收的顺序不同。

  • 同样的问题,但涉及来自多个客户端的多个消息?
    不能保证来自多个源的数据段的相对顺序。

英文:

> what happens if 2 clients try to connect AT THE SAME TIME

It is impossible for 2 clients to connect at exactly the same time: networking infrastructure guarantees it. Two requests happening at the exact same time is called a collision (wikipedia), and the network handles it in some way: it can be through detection or through avoidance.

> How the server process multiple messages from one client? I mean, does it process in order?

Yes. The Socket class API uses the TCP/IP protocol, which includes sequence numbers in every segment, and re-orders segments so that they are processed in the order they are sent, which may be different from the order they are received.

If you used DatagramSocket instead, that would use UDP, which does not guarantee ordering.

> Same question above BUT with multiple messages from multiple clients?

There are no guarantees of the relative ordering of segments sent from multiple sources.

huangapple
  • 本文由 发表于 2020年8月4日 03:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63235408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定