TCP/IP

BlitzMax Forums/BlitzMax Programming/TCP/IP

Adam Novagen(Posted 2013) [#1]
So I've been out of the coding loop for a while now, and I've suddenly been hit by "the itch" again. I'd like to take this opportunity of enthusiasm to pick up something I've wanted to for some time: TCP/IP protocols. I figure I'll start simple, see if I can get some messages to and from a friend of mine via command line or something.

I'd like to get into this good and proper, but have no idea where to start. Any suggestions on what I should read (or do) on this subject, both in total and specifically regarding BlitzMax?


BlitzSupport(Posted 2013) [#2]
Here's a quick intro, with a simple server and a simple client. Most servers would use multiple threads (to handle multiple clients), but in this instance, for simplicity, we can only deal with a single client.

Here's the server -- open MaxIDE and run it first:



Now open a separate instance of MaxIDE -- I would suggest opening the server instance on the left of your screen and the client on the right -- and run the client code, below. (The server MUST be running or the client will simply exit.)



Once connected, type into the client and hit Enter to send a message. The server will receive it and print the received message.

Hopefully the comments will explain what's going on!

To talk to an actual web server (we're effectively using our own protocol here), you need to read up on what the HTTP server is expecting to come in and what the client needs to send it. To find protocol information, Google "RFC" along with the protocol you're interested in -- in this case "HTTP".

Also make use of the Code Archives here, under the Networking section. If you hit my profile and then "Code archive entries" you'll find examples of clients and servers of various types. These are generally multithreaded, which complicates things, so use the above to get a grasp of the basics of communicating between clients and server first.

Finally, this example uses IP address 127.0.0.1 (which is always the local PC), so expects you to run the server and client on the same PC in separate MaxIDE instances. If you have more than one PC on a LAN, you can run the server on one PC and the client on the other, changing the IP in the client code from 127.0.0.1 to that of the PC which is running the server and it should work.


BlitzSupport(Posted 2013) [#3]
By the way, if you run the server, as it's listening on the standard web port 80, you can type its IP address (127.0.0.1) into a web browser instead of running the client and you'll see what the browser initially sends to the server. (However, it will then get 'stuck' and fail, since we're not following the back-and-forth protocol for communications between web browsers and web servers, so you'll just have to terminate the server at that point!)


Adam Novagen(Posted 2013) [#4]
Looks like a good start. Something I forgot to ask: I've noticed that some games (Minecraft and Terraria as examples) require port forwarding to enable online play, whereas mainstream games - naturally - do not. What is it that makes some games require the player to manually open ports on their router, but not other games and programs?


BlitzSupport(Posted 2013) [#5]
I'm not an expert on this stuff, but I think it mainly depends on whether or not the game uses a central server or one (or more) of the clients is working as server (or has some server capabilities).

Port forwarding is usually needed when an external client needs to connect to a computer on your local network (behind your router) which is acting as a host/server in some capacity -- if it does any listening for incoming connections from other clients. (There's nothing to stop a client program implementing some server capabilities.)

It's really just a case of opening a port on your router to allow incoming connections to the server application running on your local network; most ports would be blocked by default. Port forwarding is a PITA because you can't realistically do it programmatically -- there must be thousands of different router configuration interfaces. I think UPnP handles this, but of course not all routers implement it.

I believe that if all clients go through a central server (eg. a game connects to a server operated by a web hosting company and ALL communications between clients go through that) -- as you'd get in most mainstream games -- then port forwarding would not be required.


BlitzSupport(Posted 2013) [#6]
There's also "hole-punching", a type of NAT Traversal (communication between separate LANs, usually over the internet), which I gather is a sort of trick whereby a central server initially receives two or more clients, then makes them think they're talking to the central server, when in fact they're talking to each other. Bit out of my league at the moment, though...