简体   繁体   English

c#UDP客户端服务器问题

[英]c# UDP client server problems

I have a udp client and a udp server. 我有一个udp客户端和一个udp服务器。 The udp client and server is really disconnected physically. udp客户端和服务器实际上是物理连接断开的。 So I can only send messages to the udp server but I can't get acknowledgements whether the data was receieved properly. 所以我只能向udp服务器发送消息,但我无法确认数据是否已正确接收。

So what I am doing is to send a header of 4 bytes that will specify the amount of data I am about to send. 所以我正在做的是发送一个4字节的标头,它将指定我要发送的数据量。 And then I read the data according to the size I sent earlier. 然后我根据之前发送的大小读取数据。

Client Side 客户端

// Sending Header(header is the size of the data)
byte[] header = BitConverter.GetBytes(data.Count());
socket.SendTo(header, 0, header.Count(), SocketFlags.None, outEP);
// Sending Data
socket.SendTo(data, 0, data.Count(), SocketFlags.None, outEP);

Server Side 服务器端

// Receiving input size
int receivedCount = socket.EndReceive(result);
// Header is filled on BeginReceive
Int32 count = BitConverter.ToInt32(header, 0);
if(count != receivedCount) throw;
// Then I receive the count relevent
socket.Receive(data, 0, count, SocketFlags.None);

A couple problems come up: 出现了几个问题:

  1. count != receivedCount count!= receivedCount

    This is why I did the check, but it sometimes happens and sometimes it doesn't..I do need a maximum guarantee. 这就是我做检查的原因,但它有时会发生,有时它不会......我确实需要最大限度的保证。 I found out that when the count is different its not because of fallen bytes or network issues. 我发现当计数不同时,不是因为字节数减少或网络问题。 I just sometimes don't receive the header - only the data and when I read the 4 bytes I actually read the 4 bytes of the data thats why it fails. 我有时候不接收标题 - 只有数据,当我读取4个字节时,我实际上读取了数据的4个字节,这就是它失败的原因。

    The data just sometimes Overrides the header. 数据有时会覆盖标题。 Why is that? 这是为什么? and how can I solve this? 我该如何解决这个问题?

  2. Sometimes I also get the following error on the socket.Receive 有时我也会在socket.Receive上收到以下错误

    A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself 在数据报套接字上发送的消息大于内部消息缓冲区或某些其他网络限制,或者用于接收数据报的缓冲区小于数据报本身

    The problem is that is sometimes happens and most of the times it doesn't... I can just receive it all in a max buffer..but then I would have memory problems. 问题是,有时会发生,而且大部分时间都没有...我可以在最大缓冲区中接收所有内容..但是我会遇到内存问题。

    This should be in high performance and guaranteed delivery as much as possible.. This must be udp because the server side can't physically contact the client side It is one way - client to server. 这应该是高性能和尽可能保证交付。这必须是udp,因为服务器端无法物理联系客户端这是一种方式 - 客户端到服务器。

UDP doesn't guarantee the packets arrive in the order you send them (or that they arrive at all). UDP不保证数据包按照您发送的顺序到达(或者它们完全到达)。 I doubt anything gets overridden / overwritten. 我怀疑有什么被覆盖/覆盖。 You'll have to send the header and payload in one packet. 您必须在一个数据包中发送标头和有效负载。

Have you considered using TCP? 你考虑过使用TCP吗? You may argue that UDP is faster, but if you are going to implement packet reordening yourself, you'll lose that benefit. 您可能会认为UDP更快,但如果您要自己实施数据包重新排序,您将失去这种好处。 (missed that it must be UDP). (错过了必须是UDP)。

If you look at TCP (network layer handles retransmissions) or SIP over UDP (software handles retransmissions), there are means to detect lost packets and retransmit them. 如果你看TCP(网络层处理重传)或SIP over UDP(软件处理重传),有办法检测丢失的数据包并重新传输它们。 You could for instance number your packets and have the receiver acknowledge receiving each one. 例如,您可以对数据包进行编号,并让接收方确认接收每个数据包。

You will need a two way communication when using UDP and ensure that there are no lost, double or disordered packets. 使用UDP时,您将需要双向通信,并确保没有丢失,双重或无序数据包。
Have you looked at protocols like PGM - Pragmatic Multicast? 你看过像PGM这样的协议 - 实用多播?
Furthermore you might need a single thread just for cleaning the network stack quickly and post then the data into a queue or some other memory data structure. 此外,您可能需要一个单独的线程来快速清理网络堆栈,然后将数据发布到队列或其他一些内存数据结构中。 The network stack is a lot smaller than you can imagine and fuller faster than you ever thought. 网络堆栈比您想象的要小很多,并且比您想象的更快。
Edit: The problem with UDP is that by definition, you might have lossed or duplicate packates or changes in the order, even if the receiver detects them, how should the receiver request them to be corrected when no communication is possible. 编辑:UDP的问题在于,根据定义,您可能已经丢失或重复打包或更改顺序,即使接收方检测到它们,接收方如何在无法进行通信时请求更正它们。

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

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