This article is my understanding of how to use the WinSock API. It explains how to create a socket, listen on a socket as a server, connect to a socket as a client, and how to pass information from the client to the server.
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Untitled Document
First and foremost in order to use the Winsock API you have to link to the
libraries mpr.lib and wsock32.lib. To do this in Visual Studio create a new
project then under the "Projects" menu choose "Settings...", or just hit Alt+F7.
In the top left of the dialog box there is a drop down list box labeled "Settings
For:" change it to read "All Configurations". In the tab control on the right
of the dialog box select the "Link" tab. In the middle of the tab there is an
edit box labeled "Object/Library Modules:" add the name of the libraries you
want to link to, be sure all the labraries in the list are separated by spaces.
That being done you can now begin to program.
The first step in using the WinSock API is to initialize WSA. I'm not positive
what WSA is, I'm assumng its short for WinSockApi, but I can't back that up.
Whatever it is it has to be initilized. This is done by calling WSAStartup().
This function takes two parameters a version number in a WORD value and a WSADATA
structure, it returns an integer the return will be 0 if initialization is successful.
Here is an example of the initialization process:
For the version number I use the macro MAKEWORD(). It splits the version number
up and its easy to see what you are requesting. When you send that version number
you are requesting a specific version of WinSock, in the example I am requesting
version 1.1. You can request version 1.0, 1.1, and 2.0, version 2.0 is not available
in Win 95 without being specifically installed it does exist in all later versions
of Windows. The exact benifits of each version I'll leave to you to research,
from what I have read version 1.1 has all the important features and since its
available in all version of Windows without a patch it is acceptable for most
applications.
After you have initialized WinSock the next step is to create a socket. Sockets
are of two types stream sockets and datagram sockets. Stream sockets are easier
to use so I'll demonstrate them. All sockets are of type SOCKET, and you create
them with the socket() function. The socket() function takes three parameters.
The first is the type of connection you would like to use, for this use AF_INET
this designates you want to use an Internet style connection (or in other words
use TCP/IP) as far as I know this is the only connection permitted through WinSock.
The second parameter is the type of socket to use, for stream sockets use SOCK_STREAM,
or for datagram sockets use SOCK_DGRAM. The thrid parameter is some value for
the protocol from what I have read this value has very little meaning and is
usually ignored so I always pass zero here. The socket() function will return
the socket or INVALID_SOCKET if it can't create the socket. Here is an example
of that:
Now we have a usable socket, what we need to do is make use of it. As with
any network connections you have to have a server and a client. For clarity
I'm going to call the server the computer that is listening for and incoming
connection and the client the computer that requests a connection with a server.
Since the server has to be listening before a client can connect I'll show how
to setup the server first. First we bind the socket to a TCP/IP port. This is
done with the bind() function. The bind() function takes three parameters, a
socket to bind to, a pointer to a data structure that has the port information
(structure type STRUCTADDR), and the size of the structure with the port information.
There are a few points of interest in this process so i'll just explain inside
an example.
//The variables we will need SOCKADDR_IN SockAddr;
//We need a socket variable but for now lets assume its the variable Socket
we prepared before.
//bind() does require one of those prepared sockets, so at one point you will
need to create one.
/* For those who are paying attention you may have noticed that I said before
that we need a struct SOCKADDR variable. Except I didn't declare one here. The
reason is that struct SOCKADD_IN holds the same information in the same way
as struct SOCKADDR does, the difference is that struct SOCKADDR_IN is easier
to work with. */
//We want to use port 50
SockAddr.sin_port = 50;
//We want an internet type connection (TCP/IP)
SockAddr.sin_family = AF_INET;
//We want to listen on IP address 127.0.0.1
//I'll give a few better ways to set this value later
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 1;
//Ok all the information is set, lets bind()
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
printf("Attempt to bind failed.");
}
That ought to be fairly straight forward to figure out. The connection type
should always be AF_INET, the port is an unsigned integer between 0 and 65,565,
and the address is four unsigned short values from 0 to 255 that is a valid
IP address of the server. We can specify the IP address we want to listen to,
what if we want to listen on multiple addresses? You could run throuh this process
multiple times to bind a socket on each address, or you could set the SockAddr.sin_addr.S_un.S_addr
to INADDR_ANY like this:
SockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
Instead of setting the four octets of an IP address. The next issue that comes
up would be how do I know my IP address? There is a way of finding the address,
but its a little involved so I'm going to discuss that later. Now that we have
a valid socket bound to a TCP/IP port we need to listen on that socket for incoming
connections. We use the listen() function to accomplish that. The listen() function
takes two parameters a bound socket and the number of connections to accept.
Here is how that looks:
//Once again we're carrying through the Socket variable from the previous
example.
//We're only going to accept 1 incoming connection.
listen(Socket, 1);
Not much to listen(). Just to clarify the listen() function does not accept
the incoming connections, it just sets your socket to listening on the specified
port, no more no less. To accept the incoming connection you use accept(). The
accept() function will will watch the port for a breif time then return an error.
So unless you know exactly when the connection is coming and can start accept
at just the right time you are going to miss the connection. One way around
this is to place accept() in a while loop until a connection is received. There
is a problem with this technique, in a DOS or console application its fine since
nothing else can be happening it doesn't matter, but in a windows program it
will stop responding until it gets out of that loop. You may be able to set
the accept() function to run on a short timer or in a loop that is called in
a thread. At any rate here is how it would look if it were in a while loop until
it received a connection:
//We are still carrying through the Socket variable from before
SOCKET TempSock = SOCKET_ERROR;
while (TempSock == SOCKET_ERROR)
{
TempSock = accept(Socket, NULL, NULL);
}
Socket = TempSock;
The reason for creating the TempSock variable is to preserve our real socket.
I don't want to overwrite it with an error just because we missed a connection.
I never looked into what is returned on a successful connection, I would assume
it is the socket you started with, but from examples I looked at it doesn't
appear to do that. All the documentation I read on accept() skipped over the
return value, they just copied the results back into the original socket so
I am doing the same. The second two parameters can be used to gain information
on who connected by passing a pointer to a SOCKADDR structure and its size like
this:
I never tested sending a SOCKADDR_IN the same as in bind() but I haven't tested
this so I won't gaurentee the results of this.
So now we are listening on a TCP/IP port and ready to accept a connection. So
lets look into requesting a connection. To do this we use the connect() function.
This function takes the same parameters as the bind() function except the port
and address are the ones you want to connect to instead of listen on obviously.
The connect() function will return a 0 if successful. Here is an example of
that:
//The variables we will need
SOCKADDR_IN SockAddr;
//We need a socket variable but for now lets assume its a variable Socket we
prepared earlier.
//We want to use port 50
SockAddr.sin_port = 50;
//We want an internet type connection (TCP/IP)
SockAddr.sin_family = AF_INET;
//We want to connect to the IP address 127.0.0.1
//I'll give a few better ways to set this value later
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 1;
if (connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) != 0)
{
printf("Failed to establish connection with server.");
}
Now that we have a server with a connected client they need to exchange information.
This is done exactly the same for the client as it is for the server. The functions
to use are send() and recv(). They both take four parameters the socket to send
on, the data to send, and the number of bytes in the data. The way they expect
the data is in a pointer to a char. You can bundle other values into this just
typecast it into a char * and pass the correct number of bytes. The fourth parameter
isn't used so give a zero there. These functions will return the number of bytes
send or received if successful. They will return 0, WSAECONNRESET, or WSAECONNABORT
if the connection was closed at the other end. These functions will also return
SOCKET_ERROR if some error occurs during the transmission. They recv() function,
like the accept() function, only watches for a brief period for the data to
come through. Once again I place the function in a while loop until data is
received. Here is how the recv() function looks in such a loop:
int RetVal = SOCKET_ERROR;
char String[50];
while (RetVal == SOCKET_ERROR)
{
RetVal = recv(Socket, String, 50, 0);
if ((RetVal == 0)||(RetVal == WSAECONNRESET)||(RetVal
== WSAECONNABORT))
{
printf("Connection
closed at other end.");
break;
}
}
Since errors are possible in sending the data I place it in a while loop as
well. Here is how that looks:
int RetVal = SOCKET_ERROR;
char String[] = "Hello";
while (RetVal == SOCKET_ERROR)
{
RetVal = recv(Socket, String, strlen(String)
+ 1, 0);
if ((RetVal == 0)||(RetVal == WSAECONNRESET)||(RetVal
== WSAECONNABORT))
{
printf("Connection
closed at other end.");
break;
}
}
In these examples the data to send, or the received data is in the character
array String. When the data is received there is a fixed amount of data that
can be received so it is possible to overrun the buffer. That is a quick run
through of how to use WinSock for network communications.
Now as I said before there are ways of determining your own network address.
This is by calling gethostname(). This will not return your IP address, only
the text computer name. This function takes two parameters a character array
to place the computer name in and the number of characters you have allocated
in that array. Here is how it looks:
char Name[255];
gethostname(Name, 255);
If you look at the example above you'll note that it uses the IP address, not
the computer name. What you can do is to call gethostbyname() which will give
you information about a host based on its name. It takes only one parameter,
the string that has the computer name, and it returns a pointer to a HOSTENT
structure. Here is an example:
HOSTENT *HostInfo;
HostInfo = gethostbyname("computer");
if (HostInfo == NULL)
{
printf("Attempt to retreive computer information
failed.");
}
The gethostbyname() function will search through DNS records in order to find
the IP address. The careful readers will note that this HOSTENT structure is
still worthless since it doesn't fit into the SOCKADDR_IN anywhere. The IP address
is in the HOSTENT structure, its just buried. Here are the members of the HOSTENT
structure that I found useful. The h_addrtype member holds the type of address
this uses, as with the sockets the only type is AF_INET. The h_name is a character
array that will contain the complete host and domain name for that computer,
for instance host.domain.com. One catch to this, it will not do reverse name
lookups, for example if you look up the computer name "MyComputer" h_name will
hold "MyComputer.MyDomain.com" , however if you look up the computer named "10.10.10.1"
(which is really its IP) it will not translate that into a computer name gethostbyname()
will just put the text "10.10.10.1" in h_name. The last member I want to discuss
is h_addr_list, this one is somewhat confusing so of course it has the information
we are really after. The member h_addr_list if a variable of type char**, but
every time I have used it only one dimension of the array is used. In the data
that is filled the first four bytes hold the four octets of the IP address.
The rest of the array holds the same information as h_name. The octets are written
as unsigned char values so you would have to place them into the SOCKADDR_IN
structure like this:
In that way you can use the computer's name to find its IP so you can connect
to any server you have the name of. Using this same technique you can also find
your own computers IP address.
Very good tutorial, i have done this in VB, and now you came up with the solutions in C++......then I dont have to think about it anymore...Keep up the good work !!!!! (If this comment was disrespectful, please report it.)
great tutorial! just wanted to mention that the portnumber couldnt be set with a simple int. i had to do it with: SockAddr.sin_port = htons(80); (If this comment was disrespectful, please report it.)
hi, I couldn't compile it, I'm using gcc 2.95, with cygwin, it has the libraries, but may be i'm doing something wrong whent i try to link them. thanks in advance, wonderful article. (If this comment was disrespectful, please report it.)
well im just curious...while my windows program waits for the recv loop, it stops responding...is there any way around this? maybe the c++ equivalent of vb's DoEvents? (If this comment was disrespectful, please report it.)
i read this article and i learned alot from it but the only thing is as i read i wasnt sure how i would write out the whole code... if you coule post some sort of example or the full source that is in the article i would really appreciate it... i just need to see what the whole source put together looks like to understand it better. THANKS
(If this comment was disrespectful, please report it.)
I personaly copy-pasted the code to a console application, with minor changes made. It's rather easy to do, because even i could manage to pull it off. Anyway great tutorial. There was a nasty bug that toke a while for me to figure out.
The bug is in the RetVal = recv(Socket, String, strlen(String) + 1, 0); recv is for the one sending and if both are resiving it's just a endless while-loop. So to fix use send instead of recv. Another problem i encountered was with the (RetVal == WSAECONNABORT). I got an error on WSAECONNABORT, so i replaced it with -1. Any succestions why i got an error?
(If this comment was disrespectful, please report it.)
Another problem Im having is that I not quite understand how I should make the connection between client and server. In vb I did it with a data socket and a control socket, but which should be the best way to do it in a dos application?? (If this comment was disrespectful, please report it.)
I was thinking you could check for the connection in a Callback procedure, that way your program would still respond, instead of a for loop. Just a guess, never tested.
LRESULT CALLBACK Connection() { conect(...) blah blah blah } (If this comment was disrespectful, please report it.)
Good job guy, but replace a recv()by a send() where this tuto deals with the data stream ! Cya l8r =) (If this comment was disrespectful, please report it.)
9/2/2002 11:13:11 AM:
PERFECT! 5 Plaents! (If this comment was disrespectful, please report it.)
11/8/2002 1:59:51 PM:
Getting 3 errors on this line:
WSADATA WsaDat;
here are the errors:
missing ';' before identifier 'WsaDat'
'WSADATA' : missing storage-class or type specifiers
unexpected end of file found
So that's what I'm getting when I try to compile, any suggestions? (If this comment was disrespectful, please report it.)
11/16/2002 1:37:26 PM:
if you include it works (If this comment was disrespectful, please report it.)
11/16/2002 1:38:56 PM:
you have to #include . then it works (If this comment was disrespectful, please report it.)
error C2664: 'accept' : cannot convert parameter 3 from 'unsigned int' to 'int *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast (If this comment was disrespectful, please report it.)
hey! i'm totally new to c++ and i use visual c++ .net as compiler... but when i try to compile it it says that it can't find mpr.lib! btw it couldn't find wsock32.lib but i changed it to Winsock2.h then it worked... but haven't found a solution for mpr.lib yet - what can i do? (If this comment was disrespectful, please report it.)
Great tutorial to catch up Winsock programming I used to do.
Another way you can easly speficy an IP address is to use the inet_addr() function. it will take a char variable and automatically cast it into the correct structure. This is what I did:
Where ip is a char[15] containing the IP I want. Just though this might be a little easer than having to specify each octet. (If this comment was disrespectful, please report it.)
2/2/2003 5:32:08 AM:
if ur having trouble binding, then you copied and pasted.
I created a code like this but it always reports an error when connecting(connect())... What should i do with my e-mailing program? :( (If this comment was disrespectful, please report it.)
As someone here said the loop blocks the programm from responding...you should always use while loops (especially infinite ones) in different threads. Check the info about CreateThread (Windows CE) function (Very handy for accept and recv loops) (If this comment was disrespectful, please report it.)
7/15/2003 6:41:41 AM:
The tutorial seems great but i keep getting the error message "Error spawning cl.exe" when i try to build my project .... i followed the instructions to creat the project with all the added libraries and so on but when I add a source file that error message keeps comming up and if i choose a header file to write all my stuff in all I get when i press the build button is "Cannot build" or something like that.... please help all you nice guys/girls out there i really want this to work!! ;) (If this comment was disrespectful, please report it.)
7/15/2003 7:15:14 AM:
is there some code in this tutorial that is beeing outleaved? I´m sure it´s a great tutorial, well what i´ve read up until now of this tutorial it really seems great but i get lost around that struct sockaddr when he use struct sockaddr_in insteed but i don´t think he shows how he use it... please help me (If this comment was disrespectful, please report it.)
8/31/2003 5:02:56 PM:
Don't you test your own code? (If this comment was disrespectful, please report it.)
i havent tested this guys code yet, but take it easy everyone, ur obviosuly doing somethign rong, and of course he tested hsi own code (If this comment was disrespectful, please report it.)
man i hate comments on c++..... dont ask the writer how to do something, learn it yourself dummies... whats the point of tutorials if you don't learn? winsock is NOT the first thing you should be learning in c++, btw lol. but yeah 5 globes from me (If this comment was disrespectful, please report it.)
9/26/2003 2:23:43 AM:
Newbie, chances are you will not see this, but none the less, you need to have a function or a program :-/ ill assume this is console, since htats easier, by a lot, anyways #include #include /*What was the deal with winsock2 ??*/ #include
int main() { WSADATA Wsa;
if(WSAStartup(MAKEWORD(1,1), &Wsa)!=0) { cout<<"This didnt work\n" } cout<<"Press Any Key to Continue\n" getch(); return 0; }
(If this comment was disrespectful, please report it.)
9/27/2003 11:39:11 AM:
ive been having a problem with my visual basic 6.. When I compile in Visual c++ it just says linking... and stops responding and i cant do anything else, cant even stop the build I have to end task it. Ive been using this program for a long time but never had this problem before. If someone could help me i would appreciate it. Thanks (If this comment was disrespectful, please report it.)
SOCKADDR Addr; accept(Socket, &Addr, sizeof(Addr); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ whoops... missed a ) to close off the accept() (If this comment was disrespectful, please report it.)
would u plz guide me abt the complete socket programming
(If this comment was disrespectful, please report it.)
3/14/2004 9:54:54 PM:
difficult to follow, and you lack the knowledge that makes the function properly explainable...post when you can do it a little better. (If this comment was disrespectful, please report it.)
5/1/2004 4:36:20 PM:
what is mpr.lib for? I've never had to include that in any of my other winsock apps.. could someone explain please? (If this comment was disrespectful, please report it.)
6/10/2004 10:10:02 PM:
The client says it sends the message, but the server doesn't receive it. Please help. Thanks in advance. (If this comment was disrespectful, please report it.)
6/18/2004 6:00:06 PM:
This is really a good exposition you gave. I learnt a lot from it. However, can't one just use Command Prompt syntax "ipconfig" to find the ip address, instead of checking HOSTENT? (If this comment was disrespectful, please report it.)
8/7/2004 12:49:17 AM:
hi i had this problem with the send and recv: c:\Documents and Settings\Manale\Desktop\Stage\MYPROG\Server\Socket Server.cpp(164): error C2664: 'recv' : cannot convert parameter 2 from 'char [2][10]' to 'char *' Do you have any idea how to solve it. I am using .net 2003, and using c++ compiler.Thx
(If this comment was disrespectful, please report it.)
9/15/2004 7:54:43 PM:
Grr! Another flakey tutorial with loads of errors. Thank God for the comments from more knowledgable people correcting your mistakes, otherwise us beginners wouldn't stand a chance. Maybe check your code before releasing it to tens of thousands of people? Good idea? (If this comment was disrespectful, please report it.)
9/17/2004 6:28:34 PM:
This is by FAAR the BEST tutorial i have EVER SEEN!!! gj man, dont let the other C++ noobs bring you down because they cant figure out 2+2, GJ!! (If this comment was disrespectful, please report it.)
11/12/2004 9:31:15 AM:
You easily saved me three or four days work, trawling through MSDN. Thanks man! (If this comment was disrespectful, please report it.)
12/5/2004 3:08:35 AM:
I get this error 18 times for various parts of this code.
error C2228: left of '.s_b4' must have class/struct/union type (If this comment was disrespectful, please report it.)
1/25/2005 12:59:02 PM:
Not bad... I only have one suggestion for improving this tutorial. Make it bigger! I'm assuming that WinSock has far more to it than this. Still, an excellent job. (If this comment was disrespectful, please report it.)
I think i m kinda late posting (since the tutorial was posted way back) this message but i would apreciate if the author would post something on Asynchronous Sockets (without loops). u cant expect the entire OS to wait for a connection........... (If this comment was disrespectful, please report it.)
using TCPView, I see that it's not using the port chosen to listen on (port 50 in the article), it seems to be random, anyone a clue as to why? It doesn't seem to stop the client connecting tho, but only if I let all ports through the firewall, allowing just port 50 doesn't work. (If this comment was disrespectful, please report it.)
ok, I now know what's happening, it's big/little endian problem! there's another tutorial I found here: http://www.madwizard.org/view.php?page=tutorials.networking.chapter4&lang=cppbasically the port number you specify is in the form: SockAddr.sin_port = htons(50); man, that bugged me!
(If this comment was disrespectful, please report it.)
I m hitting some problems, the code works, but it seems only in my wireless network(i m behind a router), when i use the external ip i found from www.whatismyip.com, it cant seem to connect, any help would be appreciated. (If this comment was disrespectful, please report it.)
2. Access to a specific web page of your preference and get a SIMPLE sample code of a WinSock programming relating to a DATA COMMUNICATIONS AND NETWORKING application. (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)