Iceman
Iceman

Reputation: 4362

Do I need a different socket on client side for listening and sending?

I am new to socket programming. The scenario is that:

1- I am creating a UDP socket, fd

2- Binding fd to an IP address/port

3- recvfrom()ing on the socket fd from destination, D

Now if I want to sendto() some data back to D, can I use the same socket, by passing in the sockaddr_in containing the IP address and port of D? Or do I need to create a new socket and do bind()?

Edit: From the first answer, it seems I do not need another socket, and do not need to bind() on that socket before transmitting. Is that correct?

Upvotes: 0

Views: 164

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409196

Yes, you can use the same UDP socket for both receiving and sending. To send directly to the peer you just received from, use the sockaddr_in structure that was filled in by recvfrom.

Upvotes: 1

Related Questions