This tutorial will guide you on how to create a simple tcp/ip client and server application by using windows socket 2 (Winsock2). The following diagram illustrate the design of the application that we’re going to create in this tutorial.

The server will listen at a given IP & port number. whenever it receive message from its connected client, it then print out the message and bouce back the message to the client. On the other hand, the client will connect to the server and user will enter a message to send to the server. the client will print out the message that it receive from the server too. To stop the connection, the client will need to send a message “close” to the server.

Windows Socket Example

  1. Create a new visual C++ empty project using visual studio 2010 express edition. Name the project ClientExample.
  2. Add a new visual C++ empty project to the same solution. Name it ServerExample.
  3. Add a main.cpp file to both ClientExample & ServerExample project.
  4. Windows Socket Example
  5. Enter the following code to main.cpp in the ClientExample project.
  6. Enter the following code to main.cpp in the ServerExample project.
  7. Compile both projects. You may run both client and server application in 2 CMD windows. Enter a message in the client and it will send to the server.
  8. Windows Socket Example
  9. Download sample source code: ClieServerExample.zip