Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


sento() using a binded socket


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 lomateron   Members   -  Reputation: 380

Like
0Likes
Like

Posted 12 November 2013 - 03:25 AM

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



Sponsor:

#2 KnolanCross   Members   -  Reputation: 1519

Like
0Likes
Like

Posted 12 November 2013 - 10:07 AM

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, 12 November 2013 - 10:07 AM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


#3 lomateron   Members   -  Reputation: 380

Like
0Likes
Like

Posted 12 November 2013 - 11:23 AM

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? 



#4 KnolanCross   Members   -  Reputation: 1519

Like
0Likes
Like

Posted 12 November 2013 - 01:53 PM

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, 12 November 2013 - 01:53 PM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


#5 hplus0603   Moderators   -  Reputation: 6606

Like
0Likes
Like

Posted 12 November 2013 - 06:11 PM

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.


enum Bool { True, False, FileNotFound };

#6 lomateron   Members   -  Reputation: 380

Like
0Likes
Like

Posted 13 November 2013 - 12:46 AM

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, 13 November 2013 - 01:21 AM.


#7 hplus0603   Moderators   -  Reputation: 6606

Like
1Likes
Like

Posted 13 November 2013 - 10:23 AM

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.


enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS