There really is not a lot of material on this subject (I believe) that explains the use of Windows sockets sufficiently enough for a beginner to understand and begin to program them. I still remember the hassle that I went through trying to find a proper tutorial that didn't leave me hanging with many questions after I started programming with them myself.
That was a long time ago now, and it was quite a challenge for me to program my first application that could communicate with other computers over the Internet – even though my first introduction to sockets was through Visual Basic; a high-level and very user-friendly programming language. Now that I have long since switched to the more powerful C++, I rapidly found that the labor I had expended to code sockets in VB was nothing compared to what awaited!
Thankfully, after many hours searching various web pages on the Internet, I was able to collect all the bits and pieces, and finally compile my first telnet program in C++. My goal is to collect all the necessary data in one place; right here, so the reader doesn't have to recollect all the data over the Internet. Thus, I present this tutorial in hopes that it alone will be sufficient information to begin programming.
Before we begin, you will need to
include winsock.h and link libws2_32.a to your project in order to use the API that are necessary for TCP/IP. If this is not possible, use
LoadLibrary() to load ws2_32.dll at runtime, or some similar method.
All the code in this article was written and tested using "Bloodshed Dev-C++ 22.214.171.124"; but generally, it should work with any compiler with minimal modifications.
What the Heck are Threads, Ports, and Sockets?
Actually, we can use the word-picture presented to us by the name "socket" in a similar fashion to illustrate what they are and how they work. In an actual mechanical socket, you may recall that it is the female, or "receiving" end of a connection. A "thread" is a symbolic name for a connection between your computer and a remote computer, and a thread is connected to a socket.
In case I've lost you with all that proper terminology, you might think of a thread as an actual, physical, sewing-type thread stretched from one computer to the other, as the common analogy goes. In order for the threads to be attached to each computer, however, there must be a receiving object that attaches to the threads, and these are called sockets.
A socket can be opened on any "port"; which is simply a unique number to distinguish it from other threads, because more than just one connection can be made on the same computer. A few of these ports have been set aside to serve a specific purpose. Beyond these ports, there are quite a large number of other ports that can be used for anything and everything: over 6,000, actually. A few commonly used ports are listed below with their corresponding services:
|25||SMTP (Send mail)|
|43||Whois (Query information)|
|79||Finger (Query server information)|
|80||HTTP (Web pages)|
|110||POP (Receive mail)|
|513||CLOGIN (Used for IP spoofing)|
There are many more ports used for specific purposes that are not shown here. Typically though, if you wish to use a port that has no specific assigned service, any port from 1,000 to 6,535 should be just fine. Of course, if instead you want to listen in on messages sent to and from service ports, you can do that too.
Are you connected to the Internet now? Let's say you are, and you have Internet Explorer or some other web page service running, as well as AOL or some other chat program. On top of that (as if the connection wasn't slow enough already) you're trying to send and receive email. What ports do you think are opened, sending and receiving data?
- Internet Explorer (etc.) sends and receives data via port 80
- AOL and other instant messaging programs usually like to hang out in the higher unassigned ports up in the thousands to be safe from interference. Each chat program varies, as there is no specific "chat" service and multiple messaging programs may run at the same time
- When you're sending your email, you and the remote mail server are communicating using port 25
- And, when you receive email, your mail client (such as Microsoft Outlook) uses port 110 to retrieve your mail from the mail server
And onward extends the list.
It's not enough just to know what port number we're using, obviously; we need to know what remote computer/server we're connecting to. Just like we find out the home address of the people we visit before we get in the car, we have to know the "IP address" of the host we are connecting to, if we are connecting and not just listening (a chat program needs to be able to do both).
An IP address is an identification number that is assigned to each computer on the network, and consists of four sets of digits separated by periods. You can view your IP address by running ipconfig.exe at the MSDOS prompt.
For the examples shown throughout this tutorial, we will be using what is called the "loop-back address" to test our chat program without being connected to the Internet. This address is 127.0.0.1. Whenever you try to make a connection to this IP, the computer loops the request back to you computer and attempts to locate a server on the specified port. That way, you can have the server and client running on the same computer. Once you decide to connect to other remote computers, and you've worked the bugs out of your chat program, you will need to get the unique IP address of each to communicate with them over the Internet.
Because we as humans are very capable of forgetting things, and because we couldn't possibly hope to remember a bunch of numbers for every web site we visit, some smart individuals came up the wonderful idea of "domain names". Now, we have neat little names like www.yahoo.com and www.cia.gov that stand for IP addresses that are much easier to remember than clunky sets of digits. When you type one of these names in your browser window, the IP address for that domain name is looked up via a "router", and once it is obtained (or the host is "resolved"), the browser can contact the server residing at that address.
For example, let's say I call an operator because I can't remember my girlfriend's phone number (fat chance). So, I just tell the operator what her name is (and a few other details, but that's not important) and she happily gives me the digits. That's kind of what happens when a request is made for an IP address of any domain name.
We have two API that accomplish this task. It's a good idea to make sure and check to see if whoever uses you program types a domain name instead of an IP address, so your program can look up the correct IP address before continuing. Most people, anyway, won't want to remember any IP addresses, so most likely you'll need to translate domain names into IP addresses before you can establish a connection – which requires that the computer must be connected to the Internet. Then, once you have the address, you're all set to connect.
DECLARE_STDCALL_P(struct hostent *) gethostbyname(const char*);
unsigned long PASCAL inet_addr(const char*);
Just when you thought all this thread-socket stuff was going to be simple and easy, we have to start discussing byte order. This is because Intel computers and network protocols use reversed byte ordering from each other, and we have to covert each port and IP address to network byte order before we send it; else we'll have a big mix up. Port 25, when not reversed, will not end up being port 25 at all. So, we have to make sure we're speaking the same language as the server when we attempt to communicate with it.
Thankfully, we don't have to code all the conversion functions manually; as Microsoft kindly provides us with a few API to do this as well. The four functions that are used to change the byte order of an IP or port number are as follows:
u_long PASCAL htonl(u_long);
u_long PASCAL ntohl(u_long);
u_short PASCAL htons(u_short);
u_short PASCAL ntohs(u_short);
Remember! The "host" computer is the computer that listens for and invites connections to it, and the "network" computer is the visitor that connects to the host.
So, for example, before we specify which port we are going to listen on or connect to, we'll have to use the
htons() function to convert the number to network byte order. Note that after using
inet_addr() to convert a string IP address to the required form, we will be returned the address in the correct network order, eliminating the need to evoke
htonl(). An easy way to differentiate between
htonl() is to think of the port number as the shorter number, and the IP as the longer number (which is true – an IP address consists of four sets of up to three digits separated by periods, versus a single port number).
Firing Up Winsock
OK, now that we've finally covered the basics, hopefully you are starting to see light at the end of the tunnel and we can move on. Don't worry if you don't understand every aspect of the procedure, for many supplementary facts will be brought to light as we progress.
The first step to programming with windows sockets (A.K.A "Winsock") is starting up the Winsock API. There are two versions of Winsock; version one is the older, limited version; and version 2 is the latest edition and is therefore the version we prefer to specify.
#define SCK_VERSION1 0x0101
#define SCK_VERSION2 0x0202
int PASCAL WSAStartup(WORD,LPWSADATA);
int PASCAL WSACleanup(void);
typedef struct WSAData
unsigned short iMaxSockets;
unsigned short iMaxUdpDg;
char * lpVendorInfo;
typedef WSADATA *LPWSADATA;
You should only need to call these functions once each, the former when you initialize Winsock, and the latter when you are finished. Don't close down Winsock until you are finished, though, as doing so would cancel any connections that your program has initiated or any ports that you are listening on.
Initializing a Socket
We understand how sockets work now, hopefully, but up until now we had no idea how to initialize them. The correct parameters must be filled out and passed to a handy API call that begins the socket (hopefully). In this case, we are returned the handle to the socket that we have created. This handle is very "handy" and we must keep it on hand to manipulate the socket's activity.
When you are all finished doing your dirty work, it is considered proper programming practice to shut down any sockets that you have opened before your program exits. Of course, when it does, all the ties and connections it has will be forcibly shut down, including any sockets, but it's better to shut them down the graceful way with
closesocket(). You will need to pass the socket's handle to this API when you call it.
#define SOCK_STREAM 1
#define SOCK_DGRAM 2
#define SOCK_RAW 3
#define AF_INET 2
#define IPPROTO_TCP 6
SOCKET PASCAL socket(int,int,int);
int PASCAL closesocket(SOCKET);
When creating a socket, you will need to pass the "address family", socket "type", and the "protocol type". Unless you're doing some special (or odd) work, which is beyond the scope of this report, you should typically just pass
AF_INET as the default address family. This parameter specifies how the computer addresses will be interpreted.
There is more than just one type of socket; actually, there are many more. Three of the most common ones include: Raw Sockets, Stream Sockets, and Datagram Sockets. Stream sockets, however, are what we are using in this tutorial, since we are dealing with TCP protocols, so we will specify
SOCK_STREAM as the second parameter to
We're close, so close! We've got the "nitty gritty" stuff done and over with, so let's move on the more exiting parts of Winsock programming.
Connecting to a Remote Host (Acting as the Client)
Let's try out what we've gone over with a simple program that can connect to a remote computer. Doing this will help you to understand much better how everything works, and helps to prevent information overload!
You'll need to fill out information about the remote host that you are connecting to, and then pass a pointer to this structure to the magic function,
connect(). This structure and the API are listed below. Note that the
sin_zero parameter is unneeded and is thus left blank.
struct in_addr sin_addr;
int PASCAL connect(SOCKET,const struct sockaddr*,int);
I highly recommend that you type in all of the examples in this report by hand, instead of copying and pasting it into your compiler. While I know that doing so will slow you up, I am confident and know from experience that you will learn the process much better that way than if you copy and paste the code.
bool ConnectToHost(int PortNo, char* IPAddress)
int error = WSAStartup(0x0202, &wsadata);
If (wssadata.wVersion != 0x0202)
target.sin_family = AF_INET;
target.sin_port = htons (PortNo);
target.sin_addr.s_addr = inet_addr (IPAddress);
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
void CloseConnection ()
Before you move on, type this code up and give it a try.
Receiving Connections – Acting as a Server
Now that you've had a feel for what it's like to connect to a remote computer, it's time to play the "server" role; so remote computers can connect to you. To do this, we can "listen" on any port and await an incoming connection. As always, we use a few handy API calls:
int PASCAL bind(SOCKET,const struct sockaddr*,int);
int PASCAL listen(SOCKET,int);
SOCKET PASCAL accept(SOCKET,struct sockaddr*,int*);
When you act as the server, you can receive requests for a connection on the port you are listening on: say, for example, a remote computer wants to chat with your computer, it will first ask your server whether or not it wants to establish a connection. In order for a connection to be made, your server must
accept() the connection request. Note that the "server" decides whether or not to establish the connection. Finally, both computers are connected and can exchange data.
listen() function is the easiest way to listen on a port and act as the server, it is not the most desirable. You will quickly find out when you attempt it that your program will freeze until an incoming connection is made, because
listen() is a "blocking" function – it can only perform one task at a time, and will not return until a connection is pending.
This is definitely a problem, but there are a few solutions for it. First, if you are familiar with multi-threaded applications (note that we are not talking about TCP threads here), then you can place the server code on a separate thread that, when started, will not freeze the entire program and the efficiency of the parent program will thus not be impeded. This is really more of a pain that it needs to be; as you could just replace the
listen() function with "asynchronous" sockets. If I've caught your attention with that important-sounding name, you can skip ahead to the next section if you like, but I recommend that you stick with me here and learn the fundamentals. We'll spiff up our code later; but for now, let's focus on the bare essentials.
Before you can even think about listening on a port, you must:
- Initialize Winsock (we discussed this before, remember)
- Start up a socket and make sure it returns a nonzero value, which signifies success and is the handle to the socket
- Fill out the
SOCKADDR_IN structure with the necessary data, including the address family, port, and IP address.
bind() to bind the socket to a specific IP address (if you specified inet_addr("0.0.0.0") or
htonl(INADDR_ANY) as the
sin_addr section of
SOCKADDR_IN, you can bind to any IP address)
At this point, if all has gone according to plan, you're all set to call
listen() and spy to your heart's content.
The first parameter of
listen() must be the handle to a socket that you have previously initialized. Of course, whatever port this socket is attached to is the port that you will be listening on. You can then specify, with the next and final parameter, how many remote computers can communicate with your server at the same time. Generally, however, unless you want to exclude all but one or a few connections, we just pass
SOMAXCONN (SOcket MAX CONNection) as the final parameter to
listen(). If the socket is up and working fine, all should go well, and when a connection request received,
listen() will return. This is your clue to call
accept(), if you wish to establish a connection.
int ListenOnPort(int portno)
int error = WSAStartup (0x0202, &w);
if (w.wVersion != 0x0202)
addr.sin_family = AF_INET;
addr.sin_port = htons (portno);
addr.sin_addr.s_addr = htonl (INADDR_ANY);
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
If you compile and run this code, as mentioned before, your program will freeze until a connection request is made. You could cause this connection request by, for example, trying a "telnet" connection. The connection will inevitably fail, or course, because the connection will not be accepted, but you will cause
listen() to return and your program will resurrect from the land of the dead. You can try this by typing telnet 127.0.0.1 "port_number" at the MSDOS command prompt (replace "port_number" with the port that your server is listening on).
Because using blocking functions such as
listen() is so impractical and such a pain, let's go ahead and before we move on discuss "asynchronous" "sockets". I mentioned these earlier on, and promised you I'd show you how they work.
C++ gives us an advantage here that most high-level programming languages do not; namely, because we don't have to go to the extra length of "sub-classing" the parent window before we can use asynchronous sockets. It's already done for us, so all we really have to do is add the handling code into the message handler. This is because asynchronous sockets, as you will see, depend on being able to send your program messages when a connection request is made, data is being received, etc. This enables it to wait silently in the background without disturbing your parent program or impeding productivity, as it only communicates when necessary. There is a relatively small price to pay, too, for it really doesn't take much additional coding. Understanding how it all works might take a little while, but you'll definitely be pleased that you took the time to understand asynchronous sockets. It'll save you a lot of trouble in the long run.
Instead of having to rework and modify all the code that we have written up to this point, making a socket asynchronous simply requires an additional line of code after the
listen() function. Of course, your message handler needs to be ready to accept the following messages:
FD_ACCEPT: If your application is acting as the client (i.e., you are attempting to connect to a remote host using
connect()), you will receive this message when a connection request is being made. Should you choose to do so, the following message will be sent:
FD_CONNECT: Signifies that a connection has been successfully made
FD_READ: We've got incoming data from the remote computer. We'll learn how to deal with this later on.
FD_CLOSE: The remote host disconnected, so we lost the connection.
These values will be sent in the
lParam parameter of your message handler. I'll show you exactly where to put them in a minute; but first, we need to understand the parameters of the API call we'll be using to set our socket to asynchronous mode:
int PASCAL WSAAsyncSelect(SOCKET,HWND,u_int,long);
The first parameter, obviously, asks for a handle to our socket, and the second requires the handle to our parent window. This is necessary so that it send the messages to the correct window! The third parameter, as you can see, accepts an integer value, for which you will specify a unique notification number. When any message is sent to your program's message handler, whatever number you specify here will also be sent. Thus, you would code your message handler to wait for the identification number, and then determine what type of notification has been sent. I know this is confusing, so hopefully a glance at the following source code will shed a little light on the subject:
#define MY_MESSAGE_NOTIFICATION 1048 //Custom notification message
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hwnd, message, wParam, lParam);
That's not too bad, is it? Now that our handler is all set, we should append the following line of code to function
WSAAsyncSelect (s, hwnd, MY_MESSAGE_NOTIFICATION, (FD_ACCEPT | FD_CONNECT |
FD_READ | FD_CLOSE);
C:\Documents and Settings\Cam>netstat -an
|Proto ||Local Address||Foreign Address||State |
|TCP ||0.0.0.0:135 ||0.0.0.0:0 ||LISTENING|
|TCP ||0.0.0.0:445 ||0.0.0.0:0 ||LISTENING|
|TCP ||0.0.0.0:5225 ||0.0.0.0:0 ||LISTENING|
|TCP ||0.0.0.0:5226 ||0.0.0.0:0 ||LISTENING|
|TCP ||0.0.0.0:8008 ||0.0.0.0:0 ||LISTENING|
|TCP ||127.0.0.1:1025 ||0.0.0.0:0 ||LISTENING|
|TCP ||127.0.0.1:1035 ||127.0.0.1:5226 ||ESTABLISHED|
|TCP ||127.0.0.1:5226 ||127.0.0.1:1035 ||ESTABLISHED|
|TCP ||127.0.0.1:8005 ||0.0.0.0:0 ||LISTENING|
|UDP ||0.0.0.0:445 ||*:*|
|UDP ||0.0.0.0:500 ||*:*|
|UDP ||0.0.0.0:4500 ||*:*|
|UDP ||127.0.0.1:123 ||*:*|
|UDP ||127.0.0.1:1031 ||*:*|
|UDP ||127.0.0.1:1032 ||*:*|
|UDP ||127.0.0.1:1900 ||*:*|
C:\Documents and Settings\Cam>
If your server is working correctly, you should see under "Local Address" something like, "0.0.0.0:Port#," where Port# is the port that you are listening on, in a LISTENING state. Incidentally, if you forget to use
htons() to convert the port number, you might find a new port has been opened, but it will be on a completely different port than what you expected.
Don't worry if it takes you a couple of tries to get everything working right; it happens to all of us. You'll get it with a couple of tries. (Of course, if you try without avail for a couple weeks, burn this report and forget who wrote it!)
Sending and Receiving Data
Up to this section, all you've got for a server is a deaf mute! Which, not surprisingly, does not do you a lot of good in the real world. So, let's take a look at how we can communicate properly and effectively with any computer that decided to chat with us. As always, a few API calls come to the rescue when we're stumped:
int PASCAL send(SOCKET,const char*,int,int);
int PASCAL recv(SOCKET,char*,int,int);
int PASCAL sendto(SOCKET,const char*,int,int,const struct sockaddr*,int);
int PASCAL recvfrom(SOCKET,char*,int,int,struct sockaddr*,int*);
If you're not using an asynchronous server, then you'll have to put the
recv() function in a timer function, that constantly checks for incoming data – not so elegant of a solution, to say the least. If, on the other hand, you've done the smart thing and set up an asynchronous server, then all you have to do is put your
recv() code inside
FD_READ in your message handler. When there's incoming data, you'll be notified. Can't get any easier than that!
When we do detect activity, a buffer must be created to hold it, and then a pointer to the buffer passed to
recv(). After the function returns, the text should have been dutifully placed in our buffer just itching to be displayed. Check out the source code:
memset(buffer, 0, sizeof(buffer));
recv (s, buffer, sizeof(buffer)-1, 0);
Now that you can receive incoming text from the remote computer or server, all that our server lacks is the ability to reply, or "send" data to the remote computer. This is probably the most simple and self-evident process in Winsock programming, but if you're like me and like to have every step spelled out for you, here's how to use
send(s, szpText, len_of_text, 0);
For brevity's sake, the above snippet of code is just a skeleton to give you a general idea of how
send() is used. To see the entire code, please download the example source code that comes along with this tutorial.
On a more advanced note, sometimes the simple
receive() functions just aren't enough to do what you want. This happens when you have multiple connections at the same time from different sources (remember when we called
listen(), we passed
SOMAXCONN to allow the maximum number of connections possible), and you need to send data to one particular computer, and not all of them. If you're uncommonly sharp, you may have noticed two extra API below
receive() (extra credit if you did!);
These two API allow you to communicate with any one remote computer without tipping your hand to everyone else that is connected. There is an extra parameter that accepts a pointer to a
sockaddr_in structure in these advanced functions, which you can use to specify the IP address of any remote computer that you want to communicate with exclusively. This is an important skill to know if you are building a full-fledged chat program, or something similar, but beyond giving you the basic idea of how these functions work, I'll let you figure them out on your own. (Don't you hate it when authors say that? Usually it's because we don't have the slightest clue ourselves … but really, it shouldn't take much to implement them if you decide that you need to.)
Some Final Notes
Well, by now you should have a decent understanding of Windows sockets – or a profound hatred of them – but at any rate, if you're looking for a much better explanation than I can give you here, please take a look at the example source code provided with this article. Practice will do much more for you than reading any article.
Additionally, I have found that if you try to copy and paste code, or just compiling someone else's code you found on the Internet, you won't come close to the level of understanding you will gain if you type in all the examples by hand yourself. A big pain, I know! But if you take the time to do it, you'll save yourself a lot of trouble in the long run.
Have fun, and let me know what you think of this article by posting feedback.
This article (not including the accompanying source code) is copyrighted © 2006 by the author, and cannot be modified, sold, and redistributed for personal gain without prior explicit permission from him. It is provided free of charge for the benefit of the public. You are allowed, however, to make and distribute as many copies of it as you like, provided that you do not modify the original content in any way. Thanks!