简体   繁体   中英

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);

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.

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.

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. 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". But that can't be done at the protocol level. UDP is a "dump it into the outbox and hope it gets there" protocol.

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. 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.

If you do this:

socket(AF_INET,SOCK_STREAM,IPPROTO_UDP):

socket() will return -1 and sett errno to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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