UDP sockets, client/server

BlitzMax Forums/BlitzMax Programming/UDP sockets, client/server

Tom(Posted 2005) [#1]
Hi,

I want to use UDP in a client/server environment. On the server, should I be opening a new socket for each client, or should I use the 1 socket, and point it to each clients IP/PORT using connectSocket() as and when I need to send data to them?

Edit:

I'm also stuck on how you get the IP from a client.

Cheers
Tom


Tom Darby(Posted 2005) [#2]
In general, you should have a 'listener' port (or ports, if you're gonna have crazy-go-nuts levels of connection requests) on your server that does nothing but process incoming connection requests. Once you've received a connection request, you should open a new port on the server and tell the client to communicate with that port for the duration of the session.

To get the IP address from the client, try looking into the SocketLocalIP and SocketRemoteIP functions.


Tom(Posted 2005) [#3]
Been looking at this informative tutorial on sockets: http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingn.shtml

from that I gather max is missing a functional 'sendto()', though the groundwork is already there, it just needs adding to sockets.bmx, that would let you send directly to an IP.

The functions for finding IPs are returning LAN IPs, I'm behind a router so all I get is either 127.0.0.1 or whatever my LAN IP is at the time 192.168.x.x, I'm just looking to see if UDP sends the source IP along with itself, that'd be ideal.


RktMan(Posted 2005) [#4]
the blitz implementation of sockets is a little confusing because they didn't keep nomenclature consistent with accepted sockets API's.

having said that, it *appears* to me that with blitz, there is no sendto()/recvfrom() mechanisms specifically, and that you use the ConnectSocket() call with a UDP socket.

then you can read/write to the socket through the provided mechanisms in blitz.

and as far as IP's and "firewalls"/broadband connections, if you are behind a local network, and you ask your own machine what you ip address is, you will always get the one assigned to your machine by the router.

this address, is of course, NOT your external address so clients on the other side of the router can't use that address.

with a TCP client/server setup, the clients need to know the server ip address/listen port, but the server can query the socket, after it has accept()'ed a connection, to get the remote ip address.

magically, the address the server will see, will be the address of the remote computers router, so everything just works.

i have very little experience with UDP specifically, but i think basically, each computer needs to know the external address of the remote computers *router* ahead of time.

then, the router needs to have "port forwarding" setup to get the inbound packets to the right computer from the outside world.

i think this is because of the connectionless nature of UDP.

there isn't really a true "client/server" paradigm, each machine just throws packets onto the wire destined for the other machine.

so each machine must know the specific details of the other ahead of time, since one isn't "calling" the other.

again, not having done much UDP, i *think* what some applications do, is use traditional TCP client/serve stuff to have a "conversation" first between client/server, the computers exchange some information quickly, then they move the conversation over to UDP for the actual traffic.

grain of salt ...

Tony


Tom(Posted 2005) [#5]
I tested this and it works, needs to be added to socket.bmx within 'Type TSocket', and compiled with 'bmk makemods brl.socket' to update the module'
	Method SendTo( buf:Byte Ptr,count,destIP,destPort,flags=0 )
		Local n=sendto_( _socket,buf,count,flags,destIP,destPort )
		If n<0 Return 0
		Return n
	End Method

I'll look into use TCP for establishing a connection and swapping important info. But getting the net or external IP is a must for people with routers.


Tibit(Posted 2005) [#6]
You can ofcourse use sendto_ receivefrom_ directly in your code (until mark implements the above). That's how I did it in TNet. I'll suggest you check it out. (TNet is a multiplayer library for BMax). It'll probably make your life easier.

Sendtoo and ReceiveFrom is just like Receive and Send except you use the ip and port directly without the need of "connecting" the socket first. You can use both for both TCP and UDP. I guess you know that, just in case.

And both UDP and TCP sends the senders IP and Port packed into each message. That's what you retreive with receivefrom. Usually clients ip's stays the same throughout a session but that is not always the case.

UDP and TCP have the same problems when you deal with firewalls, routers and internal ip's. In the actuall world a TCP message travels just like a UDP msg. The difference is when the message arrives or does not arrive. TCP stops traffic until the problem is solved/packet resent, while UDP does nothing. Routers are smart devices, when someone behind a router talk to someone outside it, the router "remembers" the outsider. That's why it can direct the data sent back to you (which is sent to the router) to your computer automatically. That's not a TCP thing. Some routers might however have default settings that blocks/accept TCP/UDP. You only forward ports (on the router) if you want to start a server behind it. I'm pretty sure but correct me if I'm wrong.


Tom(Posted 2005) [#7]
And both UDP and TCP sends the senders IP and Port packed into each message. That's what you retreive with receivefrom. Usually clients ip's stays the same throughout a session but that is not always the case.


I see that the source IP is dependant on whether IPv4+ is used, http://www.zytrax.com/tech/protocols/tcp.html#udp is IPv4 standard for the Internet?

Edit: Ok, wikipedia says 'most' of the Internet uses IPv4 as of 2004 http://en.wikipedia.org/wiki/IPv4 but that's not a guarantee.


Tibit(Posted 2005) [#8]
Wait I was a little wrong there. The port is packed into the message, not the IP. The IP sits "on top" in the IP header.

Do you know what part of the internet that does not use IPv4?