7

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?

Cam
  • 14,930
  • 16
  • 77
  • 128
  • 2
    Out of curiosity only. How did you manage to "know very well Java,C and C++" but never get involved with networking? – Cratylus Oct 28 '10 at 19:36
  • @user384706: By learning the languages' syntax, most of their libraries, and gaining lots of experience with them. I specified that I'm good with those languages to communicate that I don't need beginner tutorials that aim to teach programming AND networking together, or that are geared towards beginner programmers. Like "Now we'll learn the while loop - and then, we'll move on to using the loop to send multiple pieces of data at once" :) – Cam Oct 28 '10 at 19:40
  • 1
    VMs aren't really necessary in my experience... you can simulate multiple machines simply by running multiple processes under a single OS. – Jeremy Friesner Oct 30 '10 at 21:57

13 Answers13

13

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:

ServerSocket server = new ServerSocket(8888); // 8888 is the port the server will listen on.

"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:

Socket connectionToTheServer = new Socket("localhost", 8888); // First param: server-address, Second: the port

But now, there isn't still a connection. The server has to accept the waiting client (as I noticed here above):

Socket connectionToTheClient = server.accept();

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:

OutputStream out = yourSocketHere.getOutputStream();
PrintStream ps = new PrintStream(out, true); // Second param: auto-flush on write = true
ps.println("Hello, Other side of the connection!");
// Now, you don't have to flush it, because of the auto-flush flag we turned on.

A BufferedReader for text-reading is the good (best*) option:

InputStream in = yourSocketHere.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
System.out.println(line); // Prints "Hello, Other side of the connection!", in this example (if this would be the other side of the connection.

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:

InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// The other side says hello:
String text = br.readLine();
// For whatever reason, you want to read one single byte from the stream,
// That single byte, just after the newline:
byte b = (byte) in.read();

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 DataInputStream and 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 use

DataInputStream.readLine()

This 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.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
6

Beej's guide to Network Programming is absolutely resounding. Used it at University.

http://beej.us/guide/bgnet/

It covers the Sockets API and I remember it using C++ for the code examples.

Also Computer Networks by Tannenbaum is an excellent read too.

brumScouse
  • 3,166
  • 1
  • 24
  • 38
  • In fact I have some old client and server code...Its from my Uni days, not the best code ever, but might be helpful too! – brumScouse Oct 28 '10 at 19:18
  • I had a brief look at this (web) book, and while I recognised the content I wouldn't think it the best order for a beginner to learn about IP based client server networking. IMNSHO – Greg Domjan Oct 28 '10 at 19:27
  • Some of the other aspects asked by the OP mentioned may be useful (some simple client and server programs for example) I did have the book mentioned in my answer as a companion though. – brumScouse Oct 28 '10 at 19:32
  • Our class used this exact guide to build our first client/server programs in C. Really useful! – o01 Nov 01 '10 at 11:40
  • @cam somewhere I read the implementation of tcp protocol in c language using structure . – Dead Programmer Nov 06 '10 at 14:50
6
  1. Understand the basic concepts about networking. Layers, IP Addresses, Ports, Packets [ Specifically UDP/TCP ]

  2. Learn programming abstractions about [1], like Sockets.

  3. Implement the "Server" and the "Client" yourself.

  4. Test it.

  5. Install Wireshark on your computer, and look for the IP addresses, Types of packets, the port numbers etc that are sent for each type of activity.

  6. Build on the knowledge by using the Networking APIs of Java/.Net/C++. It's probably a very bad idea to build everything from scratch.

Java: http://download.oracle.com/javase/tutorial/networking/index.html

.Net: http://msdn.microsoft.com/en-us/library/4as0wz7t.aspx

C++: Unix Network Programming by Richard Stevens

halfer
  • 19,824
  • 17
  • 99
  • 186
Sekhar
  • 5,614
  • 9
  • 38
  • 44
4

If you're using C++ I would highly recommend that you look at using Boost.Asio it's a great asynchronous networking library that will have you reaching you're goals a LOT faster than using sockets directly.

Obviously it's a great idea to learn the network stuff from the ground up, but I think that it is highly beneficial to do that with a good (and easy to use) library to back you up so you can get results too.

radman
  • 17,675
  • 11
  • 42
  • 58
3

In my opinion you should start by learning how to use sockets, let's say, in C, UNIX (any online tutorial will be suitable, or use "man"). And only after that you can google for language/OS specific libraries and choose whatever you'd like or whatever would fit better your needs.

LE: You can always test your applications on the same machine.

sdadffdfd
  • 673
  • 1
  • 8
  • 24
3

I couldn't recommend book by Richard Stevens more.

Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51
2

If you know nothing about TCP/IP, I'd start off with Douglas E. Comer's awesome book: TCP/IP Volume 1.

You don't need to read all of it, there's lots of useful stuff in there.

Once you've covered that, look at one of the several open source networking implementations, such as boost asio. IMHO this is the easiest networking library to get up and running. Once this has piqued your interest further, then start to investigate some of the lower level socket details. Btw, brumScouse already mentioned Beej's guide, this is also a very good resource.

And then when you are further along, and you want more: google C10K! ;)

Nim
  • 33,299
  • 2
  • 62
  • 101
  • Would you say Beej's guide falls under the scope of 'lower level socket details'? – Cam Oct 28 '10 at 23:45
  • Let's just say, that guide is about the fundamental sockets api; libraries like asio or ACE or whatever simply wrap the functionality exposed by the sockets api and give you nice abstractions to work with. So in that sense it is slightly "lower level", but at the same time it is very useful knowledge to have so that you know how the abstractions work. The reason I mention the abstractions first is that it hides a lot of implementation detail that you could get lost in... – Nim Oct 29 '10 at 07:33
2

I come from a mostly Linux network programming background. I've done a small bit of Windows socket programming, from what I've seen the Windows socket APIs are pretty similar to the POSIX socket APIs, and there is some pretty good information on MSDN for porting from POSIX to Windows sockets.

Network programming isn't really difficult conceptually, there are a few areas that you'll need to have an understanding of to be effective: First you'll need to have a basic understanding of networking. You'll need to at the least know about TCP and IPv4, since those are likely to be the default protocols that you'll end up using. Understanding a bit about DNS and ethernet at a conceptual level will probably also be useful, but not strictly required at first. Once you have a basic background (reading a few wikipedia articles or a basic guide to networking will probably be enough to get you started at first) you can start looking at the POSIX socket APIs. These are pretty straightforward once you have the networking basics down, and there are plenty of tutorials out there (Beej's network programming guide was listed by someone else, it's pretty good, and the man pages actually have some pretty useful examples as well). The biggest stumbling block you might run into here is knowing what needs to be in network byte order vs. host byte order, but as a rule of thumb anything that you are writing to a socket should be put into network byte order first, and anything you read from the socket should immediately be converted to host byte order.

Where a lot of people stumble is that just learning the POSIX socket API isn't really enough to do effective network programming. It'll get you far enough to write a simple echo client, but to move beyond that there are a few other things you need to be familiar with. The biggest ones are working with non-blocking file descriptors, signals, working with select/epoll/etc., and getting a handle on the child process workflow. At a high level it's pretty conceptually simple (create->bind->listen->select->fork->accept), but if you're not used to working with file descriptors, select, fork, etc. then those are things you'll need to get up to speed on.

One of the best programs to start with, in my opinion, is an IRC client. It will give you all of the basics you'll need (read/writing messages to a server, concurrent connections, dealing with different server implementations, etc.), the protocol is pretty easy to parse, and there are a lot of servers out there to connect to.

Cercerilla
  • 1,353
  • 12
  • 13
2

You may want to read

Sockets programming in Java: A tutorial

on javaworld. It explains the basics. Don't try to go too far in low-level networking initially. Code some simple client/server-s to see how it works.

khachik
  • 28,112
  • 9
  • 59
  • 94
2

Here's something not mentioned yet. Qt

Qt (c++ framework) has a very good "network" class that I've used for TCP and UDP server/client type connections. There are several different examples that cover both:

Blocking connections (connections that will make the current thread 'wait' for a return)

Non-blocking connections (connections that set off call_backs once data is available)

Check it out and you'll never look back. They are very well documented!!!

g19fanatic
  • 10,567
  • 6
  • 33
  • 63
1

Do start with the basics but then you should take a look at what is really used (in Java):

  1. For socket programming use Apache Mina.
  2. Modern client server apps use REST: Jersey, RESTeasy (both JAX-RS complient) and Restlet (pioneer in this field).
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
1

Stevens' UNIX Network Programming has all the information you could wish for on using the BSD sockets API (which is now the standard sockets API) and how various UNIX systems interpret the functions in those APIs. It includes a lot of code demonstrating the topics under discussion. It is also very dense.

Beej's guide is a good, quick summary of how to use the sockets API. It might be one of the better places to start and just get the hang of the mechanics of putting together a server and a client. To understand why you're making those function calls with that data in that order, and what exactly the function calls are doing, you can turn to Stevens.

If you want to pick up best practices, take a look at production code: How does Mongrel 2 handle networking? How about lighttpd? Varnish? Why do they handle it the way they do?

You can also look at the implementation of the networking stack for a platform you're interested in. Grab the kernel source code, possibly a book discussing the kernel's design, and dig in.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
0

IMHO, socket programming in Java is much easier than in C/C++. You can start learning it from Java tutorial: http://download.oracle.com/javase/tutorial/networking/sockets/

For "basic" C/C++ socket programming, you can learn from "Beej's Guide to Network Programming". It is worth to learn when I was in university :). Then, you may learn about Windows Socket API for next starting point.

To emulate the environment, off course you can use VMs :)

anggriawan
  • 234
  • 1
  • 2
  • 7