简体   繁体   English

从 Java 游戏服务器到客户端的 TCP 和 UDP 连接

[英]TCP and UDP connection to client from Java game server

I'm creating a game server for a University project so I'm doing this without using any libraries / frameworks.我正在为大学项目创建游戏服务器,因此我在不使用任何库/框架的情况下执行此操作。

After a lot of research I want the client and server to do the bulk of communication such as character movement and timers using the UDP protocol as the reliability of this aspect of the game is not so important and lost packets can be compensated for.经过大量研究,我希望客户端和服务器使用 UDP 协议进行大量通信,例如角色移动和计时器,因为游戏这方面的可靠性不是那么重要,丢失的数据包可以得到补偿。

But I also want to use the TCP protocol for some other aspects of the game such as actions and events where it is critical the information reaches the client.但我也想将 TCP 协议用于游戏的其他一些方面,例如信息到达客户端至关重要的动作和事件。

My problem is that I don't know much about using UDP in general and in Java, from my understanding it is completely different to just having an open Socket object for TCP.我的问题是,我对一般情况下和 Java 中使用 UDP 知之甚少,据我所知,它与仅拥有一个用于 TCP 的开放 Socket 对象完全不同。 I'm thinking that the initial connection between the client and server will be done by TCP then once this connection has been established should the server send a port number back to the client that the client will use to communicate with the server via UDP?我认为客户端和服务器之间的初始连接将由 TCP 完成,那么一旦建立了此连接,服务器是否应该将端口号发送回客户端,客户端将使用该端口号通过 UDP 与服务器进行通信?

Which brings me to the issue of having multiple clients, do they all need to be allocated different port numbers to connect via UDP to the server?这让我想到了拥有多个客户端的问题,它们是否都需要分配不同的端口号才能通过 UDP 连接到服务器? So the server will have 1 different port number for each client connected?那么服务器将为每个连接的客户端提供 1 个不同的端口号?

My plan for the server is to have for every client connected 1 sending thread and 1 receiving thread - would I be able to handle both TCP and UDP communications in each of these threads or would there need to be 4 threads for each client with 2 for tcp and 2 for udp?我对服务器的计划是为每个客户端连接 1 个发送线程和 1 个接收线程 - 我是否能够在每个线程中处理 TCP 和 UDP 通信,或者每个客户端需要 4 个线程,其中 2 个用于UDP 的 tcp 和 2?

Again these are just my first thoughts and I don't know much about UDP so sorry if I've got the complete wrong end of the stick!同样,这些只是我的第一个想法,我对 UDP 知之甚少,如果我完全弄错了一端,我很抱歉! Thanks if anyone can help with any of this though!谢谢,如果有人可以帮助解决这个问题!

Should the server send a port number back to the client that the client will use to communicate with the server via UDP?服务器是否应该将端口号发送回客户端,客户端将使用该端口号通过 UDP 与服务器进行通信?

You can do this, or you can load it from a properties file.您可以这样做,也可以从属性文件中加载它。 Depends on your implementation.取决于你的实施。

Do [multiple clients] all need to be allocated different port numbers to connect via UDP to the server? [多个客户端]是否都需要分配不同的端口号才能通过UDP连接到服务器? So the server will have 1 different port number for each client connected?那么服务器将为每个连接的客户端提供 1 个不同的端口号?

No. A server can differentiate which client a packet belongs to from the IP address on the Datagram (UDP) packet that was received.不可以。服务器可以根据收到的数据报 (UDP) 数据包上的 IP 地址区分数据包属于哪个客户端。

Would I be able to handle both TCP and UDP communications in each of these threads or would there need to be 4 threads for each client with 2 for tcp and 2 for udp?我是否能够在这些线程中的每一个中处理 TCP 和 UDP 通信,还是需要每个客户端有 4 个线程,其中 2 个用于 tcp,2 个用于 udp?

You do not need a separate thread for sending data, because this doesn't cause the thread to block.您不需要单独的线程来发送数据,因为这不会导致线程阻塞。 Only receiving causes the thread to block, so your client program would only need 2 communication threads;只有接收会导致线程阻塞,所以你的客户端程序只需要 2 个通信线程; One to receive TCP, and another to receive UDP communications.一个接收 TCP,另一个接收 UDP 通信。

NOTE: Java does handle TCP and UDP communications quite differently.注意:Java 处理 TCP 和 UDP 通信的方式完全不同。 TCP is a stream that you write to (which automatically handles handshaking and packet lose for you), while UDP uses a DatagramPacket object that is populated with bytes and sent. TCP 是您写入的流(它会自动为您处理握手和数据包丢失),而 UDP 使用填充有字节并发送的DatagramPacket对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM