Java – does my server use both TCP and UDP?

I'm writing a client / server application. I really can't find a guide that meets my needs Doing my own business will cause many design defects before I even start For example, the server should update the client status once per second more than once I assume I can't update each client individually, but UDP broadcasting should solve this problem However, I still need a TCP channel to reliably communicate with each client, such as chat messages, user input, etc

Studying this topic seems to be that a server can use both protocols at the same time - but only possible (not perceptual) No one has proposed such an approach. In fact, I gather from this article, and neither method is easy to use

Now I am very confused about how I should handle the data in my server Unless I completely misunderstand the information about packet loss, I want to ensure that the 'server request packet' entered by the user will not be lost All the answers about using UDP to ensure delivery say that TCP is used instead of TCP What's more frustrating is that every server / client program, I can imagine, needs at least some guaranteed messages (such as "disconnect" messages?)

Should I use UDP, TCP, or am I just thinking this is completely wrong?

Solution

Let's get some facts:

>UDP is unreliable Forever In some cases, UDP can be particularly damaging; For example, if there is network congestion, rate limit or traffic analysis, or if the UDP message size is greater than MTU. > UDP broadcast is only applicable to your local network segment Internet routers usually do not allow broadcasting to spread further This really limits its usefulness. > UDP multicast may be a possibility, except that it is also blocked

So this may give you two main possibilities:

>UDP peer-to-peer messaging from your server to each client. > TCP from the server to each client

Another possibility is some kind of peer-to-peer grid communication using UDP or TCP, but this becomes very complex Don't go there unless you really need to, really know what you're doing

So your question

I recommend using TCP between the server and each client because it is the simplest For further simplification, each client uses multiple TCP connections to avoid multiplexing multiple "conversations" single sockets

The network performance is not optimal, but its application may be very good And I suspect this is part of an application that you want to spend all your developer time on

When you reach the location of the working application (client and server) and someone uses it, you may find (or not) that the network is the main bottleneck and the cause of user dissatisfaction Then you will see optimizing communication in application version 2.0

When you implement version 2.0 (or 3.0...) to address scalability, you will need to move away from relying on a single server In short, if you have n clients and N is increasing, a single server will not be able to cope at some time Other things in application design can also be problematic For example, in a game application, you can't send continuous updates about each player to all other players... The number of players is growing But please note that these problems are largely independent of the network protocol you use

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>