TCP Client Server Communication

How do you communicate through internet ? what protocol do you use ? How the connection is established ?


Well , today we are going to see about establishing a simple tcp connection between client and the server .

In the client side , we are gonna to establish a connection with server (send a request )

For that we need to do 3 things ,
1.socket() -- create a socket .
2.connect() -- make a connection with the server .
3.recv() -- for receving data from server .  

below picture tells the needs for tcp client .


#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h> // to use many data types
#include<sys/socket.h> // to create a socket connection
#include<netinet/in.h> // to address internet protocol
int main()
{
int client_socket ;
client_socket = socket(AF_INET , SOCK_STREAM , 0);
// AF_INET - AF_INET is the address family that is used for the socket you're creating (in this case an Internet Protocol address).
struct sockaddr_in server_address;
// Creating a socket
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9003);
server_address.sin_addr.s_addr = INADDR_ANY;
//connecting with socket
connect(client_socket , (struct sockaddr*) &server_address , sizeof(server_address));
//reciving data
char server_response[256];
//reciving data from server
recv(client_socket , &server_response , sizeof(server_response) , 0) ;
printf("[+] Connection Established\n");
printf("[+] Message from server : %s\n",server_response);
close(client_socket);
return 0;
}
view raw tcp_client.c hosted with ❤ by GitHub

In tcp server , we are going get the response from the client and response to it .
For establishing a tcp server connection we need to do 4 things ,
1: socket() -- create a socket connection .
2: bind() -- bind with ip , server_address , port .
3: listen() -- listen for any connections that are active .
4: accept() -- for accepting the client request .

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h> // For supporting many data types
#include<sys/socket.h> // For creation of socket
#include<netinet/in.h> // For addressing Internet protocol
int main()
{
int server_socket ;
char server_message[256]="Hi buddy , This is Lazy Swapper" ;
// creating a SOCKET
server_socket = socket(AF_INET , SOCK_STREAM , 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9003);
server_address.sin_addr.s_addr = INADDR_ANY ;
//binding the socket
bind(server_socket , (struct sockaddr*) &server_address , sizeof(server_address));
//listening for connection
listen(server_socket , 5 );
// 5 denotes no of connection
//accepting the request
int client_socket ;
client_socket = accept(server_socket , NULL , NULL);
//sending the message
send(client_socket , server_message , sizeof(server_message) ,0 );
printf("[+] successfully message has been sent ");
close(server_socket);
return 0;
}
view raw tcp_server.c hosted with ❤ by GitHub

For video reference : tcp client
tcp server

Comments

Popular posts from this blog

Python : Is Just a Hype ?

What is the best way to learn Programming ?

Dangerous commands in linux that you should never try .