You are currently viewing TCP Client Server-Socket Programming in C

TCP Client Server-Socket Programming in C

Spread the love

TCP Client-Server Socket Programming in C

TCP Client Server-Socket Programming | Socket programming is a common way of connecting two nodes on a network to communicate with each other. One node/socket is listening to a particular port at an IP. While another socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.

TCP Client Server-Socket Programming
TCP Client Server-Socket Programming

TCP Stands for Transmission Control Protocol. TCP is a communication protocol that was introduced in 1974.

Responsibilities of TPC:

Creating packets from byte stream received from the application layer. Transport Layer uses the port numbers to create a process to process communications. It uses the acknowledgement packet, time out, and retransmission
to achieve error control.

Features:

TCP is a connection-oriented protocol. Before any data transfer activity, TCP established a connection. One entity who is waiting for connection is server and another entity is a client connects to the server. In TCP is connection is full-duplex.

TCP is a reliable transmission protocol. Byte streams are broken into chunks known as segments. The receiver always sends acknowledgements (ACKs) for segments. If a segment not received in time then TCP retransmit. We can say TCP maintains timer as well.

TCP-segment
TCP Segment

 

Client-Side Programming:

#include<stdio.h>
#include<sys/socket.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<netdb.h>

int main(int argc, char *argv[])
{
    int sockfd, portno, conn;
    char buffer[256];
    int buf;	
    struct sockaddr_in server;
    struct hostent *server1;

    if(argc<3)
    {
        perror("Not provided all the information.");
        exit(1);
    }

    sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        perror("could not create socket.");
        exit(1);
    
    }

    server1=gethostbyname(argv[1]);
    if(server1==NULL)
    {
        perror("No such host is available");
        exit(1);
    }
    
    
    memset((char *)&server, 0, sizeof(server));
    server.sin_family=AF_INET;
    memcpy((char *)server1->h_addr,(char *)&server.sin_addr.s_addr,server1->h_length);

    
    portno=atoi(argv[2]);
    server.sin_port=htons(portno);
    

    conn=connect(sockfd,(struct sockaddr *)&server,sizeof(server));
    if(conn<0)
    {
        perror("could not connect server.");
        exit(1);
    }
    printf("Please enter the message: ");

    memset(buffer,0,sizeof(buffer));
    fgets(buffer,256,stdin);

    buf=write(sockfd,buffer,strlen(buffer));
    if(buf<0)
    {
        perror("error in writing.");
        exit(1);
    }
    
    memset(buffer,0,sizeof(buffer));
    buf=read(sockfd,buffer,256);
    if(buf<0)
    {
        perror("error in writing.");
        exit(1);
    }
    printf("%s\n",buffer);	
    return 0;
}

 

Server Side

#include<stdio.h>
#include<sys/socket.h>
#include<string.h>
//#include<sys/types.h>
#include<netinet/in.h>	//...for htons,INADDR_ANY
#include<stdlib.h>
#include<unistd.h>


int main(int argc, char *argv[])
{
    int sockfd, newsockfd, bindfd,portno,clilen;
    char buffer[256];
    int buf;	
    struct sockaddr_in server, cli_addr;

    if(argc<2)
    {
        perror("Not provided all the information.");
        exit(1);
    }

    sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        perror("could not create socket.");
        exit(1);
    
    }

    memset((char *)&server, 0, sizeof(server));

    server.sin_family=AF_INET;
    server.sin_addr.s_addr=INADDR_ANY;

    portno=atoi(argv[1]);
    server.sin_port=htons(portno);

    bindfd=bind(sockfd, (struct sockaddr *)&server, sizeof(server));
    if(bind<0)
    {
        perror("could not bind.");
        exit(1);
    }

    listen(sockfd,5);

    clilen=sizeof(cli_addr);
    newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
    if(newsockfd<0)
    {
        perror("could not accept client.");
        exit(1);
    }

    memset(buffer,0,sizeof(buffer));

    buf=read(newsockfd,buffer,256);
    if(buf<0)
    {
        perror("reading from socket.");
        exit(1);
    }

    printf("the value is %s\n",buffer);
    buf=write(newsockfd,"this is for chapter 5",25);
    if(buf<0)
    {
        perror("error in writing.");
        exit(1);
    }
    return 0;
}

 

Program Execution Guide:

To compile server.c:
$gcc -o exe_filename server.c
To run exe_filename:
$./exe_filename port_number

Whatever may be the executable file name.
port_number should be the 16-bit integer number.
You have to make sure the server is in listen mode.

To compile client.c:
$gcc -o exe_filename1 client.c
To run exe_filename1:
$./exe_filename1 localhost port_number

in place of localhost, you have to mention the IP address of the server, if your server is listening in another machine.

 

Install SSL certification to encrypt your website’s transaction for free.

Encrypting files on ubuntu.


Spread the love

Santosh Adhikari

Hello, it's me Santosh Adhikari, from Kathmandu, Nepal. I'm a student of Science and Technology. I love new challenges and want to explore more on Software Quality Assurance. I want to be a professional SQA. I'm also interested in Blogging, SEO activities.
0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
data analytics course
3 years ago

I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra.
data analytics course