Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of using sendto()/recvfrom() instead of connect()/send()/recv() with UDP sockets?

I can grasp the concept of TCP vs UDP, but still I don't know why are there 2 ways of sending UDP packets, and with that I still don't understand if this is absolutely necessary to bind() and accept()...

like image 726
jokoon Avatar asked May 31 '11 15:05

jokoon


People also ask

What is the use of socket Recvfrom ()?

The recvfrom() function receives data on a socket named by descriptor socket and stores it in a buffer. The recvfrom() function applies to any datagram socket, whether connected or unconnected. The socket descriptor. The pointer to the buffer that receives the data.

What is the difference between RECV and Recvfrom?

Unlike the recv() call, which can only be used on a connected stream socket or bound datagram socket, recvfrom() can be used to receive data on a socket whether or not it is connected. If no messages are available at the socket, the recvfrom() call waits for a message to arrive unless the socket is nonblocking.

What is the difference between the functions send () and sendto ()?

send(), sendto() These functions send data to a socket. Generally speaking, send() is used for TCP SOCK_STREAM connected sockets, and sendto() is used for UDP SOCK_DGRAM unconnected datagram sockets.

Can you use RECV for UDP?

On MSDN it states that creating a UDP socket via the connect() function should bind the socket to the address and port specified and as a result be able use the send() and recv() functions with the created socket.


2 Answers

  1. accept() is for TCP. It has nothing to do with UDP.

  2. connect() in UDP doesn't do anything to the other end, it just conditions the local API to know who you are sending to and receiving from.

  3. If you don't already know that, or don't care, or want to send to multiple destinations with the same socket, you don't use connect(), you use sendto() instead. Similarly for receiving.

    Consider a UDP server for example. It would call recvfrom(), so it would get the source address information, process the request, create the response, and send it to that address via sendto(). No connect() involved anywhere, ergo not possible to use either send() or recv().

  4. It is only necessary to bind() a server, because the clients need a fixed port number to send to. A client needn't bind() at all: an automatic bind() will take place on the first send()/sendto()/recv()/recvfrom() using a system-assigned local port number.

like image 105
user207421 Avatar answered Sep 16 '22 17:09

user207421


It is important to understand that TCP is connection-oriented, while UDP is a connectionless protocol.

  • TCP: You need to connect first prior to sending/receiving data to/from a remote host.
  • UDP: No connection is required. You can send/receive data to/from any host.

You will normally use sendto() on UDP socket in order to specify the destination. Similarly, you would normally use recvfrom() to know where the UDP data was received from.

However, you can actually use connect() on UDP socket as an option. In that case, you can use send()/recv() on the UDP socket to send data to the address specified with the connect() and to receive data only from the address. (The connect() on UDP socket merely sets the default peer address and you can call connect() on UDP socket as many times as you want, and the connect() on UDP socket, of course, does not perform any handshake for connection.)

Hope this helps.

like image 25
enobufs Avatar answered Sep 17 '22 17:09

enobufs