• Advertisement
Sign in to follow this  

sento() using a binded socket

This topic is 1531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this UDP socket that I binded to recvfrom(), after I receive I want to send back the data.

can I use the socket that I used to recvfrom()?

if yes, is that normal to do? do I have to bind the socket after using sendto() so I can use it again to recvfrom()?

Share this post


Link to post
Share on other sites
Advertisement

You bind a socket to reserve a port to it. You will can bind only once and before the recv_from.

 

Assuming you are using C, you can use sendto with the same socket.

The way it goes is:

 

1) Create socket.

2) Bind it to a port.

3) Call recvfrom, you will have 4 information:

- The block received.

- The size of the block received.

- The address that sent you information[1].

- The size of the address that sent you the information[2].

4) Call sendto with the socket created at the step 1, and the adress information [1][2] that you received on the former step.

5) If you need to send/receive more information, go to step 3. Else go to step 6.

6) Call close and finish.

Edited by KnolanCross

Share this post


Link to post
Share on other sites

I am using winsock on C++, and the sendto() documentation says that this function binds the socket to the addres given in the parameter, so I was asking because if it binds it to that then I should bind it after using sendto() to the address of my own computer to recvfrom(), no? 

Share this post


Link to post
Share on other sites

Ah, you haven't said that before tongue.png

 

I have never used winsock to be sure (linux dev here). If I were you I would use the msdn examples of recvfrom and sendto to make a very simple client/server app, sorry not to be able to help you more with it.

Edited by KnolanCross

Share this post


Link to post
Share on other sites

Note that "bind" is a local concept, and "connect" is a remote concept.

 

If the first thing you do on a socket is sendto(), without a bind() before it, then it will be bound to a "random" local UDP port number, and cannot be re-bound later. This is typically what you want for clients.

 

If the first thing you do on a socket is recvfrom(), then you have to first bind(), because otherwise the socket does not correspond to a local port number, and the recvfrom() will not succeed. This is typically the case for servers.

 

In general, you should study the way that UDP addressing works; the four-tuple of source IP, source port, destination IP, and destination port, for a particular UDP datagram, and the function of these functions should become clear.

Share this post


Link to post
Share on other sites

hplus0603

If the first thing you do on a socket is recvfrom(), then you have to first bind()

 

That is what I do in my code like this

//create socket...
//bind socket...
char pointe[4];
SOCKADDR_STORAGE unknwSender;
int sizeSocke;
iResult = recvfrom(ConnectSocket, pointe, 4, 0, (sockaddr*)&unknwSender, &sizeSocke);

now can I do this code just after the previous code?

iResult = sendto( ConnectSocket, pointe, 4, 0 ,(sockaddr*)&unknwSender, sizeSocke );

iResult = recvfrom(ConnectSocket, pointe, 4, 0, (sockaddr*)&unknwSender, &sizeSocke);

 

.........................................................

Ok I think I understand now

The sendto() of this previous code will not change the port so I can use recvfrom() whitout having to bind again the socket, is that correct?

 

And in the client code I just find the server sockaddr, then create a socket and then use sendto() and after this I can use recevfrom(), and I never have to use the bind() function in any part of the code, is that correct?

Edited by lomateron

Share this post


Link to post
Share on other sites

That sounds correct.

 

The flow for a UDP server is typically:

 

socket()
bind()
forever() {
  recvfrom()
  sendto()
}

 

The flow for a UDP client is typically

 

socket()
gethostbyname()
forever() {
  sendto()
  recvfrom()
}

 

In either case, the same socket is used throughout.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement