Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Nov 12, 2018 9:17 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: No bind on sockets?
PostPosted: Sat Aug 20, 2011 8:58 am 
Offline

Joined: Sat Jul 16, 2011 2:45 pm
Posts: 76
Just wondering why socket core and similar socket-classes do not have a bind option? (only one of the UDP classes does)

I may be mistaken but it appears you can only set the port on the local machine while listening, or the remote server while connecting but not both?

I know it may be rare, but if you have multiple applications on a local network and two machines get the same port number randomly assigned to them and /or the router is configured so specific ports point to specific computers instead of simply specifying a client-port we cant and hope we get lucky? :s


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Sat Aug 20, 2011 9:32 am 
Offline
User avatar

Joined: Wed May 10, 2006 2:42 pm
Posts: 2985
Location: Germany
listen lets the socket wait for a connection and connect connects.

What do you need bind for?
if you want to have multiple sockets waiting on a given port, use the serversocket.

And if you specify port = 0 for listening, you get a randomly picked free port.

Greetings
Christian

_________________
See you in Orlando, Florida for Real World 2013
More details and registration here:
http://www.realsoftware.com/community/realworld.php


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Sat Aug 20, 2011 10:46 am 
Offline

Joined: Sat Jul 16, 2011 2:45 pm
Posts: 76
Hi monkeybread,

I want to bind the socket to a local port when doing a connect basically. Listen does this, but I don't want to set a server up that is listening to all incoming traffic.

I want to connect to a server and make sure it returns it's replies to a specific port so there isn't any conflicts with multiple clients under the same IP address.

I cant even see how one would use socketcore to add functionality for other protocol by forming your own packets? Cant even see how you could do a simple ping :/


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Sat Aug 20, 2011 1:10 pm 
Offline
User avatar

Joined: Wed May 10, 2006 2:42 pm
Posts: 2985
Location: Germany
you don't need to worry. Your sending socket will get automatically a free port on connecting. And other side will also use a free port.

And on connecting sockets tell each other what port they'll use.

Greetings
Christian

_________________
See you in Orlando, Florida for Real World 2013
More details and registration here:
http://www.realsoftware.com/community/realworld.php


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Sat Aug 20, 2011 2:04 pm 
Offline

Joined: Sat Jul 16, 2011 2:45 pm
Posts: 76
The random port is the issue though, i.e. when your behind a NAT router, have port forwarding set up and so on.

It's much easier to if possible, allow the program to set its own local port than some form of OS specific configuration just for say, a one time connection :<


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Sat Aug 20, 2011 2:16 pm 
Offline
User avatar

Joined: Wed May 10, 2006 2:42 pm
Posts: 2985
Location: Germany
But with NAT routing you only need to setup the port which receives the connection. The other port is known to NAT and setup automatically for answers.

Greetings
Christian

_________________
See you in Orlando, Florida for Real World 2013
More details and registration here:
http://www.realsoftware.com/community/realworld.php


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Sat Aug 20, 2011 3:27 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
The random port thing works just fine with NAT/router/firewall. Port forwarding only comes into play on the initial connection, which is to a known, forwarded port. Once the server replies to the connection, everything flows through the router/firewall unimpeded. In other words, if you're behind a firewall, it blocks incoming connections except on the specific ports you have "opened". But once you respond to the connection, the firewall says, "OK, this conversation is trusted" and allows the packets to flow.


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Mon Aug 22, 2011 4:49 pm 
Offline

Joined: Sat Jul 16, 2011 2:45 pm
Posts: 76
Hey guys, thanks for the replies.

Well I'm probably not explaining myself very well so maybe this link would do it better: http://www.tomshardware.co.uk/forum/288 ... ers-server

It might not be an issue with all routers, but still, a lot of games allow you to change your client port for that very reason, being given random isn't an option and is too restrictive. AFAIK you are supposed to be able (as part of the standard socket service) to bind it to a local port, with 0 meaning random (as far as my limited knowledge on the subject goes)

It looks like RS sockets are not giving full socket functionality let alone Raw sockets as a bare minimum; so looks like i'll probably have to make a class to use API calls and implement it in each OS that way :/ Can it really be called a socket if it isn't implementing all the pretty much standardized features of sockets :/


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Tue Aug 23, 2011 8:41 am 
Offline
Site Admin
User avatar

Joined: Tue May 06, 2008 1:07 pm
Posts: 1464
Location: NotEvenOnTheMap, CT
RS sockets work just fine. Pick your port, listen to it. What have you tried, what is happening, and what is your expected result? Answer those three questions, and I'm sure we'll get you the answer you need.

_________________
Thom McGrath - @tekcor
Web Framework Architect, Real Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Tue Aug 23, 2011 2:23 pm 
Offline

Joined: Sat Jul 16, 2011 2:45 pm
Posts: 76
Thom McGrath wrote:
RS sockets work just fine. Pick your port, listen to it. What have you tried, what is happening, and what is your expected result? Answer those three questions, and I'm sure we'll get you the answer you need.
Thanks for the reply Thom,

Listen is fine for a server but I'm not trying to set up a server. I want to bind to a local port, then .connect to a server without being assigned a random port for my end of the connection?

I don't use sockets that much but i thought the general set up was:

Socket() - create a socket of xyz type.
Bind() - Bind the socket to a local address/Port (primarily for listening)
Listen() - Wait for inbound packets from a client to set up a connection at defined local address
Connect() - Connect to a server, receiving replies at defined local address or random free port if no binding was done.
send() /Receive() - send /receive data.

You would call bind() before listen() too, with sockets normally :/ Not just listen on its own... I understand RS has probably taken bind away to make things simpler but has taken functionality of sockets out by doing so.

Normal socket functionality:
http://pubs.opengroup.org/onlinepubs/00 ... nnect.html
Quote:
DESCRIPTION

The connect() function shall attempt to make a connection on a socket. The function takes the following arguments:
socket
Specifies the file descriptor associated with the socket.
address
Points to a sockaddr structure containing the peer address. The length and format of the address depend on the address family of the socket.
address_len
Specifies the length of the sockaddr structure pointed to by the address argument.

If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the socket's address family is AF_UNIX, is an unused local address.

If the initiating socket is not connection-mode, then connect() shall set the socket's peer address, and no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send() functions, and limits the remote sender for subsequent recv() functions. If address is a null address for the protocol, the socket's peer address shall be reset.

If the initiating socket is connection-mode, then connect() shall attempt to establish a connection to the address specified by the address argument. If the connection cannot be established immediately and O_NONBLOCK is not set for the file descriptor for the socket, connect() shall block for up to an unspecified timeout interval until the connection is established. If the timeout interval expires before the connection is established, connect() shall fail and the connection attempt shall be aborted. If connect() is interrupted by a signal that is caught while blocked waiting to establish a connection, connect() shall fail and set errno to [EINTR], but the connection request shall not be aborted, and the connection shall be established asynchronously.

If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect() shall fail and set errno to [EINPROGRESS], but the connection request shall not be aborted, and the connection shall be established asynchronously. Subsequent calls to connect() for the same socket, before the connection is established, shall fail and set errno to [EALREADY].

When the connection has been established asynchronously, select() and poll() shall indicate that the file descriptor for the socket is ready for writing.

The socket in use may require the process to have appropriate privileges to use the connect() function.


All sockets I have seen pretty much act in the same way. i.e. you can bind to a local address before using connect.


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Tue Aug 23, 2011 2:31 pm 
Offline
Site Admin
User avatar

Joined: Tue May 06, 2008 1:07 pm
Posts: 1464
Location: NotEvenOnTheMap, CT
Ok, when using a TCP socket, you MUST connect to a specific server and port. No alternative. This is just the way TCP works, it's not our limitation.

So if the server at server.host.com is listening to port 9000, set your address to "server.host.com" and port to 9000. Piece of cake.

It sounds like you're looking for UDP though. In that case, there is no real client and no real server. Everybody is on the same level. So choose your port and bind to it. All computers on the same subnet bound to the same port can broadcast to each other.

I'm over simplifying, but that's the general idea. Does that get you closer to your goal?

_________________
Thom McGrath - @tekcor
Web Framework Architect, Real Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Tue Aug 23, 2011 3:31 pm 
Offline

Joined: Sat Jul 16, 2011 2:45 pm
Posts: 76
I'm not saying you 'can't' specify server address, that would just be insane ;)

With the example you gave you are still saying "connect to server.host.ext:9000" but with bind() or are saying "Reply to me at Client:9876" instead of "Reply to me at Client:RandomPort assigned by OS"

Without using "Bind" you are letting windows (Or the Socket host system) assign you a port.

To use Listen you have to bind to local address. But "listen" in itself is only a "state" and not the actual assignment of a port address. If you're happy with a random port, then you "bind" to port 0 before calling listen.

If you're happy with a random port on connect, bind can be skipped because you are initiating the connection and passing the port onto the server, so you dont personally need to know the port address before hand. BUT if you want to bind a connection to a local port you can use "bind 9876" so that when you do Connect to server.ext:9000 you know that the connection is going to be between Client:9876 <==|==>Server:9000

RS' Socket implimentation when calling TcpSocket.Listen must be doing a Socket() followed by Bind() (using the port properties) then Listen() with the OSes socket API (im assumign you are using the OSes sockets)

however, it seems RS's sockets are skipping the use of bind() when doing Connect() (Which is perfectly valid and 100% works. But you are allowed to call bind() (AFAIK) before Connect() to self assign a local port providing it is free

I've never tried IP spoofing, but I assume if you did Bind "some random person IP address + port" and then did connect(), the server would be sending reply packets to some random person's IP:port address since the sockets are handling the IP protocol part of the packet. Providing the socket dont filter out such spoofing.

You cant do that with RS's implementation because there isn't a bind to use :/


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Tue Aug 23, 2011 3:53 pm 
Offline
Site Admin
User avatar

Joined: Tue May 06, 2008 1:07 pm
Posts: 1464
Location: NotEvenOnTheMap, CT
So you're bothered by the fact that connecting to a server doesn't always get you the port you connected to? That's normal when the server must accept multiple connections. Since the server itself cannot handle more than one connection per port, the server accepts the connection on port 9000, then switches to a different open port and resumes accepting connections on 9000. The client's port property will see that same new port number.

Without knowing the problem you're trying to solve, it feels like you're overthinking this.

_________________
Thom McGrath - @tekcor
Web Framework Architect, Real Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Tue Aug 23, 2011 3:55 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Are you sure you want TCP? As Thom said, it sounds more like UDP, in which case Connect = Bind. With UDP, the port you set is the local port. When you send a packet, you supply the remote port in the Datagram. That gives you a fixed port on both ends.


Top
 Profile  
Reply with quote  
 Post subject: Re: No bind on sockets?
PostPosted: Tue Aug 23, 2011 4:26 pm 
Offline

Joined: Sat Jul 16, 2011 2:45 pm
Posts: 76
Thom,

I'm on about clients not servers, sorry for any confusion :(

When i connect to google.com:9000 how does google know who i am? it cant send me the web page unless I tell it that my IP is 77.101.214.40.

Connect is going to tell google "77.101.214.40" and then a random free port on my computer to use to reply to. Thats the standard functionality of sockets you find built into an operating system?

But, you should also be able to bind the socket to port "xyz" such as 9876 in the previous example. So when you connect to google.com:9000, the socket will put 77.101.214.40:9876 in the IP header and the connection between you, the client and google on your machine will be bound to port 9876 instead of a random port which might have been 64000?

Timhare,
I wasn't too sure if the UDP class was allowing both, since I was looking for "bind" I guess I probably got confused looking through all the socket types since I have only used sockets via the direct API methods.

TCP I do need to use for my project however, I personally can not see why you wouldn't be able to set the local port on a tcp like the udp counterpart when making a connection and when I was searching I did find many instances of people saying using bind before connect to define a local port for TCP :/

Thanks for taking the time to reply guys, much appreciated


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group