简体   繁体   English

侦听UDP套接字并转发到TCP套接字(可能是Java)

[英]Listen to UDP socket and forward to TCP socket (Java possibly)

有没有一种方法可以通过UDP侦听器侦听UDP数据包,然后将数据转换为字符串或其他任何内容,然后通过TCP套接字发送到TCP服务器,我对UDP不太熟悉,因此任何技巧都很棒

I wouldn't worry about UDP to TCP I would worry about the other way around. 我不会担心UDP到TCP的问题,反而会担心其他问题。

You can't receive what you can't fit in your buffer through UDP so all receives will translate to one or more TCP sends. 您无法通过UDP接收无法容纳在缓冲区中的内容,因此所有接收都将转换为一个或多个TCP发送。

TCP on the other hand is a segment based protocol and you may receive only the part of the reassembled segment which has been acknowledged and is available to the application buffer, this may not be the entire application layer message in all cases as I will illustrate. 另一方面,TCP是基于段的协议,您可能只收到已确认的重组段的一部分,并且该段可用于应用程序缓冲区,这在所有情况下可能都不是整个应用程序层消息,正如我将说明的那样。

Let Data = 30,000 bytes of data... 让数据= 30,000字节的数据...

To get this data somewhere in the machine you can just pass a pointer but to get it to another machine you have to use a network or hardware application which is typically accessed via a driver. 要在机器中某处获取此数据,您可以仅传递一个指针,但要将其传递到另一台机器上,则必须使用通常通过驱动程序访问的网络或硬件应用程序。

Sockets are a common design paradigm or more succinctly interface which allows vastly different networks and mediums to communicate heterogeneously... 套接字是一种常见的设计范例或更简洁的接口,它允许截然不同的网络和介质进行异构通信。

Through a socket you can simply worry about the protocol such as IP or TCP or UDP maybe ICMP and you don't have to worry about how the data gets to the hardware. 通过套接字,您可以简单地担心诸如IP或TCP或UDP之类的协议(例如ICMP),而不必担心数据如何到达硬件。

The network stack usually is a component of the operating system which inter-operates with the hardware and software drivers and is hidden behind the Socket interface such that the semantics of the hardware and network do not matter when only the protocol is of concern... 网络堆栈通常是操作系统的一部分,可与硬件和软件驱动程序进行互操作,并隐藏在Socket接口的后面,因此当仅关注协议时,硬件和网络的语义就无关紧要...

Back to Data... 返回数据...

The data sent will be split into several packets which can fit on the network medium, this is usually called the Maximum Transmittable Unit. 发送的数据将被拆分成几个适合网络介质的数据包,通常称为最大可传输单元。

It allows the the required header fields of the medium as well as a sufficient amount of data to allow for flow control to a driver. 它允许媒体的必需标头字段以及足够数量的数据,以允许对驱动程序进行流控制。

The driver receives this data and creates network packets which can be trivially sent and received on the medium and local machine network. 驱动程序接收此数据并创建网络数据包,这些数据包可以在中型和本地计算机网络上轻松发送和接收。

To get to the physical network additional layers must be added to the packet such as the IP and protocol headers, in this case TCP. 为了到达物理网络,必须在数据包中添加其他层,例如IP和协议标头,在本例中为TCP。

The network stack let's the driver know Data is ready to be sent during a call to send. 网络堆栈使驱动程序知道在发送呼叫期间已准备好发送数据。

The data is copied to the network stack and the application can proceed (TCP or UDP) 数据被复制到网络堆栈,应用程序可以继续(TCP或UDP)

Segments are created by the protocol in the maximum segment size or less under tcp and they are transmitted. 协议段会在tcp下以最大段大小或更小长度创建段,然后进行传输。

When any one segment is received and acknowledged is can be received by a call to recv . 当接收到任何一个段并进行确认时,可以通过调用recvrecv

The order of the data is maintained by the network stack and if an entire segment is not received it is re-transmitted.. if part of a segment is recieved it is held until the segments which preceeded it were recieved and acknowledged. 数据的顺序由网络堆栈维护,如果未接收到整个段,则将重新传输该数据。如果接收到段的一部分,则将保留该段,直到接收并确认之前的段为止。

Some implementations of TCP allow for partially recieved segments to be made available to the application buffer (either with a special configuration or otherwise, usually with disabling tcp retransmission as well as send and receive coalescing but not limited to) even though loss or fragmentation has occurred within part of a segment during reception, usually the remainder of the segment is recieved and made available to the application during the next recieve however it's totally possible that more data arrives from a new segment and the old data either never does or arrives so late that your application is not eligible to recieve data which was already released to the application. TCP的某些实现允许部分接收的段可用于应用程序缓冲区(具有特殊配置或其他配置,通常禁用tcp重传以及发送和接收合并,但不限于此),即使发生丢失或碎片在接收过程中某个段的一部分内,通常会接收到该段的其余部分,并在下一次接收时将其提供给应用程序使用,但是完全有可能有更多的数据来自新段,而旧数据则永远不会到达或迟到,以至于您的应用程序没有资格接收已经发布到应用程序的数据。

Thus you will normally and eventually receive 30,000 bytes at the receiving end (actually quite a bit more due to IP and TCP overhead) but it's quite possible in certain situations that you receive data out of order or not at all, this can be due to hardware failure on a route in between you and the receiver and is certainly not the norm but it can indeed happen. 因此,您通常会最终在接收端收到30,000字节(由于IP和TCP开销实际上会更多),但是在某些情况下很有可能接收到的数据乱序或根本不接收,这可能是由于在您和接收方之间的路由上发生硬件故障,这当然不是正常现象,但确实可能发生。

In such cases the application layer should be able to determine when this anomaly occurs either by using a sequence number within the application protocol or by scanning the IP and TCP headers which can be achieved in various ways. 在这种情况下,应用程序层应该能够通过使用应用程序协议内的序列号或扫描IP和TCP报头来确定何时发生此异常,这可以通过各种方式实现。

Hopefully that is enough to get you thinking... Please also see When sending data larger than the SendBufferSize, how will the data be received? 希望这足以让您思考...请另参阅当发送大于SendBufferSize的数据时,如何接收数据?

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

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