I’m a good programmer, but I have zero network experience.
Basically, I’d like to get into client-server networking. For example, I’d like to try getting a server process going which allows clients to connect over the internet and send pings to all of the other connected clients. Then maybe I’ll try developing a simple chat client, or some simple multiplayer game and I’ll go from there.
Languages I know very well that might be useful: Java, C++, C.
How do I get started? I want to learn best-practices up front, so good learning resources you can recommend (eg books, online materials, etc) would be great.
Edit: Should I also look into some kind of VM to emulate various machines interacting with each other?
Edit 2: I’ve put up a 50-rep bounty. Some great answers have been put up so far – I’m looking for more detailed answers though, so hopefully this will encourage that. For example an answer by someone with experience in this type of stuff that compares different learning approaches would be really helpful. Thanks! Also could I get some feedback on the whole VM thing?
I prefer Java. I’m going to explain TCP:
The basic concept is that you have to run a “Server” on a machine. That server accepts clients waiting for a connection. Each connection goes over a port (you know, I hope…).
Always use ports above 1024 because ports lower than 1025 are most of the time reserved for standard protocols (like HTTP (80), FTP (21), Telnet, …)
However, creating a Server in Java is done this way:
“Socket” is the word you are probably looking for if you want to do research.
And to connect your client to a server you have to write this:
But now, there isn’t still a connection. The server has to accept the waiting client (as I noticed here above):
Done! Your connection is established! Communicating is just like File-IO. The only thing you have to keep in mind is that you have to decide when you want to flush the buffer and really send the data through the socket.
Using a PrintStream for text-writing is very handy:
A BufferedReader for text-reading is the good (best*) option:
Hopefully you can start with networking with this information!
PS: Of course, all networking code have to be try-catched for IOExceptions.
EDIT: I forgot to write why it isn’t always the best option. A BufferedReader uses a buffer and read as much as it can into the buffer. But sometimes you don’t want that the BufferedReader steals the bytes after the newline and put them into his own buffer.
Short example:
But the BufferedReader has already that byte, you want to read, in his buffer. So calling
in.read()will return the byte following on the last byte in the buffer of the reader.So, in this situation the best solution is to use
DataInputStreamand manage it your own way to know how long the string will be and read only that number of bytes and convert them into a string. Or: You useThis method doesn’t use a buffer and reads byte by byte and checks for a newline. So this method doesn’t steal the bytes from the underlying InputStream.