BK's First Winsock Classes for TCP/IP

This page contains my first class using winsock for TCP/IP communications.

Searching for the right tutorial on creating winsock code in C++ was a nightmare. It took more than a week and a bunch of replies on MSDN forum before I found a decent set of instructions. That page is in the list of links above.

I started with the instructions and created a class for the server and the client along with a main() to drive each. If you need to add TCP to current C++ code this may help out.

Short digression: I did find another MSDN page with insructions on Winsock that I think are for asynchronous winsock. Here is the full link:
I have not yet sucessfully built a class with following this web page. There is something that I am missing but will keep working.

Back to the working code. Each method returns a status. However, for simplicity I omitted all code in the main that checks the status. If you want to adapt this to your code, you will need to remove all the print statements, then use the status values in your code.

The six files that make up the two classes and applications have been placed on this site as html, so you can view them without downloading anything, and as a zip containing all code files. The files that have "server" as part of the name form the server class and a main to call and test the class. The files that have "client" as part of the name form the client class and a main to call and test the class.

To compile and run the code do the following for the server file.

  1. Get a copy of each file on your computer.
  2. Create a new project and select Visual C++ -> Win32 -> Win32 Console Application.
  3. Select name for the Name and Solution Name. The names I selected were BK_Socket_Client and BK_Socket_Server.
  4. Select Finish or select Next then Empty project.
  5. Open the original main file and copy it all into your new main file. Replace everything that was there with the new code.
  6. Copy the two class files (cpp and h) into your project directory.
  7. In your Solution Explorer right click on Header File, add an existing item, and add the new header file.
  8. Then do the same for the source code file of the class. You should have two source files and one header file.
  9. You should be ready to build.
  10. After building client or server, start another copy of Visual Studio and build the other.

Once both applications have been built, start the server then the client. The server must be started first. When the client is started you can enter text into the client DOS window and send it to the server. The server will respond telling the client how many characters were received.

To end the process enter a single character in the client then return. Both applications will then go to the exit point and wait for a final enter key to close out.

NOTE: I cheated in posting the code on the pages. The source was copied and pasted into Microsoft Word, then saved as .htm files and copied to this site. Windows users can use ctl-a and ctl-c to copy the code directly from the web page into your application, but there will be extra blank lines between every line of code. The zip file contains just the source code. It is not a project and you get only the .h and .cpp files.

I used Visual Studio very lightly for two years then realized that it can be started several times. The server can be started in one copy of VS while the client is started in another. You can then trace through each in their own window at the same time.

And if you go to that extent even once, get a second montitor or a really wide one as can be had from Dell. The extra real estate is great. Once you go double you will never go back.

October 2010