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