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
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Difference Between Daemon Threads and User Threads In Java
- Difference between Satellite Communication and Optical Communication
- Difference between Analog Communication and Digital Communication
- Java Implementation of Deffi-Hellman Algorithm between Client and Server
- Difference between Client-Server and Peer-to-Peer Network
- Difference between Server OS and Client OS
- Difference between Client /Server and Distributed DBMS
- TCP Server-Client implementation in C
- UDP Server-Client implementation in C
- TCP Client-Server Program to Check if a Given String is Palindrome
- Distributed Objects Computing: The next generation of client-server computing
- Differences between Point-to-Point and Multi-point Communication
- Difference Between Computer Network and Data Communication
- Print 1 2 3 infinitely using threads in C
- C++ File Writer-Reader application using Windows Threads
- Data Communication over Telephone Lines Using Modems
- Explicitly assigning port number to client in Socket
- Advantages and disadvantages of Remote Communication Technology
- Network and Communication
- Advantages and disadvantages of Wireless Communication