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

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.

3.Sequence of function for  client-server  communication




create a client server socket connection 
  1. Create a socket with the socket() system call.
  2. Connect the socket to the address of the server using the connect() system call.
  3. 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


Send data from client to server in socket programming 
  1. Create a socket with the socket() system call.
  2. Bind the socket to an address using the bind() system call. ...
  3. Listen for connections with the listen() system call.
  4. Accept a connection with the accept() system call. ...
  5. 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 :

*Socket :
Sockets are a mechanism for exchanging data between processes. These processes can either be on the same machine, or on different machines connected via a network. Once a socket connection is established, data can be sent in both directions until one of the endpoints closes the connection.
 

*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