API,article,understanding,WinSock,explains,cr
Quick Search for:  in language:    
API,article,understanding,WinSock,explains,cr
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 485,557 lines
 Jobs: 1,067 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
mini math programs
By Johno on 10/24


The matrix
By Alcodes on 10/24


Palindrome Finder
By Jonathan Volk on 10/24


processor identification and info
By Razvan Petrescu on 10/24


Login Interface using ncurses for Linux
By Suvoraj Biswas on 10/24


Matrix Encryption Algorithm
By Tony Fecteau on 10/23


SMALL TEXT GAME
By cJ! on 10/23


stod
By Mark Jundo P. Documento on 10/23


Basic Loops
By richard james on 10/23


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

WinSock Programming

Print
Email
 

Submitted on: 8/24/2001 2:09:03 PM
By: Jason Beighel 
Level: Beginner
User Rating: By 22 Users
Compatibility:C++ (general), Microsoft Visual C++

Users have accessed this article 19834 times.
 
(About the author)
 
     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...   
1) 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.   
2) 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.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) 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:

     WSADATA WsaDat;
     if (WSAStartup(MAKEWORD(1, 1), &WsaDat;) != 0)
           {
            printf("WSA Initialization failed.");
           }

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:

      SOCKET Socket;
      Socket = socket(AF_INET, SOCK_STREAM, 0);
      if (Socket == INVALID_SOCKET)
           {
          printf("Socket creation failed.");
           }

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 prepa // red 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 thi // s 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:

SOCKADDR Addr;
accept(Socket, &Addr;, sizeof(Addr);

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 1 // 27.0.0.1
//I'll give a few better ways to set thi // s 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:

SOCKADDR_IN SockAddr;
HOSTENT *HostInfo;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)HostInfo->h_addr_list[0][0];
SockAddr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)HostInfo->h_addr_list[0][1];
SockAddr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)HostInfo->h_addr_list[0][2];
SockAddr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)HostInfo->h_addr_list[0][3];

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.


Other 4 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
8/24/2001 11:11:51 PM:DarkStarX
This is a great tutorial!!! It's exactly what I needed to learn the basics!! 5 globes! Thanks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/25/2001 8:56:37 PM:Ryan O'Connor
Absolutely Brilliant. Well done - 5 globes
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/26/2001 6:54:34 PM:Will
Very nice article. Keep it up!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/22/2001 7:40:52 AM:Staffan C
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 !!!!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/28/2001 6:35:45 AM:Adrian Rudnik
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);
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/24/2001 9:15:57 PM:Cody
Nice Job Dude! That helps a lot, i had some holes in the looping for sending and recieving and this really cleared em up. 6 Globes
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/1/2001 11:20:45 AM:BlackWizzard
It dont work... can you post an example?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/6/2002 12:18:36 PM:Festivius
Thanks+for+the+tutorial%2C+it+answered+s ome+questions+I+had%2E++I+just+want+to+m ention+to+everyone+though+just+one+smal+ error%2E+++When+you+were+setting+up+the+ IP+adress+in+the+article%2C+++the+4th+bi t+you+have+as+%2E%2E%2E%2Es%5Fb1%3D1+whe re+it+should+be+%2E%2E%2E%2E%2Es%5Fb4%3D 1++very+small++but+if+someone+is+going+t o+be+cut+and+pasting+your+code+it+could+ cause+them+a+little+frustration%2E++Than ks+again+for+the+tutorial%2E
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/6/2002 12:20:39 PM:festivius
Sorry about the above posting Dont know why it came out like that
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/19/2002 10:45:11 AM:GJ
Nice article dude, but GetHostByName still cant look up www.microsoft.com for me :(
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/15/2002 5:33:12 PM:Luis
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/6/2002 12:40:45 PM:some dude
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/9/2002 5:23:54 PM:dataholic_
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; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^ Whoops, change the last b1 to b4 ;-)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/25/2002 9:24:57 PM:[Lucifuge]
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/19/2002 12:50:02 PM:kumikana
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; } }
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/19/2002 12:51:27 PM:kumikana
Sorry i botched... my mistake... 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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/27/2002 3:45:26 AM:Henrik
Can anyone give me any suggestions on why the bind() is failing??
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/27/2002 5:48:24 AM:Henrik
Never mind, I got 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??
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/28/2002 4:21:22 PM:Kurt Serge
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 }
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/30/2002 8:09:28 AM:Juz4m
Good job guy, but replace a recv()by a send() where this tuto deals with the data stream ! Cya l8r =)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/2/2002 11:13:11 AM:
PERFECT! 5 Plaents!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/15/2002 9:30:56 AM:
dsfds
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.