Socket Programming in C/C++
Client-Server chat in C++ using sockets
1.Client and server communication
The client sends a request, and the server returns a response. This exchange of messages is an example of inter-process communication. To communicate, the computers must have a common language, and they must follow rules so that both the client and the server know what to expect
2.Client-server socket programming
3.Sequence of function for client-server communication
- Create a socket with the socket() system call.
- Connect the socket to the address of the server using the connect() system call.
- Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls
- Create a socket with the socket() system call.
- Bind the socket to an address using the bind() system call. ...
- Listen for connections with the listen() system call.
- Accept a connection with the accept() system call. ...
- Send and receive data.
3.Implementation
server.cpp
client.cpp
// We use AF_ LOCAL as defined in the POSIX standard for communication between processes on the same host
//SOCK_STREAM: TCP(reliable, connection oriented)
**NOTE :
*TCP (Transmission control protocol)
A TCP (transmission control protocol) is a connection-oriented communication. It is an intermediate layer of the application layer and internet protocol layer in the OSI model. TCP is designed to send the data packets over the network. It ensures that data is delivered to the correct destination.
TCP creates a connection between the source and destination node before transmitting the data and keeps the connection alive until the communication is active.
In TCP before sending the data it breaks the large data into smaller packets and cares the integrity of the data at the time of reassembling at the destination node. Major Internet applications such as the World Wide Web, email, remote administration, and file transfer rely on TCP.
TCP also offers the facility of retransmission, when a TCP client sends data to the server, it requires an acknowledgment in return. If an acknowledgment is not received, after a certain amount of time transmitted data will be loss and TCP automatically retransmits the data.
The communication over the network in TCP/IP model takes place in form of a client-server architecture. ie, the client begins the communication and establishes a connection with a server.
Commentaires
Enregistrer un commentaire