Lately I've been feeling stupid because I don't know how programs are able to send data across the Internet, how sockets work and what they are, how servers work and how to set them up and interface with them, etc. TLDR I'm brain dead when it comes to anything involving networks, the Internet, or whatever I'm talking about in case I'm using the wrong terms.

Like most kids who ever played WoW, Runescpe, all that mindless lingo, I've always wondered how the "online" aspect works, more specifically. With that curiousity in mind, I was wondering if anyone here could point me in the right direction of learning the core workings behind how things like players interact non-locally? I'd like to learn enough so that I could potentially make program with a small room where two players from different computers, connected through the Internet, could walk around and see the other person move. Not that I have the urge to make an MMO mind you; I feel as if learning these skills could potentially help me with future projects and generally just increase my programming and technical knowledge.

Thanks for any pointers in advance; I'm sure I could find decent tutorials on The Google, but since I have a decent stash of technical vets here, I decided to see what you guys think I should do to go about learning and practicing these skills. Also, sorry in advance for anything above sounding stupid, I know next-to-zero terms regarding this subject, and will hopefully look back upon this post in a few months and laugh Smile

EDIT: Regarding languages, would be preferred if the language shown was Java (I dislike it, but it is very useful today still and I'm gonna be "learning" it this coming year in APCS), Python, Ruby, or something kinda like that, but if it's something like C#, C, or the like I can tough it out too.
I would start by learning how IRC works, it's a very good start. It uses sockets, not sure of what larger networks use, but IRC is pretty cool to learn. I learn it a few weeks ago and it turned out to be very useful.

I used Python to learn it, but sockets can be used in a lot of languages Smile
Sockets are actually pretty straight forward. Give it the ip you want to connect to, and make the connection. The other computer needs to be listening for the connection, and then they shake hands and woo, you can start sending data across to your hearts content. The routers and protocol handle getting it there and (depending on the protocol) data loss and such. Take a look at this:
http://merthsoft.calcg.org/chat.zip
It's a very simple network chat program following the server-client paradigm. It's one of the first networked programs I ever made (it's written in C). Should be a nice start. And, of course, googling "java socket tutorial" will likely give you lots of results.

And, because everyone loves screen shots, it looks like this:
Thanks, that sounds like a good start then, I'll look at that and see what David means about IRC before continuing, once I get on my computer later today Smile
If you're interested in IRC, the client protocol (how IRC clients communicate with servers) can be found here: http://tools.ietf.org/html/rfc2812. You can experiment directly by using PuTTY, creating a raw connection to an IRC server on port 6667.
In Java sockets get a bit messy (but on the other hand that means lots of tutorials!), but in Python it's ridiculously simple: to create a connection, it's as simple as socket.socket().connect().

As ephan said, IRC sockets are really fun (bots!), so you should try that. It's pretty simple, too, since once you set it up right you can just read lines from the socket as if it were a file that constantly gets updated. In Java you probably need to buffer it a few times to make it work well.

Code:
import socket
import string
HOST="irc.prison.net"
PORT=6667
NICK="HelloBot"
IDENT="HelloBot"
REALNAME="Hello Bot"
CHANNEL = "#channelname"
readbuffer=""
s=socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
s.send("JOIN %s\r\n" % CHANNEL)
while 1:
    readbuffer=readbuffer+s.recv(1024)
    temp=string.split(readbuffer, "\n")
    readbuffer=temp.pop( )
    for line in temp:
        line=string.rstrip(line)
        line=string.split(line)
        if(line[0]=="PING"):
            s.send("PONG %s\r\n" % line[1])
        elif(line[1]=="PRIVMSG") and (line[3][0:7] == ":!hello"):
            s.send("PRIVMSG " + CHANNEL + ":Hello\n")


This bot says "Hello", when someone says "!hello".
I do recommend that you don't use irc.prison.net, though, which has a strict no-bots policy:
Quote:
1) NO BOTS are allowed on this server! Bots of any kind are not tolerated (channel, flood, fserv, dcc, ftp, etc).
irc.mzima.net doesn't care.
irc.nac.net is a very nice one, too. I rarely get downtimes on it, and I haven't seen anything about bots (I've also had ~3 connections to it on the same network).
Some servers have messages about bots but they rarely check, so you can probably get by. I guess it's still better to follow the rules though.
calcdude84se wrote:
I do recommend that you don't use irc.prison.net, though, which has a strict no-bots policy:
Quote:
1) NO BOTS are allowed on this server! Bots of any kind are not tolerated (channel, flood, fserv, dcc, ftp, etc).


Thank you I did not know that Smile
Ashbad wrote:
Thanks, that sounds like a good start then, I'll look at that and see what David means about IRC before continuing, once I get on my computer later today Smile
Great. And just so you know, I've set up a server at merthsoft.com port 7654, so you're welcome to play around with the client/protocol using that.

Edit: Except it seems to be freaking out on me, so just highlight me in IRC to get it up...
Why write an IRC client? That's boring. Write your own client/server pair. If you are approaching this from a game perspective, you can use TCP (more reliable), but most games use UDP (lower latency)
Cool, thanks Merth Smile

I read up on sockets, and they seem to be somewhat straightforwa even in Java -- however, I do have 2 questions:

The hostname -- how exactly would you specify it so you can connect to an exact computer over the Internet? The example in the tutorial I read so far just used silly stuff like "JokeServer" and such, but what would I do to connect to something like my home computer? Do I specify it with something similar to a URL, like vie seen before? Sorry, but I'm somewhat confused.

Ports -- easy concept, but just curious, how do you know what ports are being used or not? What is the number range of port ID#s you can use? How many sockets can you connect to a single port? Not needed, but what, more technically, is a port referring to, is it like a hardware port the OS interfaces with, or something totally different?

The tutorial might have answered some of these and I didn't know it, but most of these I still am left not knowing. Thanks in advance for any explanations, and sorry for the n00bishness.

Kllrnohj wrote:
Why write an IRC client? That's boring. Write your own client/server pair. If you are approaching this from a game perspective, you can use TCP (more reliable), but most games use UDP (lower latency)


I just researched both of those somewhat, and I'll be interested to play around with both protocols. Both seem to have fallbacks, but from what i see it makes sense that many games use UDP.
"The hostname -- how exactly would you specify it so you can connect to an exact computer over the Internet? The example in the tutorial I read so far just used silly stuff like "JokeServer" and such, but what would I do to connect to something like my home computer? Do I specify it with something similar to a URL, like vie seen before? Sorry, but I'm somewhat confused. "

I don't think you can just connect to any computer, you need to connect to a server that is prepared for it, but I'm not quite sure, wait for other people to answer.
Ashbad wrote:
The hostname -- how exactly would you specify it so you can connect to an exact computer over the Internet? The example in the tutorial I read so far just used silly stuff like "JokeServer" and such, but what would I do to connect to something like my home computer? Do I specify it with something similar to a URL, like vie seen before? Sorry, but I'm somewhat confused.
I'm fairly certain something like "merthsoft.com" works, or "192.168.0.115". When in doubt, check Wikipedia. Basically, if you want to connect to a computer over the internet, use the domain name or IP address, if you want to connect on your local network, use the network name or internal IP address.

Ibid. wrote:
Ports -- easy concept, but just curious, how do you know what ports are being used or not? What is the number range of port ID#s you can use? How many sockets can you connect to a single port? Not needed, but what, more technically, is a port referring to, is it like a hardware port the OS interfaces with, or something totally different?
See this list for a list of what ports are used for, and this page for a good explanation of what ports are. The short answer is they're a software way for your computer to know which application a packet is going to.
Ashbad wrote:
The hostname -- how exactly would you specify it so you can connect to an exact computer over the Internet? The example in the tutorial I read so far just used silly stuff like "JokeServer" and such, but what would I do to connect to something like my home computer? Do I specify it with something similar to a URL, like vie seen before? Sorry, but I'm somewhat confused.


Hostname is either an IP address or a name that can be resolved to an IP address. Name resolution is a multistep process that varies from OS to OS, but usually it is resolved in the following order:

1) local hostnames file (eg, on my desktop I could map the hostname "my_server" to map to IP 127.0.0.1), usually this file is defined/managed by the OS

2) local network DNS (usually used by corps, but you could also have your own. This would allow you to name your computers on your network - some routers do this automatically, most screw it up)

3) WAN DNS (aka, the internet's DNS) - this would be things like google.com, cemetech.net, etc...

Programs you write won't care how the hostname is resolved, just whether or not it does resolve.

Quote:
how do you know what ports are being used or not?


Pretty much you don't. Your server won't be able to listen on a port someone else is listening on, but your client has no real way of knowing.

Quote:
How many sockets can you connect to a single port?


There is no limit on the number of sockets per port, BUT only one thing can be listening on a port. Basically only one server per port, but that server can have unlimited sockets connect to it (except for OS/hardware limitations, of course)

Quote:
Not needed, but what, more technically, is a port referring to, is it like a hardware port the OS interfaces with, or something totally different?


It's just a number in the packet, nothing more. Think of it as an extension of the IP address.
I'm coming in a bit late on this topic, but Merthsoft, nice program. Smile I too made an ncurses-based multi-user C++ socket chat server/client program back in the day (about four years ago). I should publish it at some point as a nice little sockets demo.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» 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
You cannot vote in polls in this forum

 

Advertisement