41

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

red0ct
  • 4,840
  • 3
  • 17
  • 44
jokoon
  • 6,207
  • 11
  • 48
  • 85

2 Answers2

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    what about listen() ? and about the bind() method, does it mean I receive everything that land on that port ? – jokoon Jun 15 '11 at 11:00
  • 1
    @gokoon 1. listen() is for TCP. It has nothing to do with UDP. 2. What exactly does 'what about bind()' mean, as a question? 3. Yes you receive everything addressed to that UDP port if you aren't connected. – user207421 Jun 22 '11 at 10:48
  • @EJP This http://linux.die.net/man/7/udp says that when connect() is called, UDP uses read/write! So read/write or send/recv ? – onmyway133 Sep 11 '13 at 07:24
  • 1
    @entropy No it doesn't. It says "When connect(2) is called on the socket, the default destination address is set and datagrams can now be sent using send(2) or write(2)". Similarly in that circumstance you can use either *read()* or *recv()*. – user207421 Nov 14 '13 at 22:49
  • @EJP: After calling `connect()`, what happens to datagrams from peers other than the default destination? Are they discarded? Can you `bind()` multiple sockets to the same local port and `connect()` each to a different peer, then will they all receive data coming from their specified peer? – Ben Voigt Jul 21 '14 at 20:34
  • @BenVoight Datagrams from the wrong source are discarded. I guess you could do that multiple bind() thing. – user207421 Aug 08 '14 at 23:23
  • 1
    @EJP If I do a bind() function before the first sendto() calls, does it overrides the automatic bind() done by the first send() /sendto() /recv() /revfrom() calls ? – Sorcrer Jan 30 '15 at 06:36
  • @Sorcrer The automatic bind only happens if there hasn't been an explicit bind. – user207421 Apr 22 '15 at 22:36
32

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
enobufs
  • 860
  • 1
  • 7
  • 12