Prerequisite: Socket Programming in C/C++
It is possible to send data from the server and receive a response from the client. Similarly, the client can also send and receive data to-and-from. Here we will discuss the approach using Win32 Threads in C/C++.
- Use CreateThread function which creates a new thread for a process.
- The CreateThread method must specify the starting address of the code that the new thread is to execute. Following is the prototype of CreateThread function:
HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );
- Then using WaitForSingleObject function that returns message in form of object received from client, to receive data from client. Following is the prototype of WaitForSingleObject function:
DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds );
Creating the Server program
In the Server Program, we will be using two threads, one for Sending data to the client and another for Receiving data from the client. The process of communication stops when both Client and Server type “exit“.
Below is the implementation of the Server Program:
Run the ServerApplication.cpp file using the command:
g++ ServerApplication.cpp -lws2_32
Creating the Client Program
In the Client Program, we will be using two threads one for Sending data to the server and another for Receiving data from the server. The process of communication stops when both Server and Client type “exit“.
Run the ClientApplication.cpp file using the command:
g++ ClientApplication.cpp -lws2_32