简体   繁体   English

通过C中的套接字函数制作可靠的UDP

[英]Making a reliable UDP by socket function in c

I am having this doubt in socket programming which I could not get cleared by reading the man pages. 我在套接字编程中有这个疑问,我无法通过阅读手册页来解决。

In c the declaration of socket function is int socket(int domain, int type, int protocol); 在c中,套接字函数的声明为int socket(int domain, int type, int protocol);

The linux man page says that while type decides the stream that will be followed the protocol number is the one that decides the protocol being followed. linux手册页说,当类型决定要遵循的流时,协议号就是决定要遵循的协议的流。

So my question is that suppose I give the type parameter as SOCK_STREAM which is reliable and add the protocol number for UDP would it give me a reliable UDP which is same as TCP but without flow control and congestion control. 所以我的问题是,假设我将类型参数指定为SOCK_STREAM ,并且添加了UDP的协议号,它将为我提供一个与TCP相同但没有流控制和拥塞控制的可靠UDP。

Unfortunately I can't test this out as I have a single machine so there is no packet loss happening. 不幸的是,由于我只有一台机器,因此无法进行测试,因此不会发生数据包丢失的情况。

Could anyone clear this doubt? 谁能解决这个疑问? Thanks a lot... 非常感谢...

UDP cannot be made reliable. 无法使UDP可靠。 Transmission of the packets is done on a "best effort" capacity, but any router/host along the chain is free to drop the packet in the garbage and NOT inform the sender that it has done so. 数据包的传输以“尽力而为”的能力完成,但是链上的任何路由器/主机都可以随意将数据包丢弃到垃圾中,而不会通知发送方它已经这样做了。

That's not to say you can't impose extra semantics on the sending and receiving ends to expect packets within a certain time frame and say "hey, we didn't get anything in the last X seconds". 这并不是说您不能在发送和接收端施加额外的语义以期望在特定时间范围内发送数据包,而是说:“嘿,我们在最近的X秒内什么都没收到”。 But that can't be done at the protocol level. 但这不能在协议级别完成。 UDP is a "dump it into the outbox and hope it gets there" protocol. UDP是一种“将其转储到发件箱中并希望它到达那里”的协议。

No. For an IPV4 or IPV6 protocol stack, SOCK_STREAM is going to get you TCP and the type SOCK_DGRAM is going to give you UDP. 否。对于IPV4或IPV6协议栈,SOCK_STREAM将为您提供TCP,而SOCK_DGRAM类型将为您提供UDP。 The protocol parameter is not used for either of the choices and the socket library is typically expecting a value of 0 to be specified there. 协议参数不用于任何一个选择,套接字库通常期望在那里指定0值。

If you do this: 如果您这样做:

socket(AF_INET,SOCK_STREAM,IPPROTO_UDP):

socket() will return -1 and sett errno to socket()将返回-1并将errno设置为

EPROTONOSUPPORT
          The protocol type or the specified protocol
          is not supported within    this domain.

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

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