UDP Socket Programming in C language

UDP socket programming in C

Client-side program:

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<netdb.h>
int main(int argc, char * argv[])
{


    int sockfd,bindfd,fromlen,n;
    struct sockaddr_in server, from;
    struct hostent *hp;
    char buff[50];
    
    if(argc<3)
    {
        printf("insufficient argument");
        exit(1);
    }	

    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd<0)
    {
        printf("could not create the socket.");
        exit(1);
    }
    
    server.sin_family=AF_INET;
    server.sin_port=htons(atoi(argv[2]));
    hp=gethostbyname(argv[1]);
    memcpy((char*)hp->h_addr,(char  *)&server.sin_addr.s_addr,hp->h_length);
    
    fromlen=sizeof(server);

    printf("Enter the value to request the server:");
    fgets(buff,sizeof(buff),stdin);
    n=sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&server,fromlen);
    if(n<0)
    {
        printf("could not send message.");
    }
    n=recvfrom(sockfd,buff,50,0,(struct sockaddr *)&server,&fromlen);
    if(n<0)
    {
        printf("No data to receive.");
    }
    printf("Response sent by server is:%s",buff);	
    return 0;
}

 

Server Side Program:

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

int main(int argc, char * argv[])
{


    int sockfd,bindfd,fromlen,n;
    struct sockaddr_in server;
    struct sockaddr_in from;
    char buff[50];
    
    if(argc<2)
    {
        printf("insufficient argument");
        exit(1);
    }	
    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd<0)
    {
        printf("could not create the socket.");
        exit(1);
    }
    memset((char*)&server,0,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(atoi(argv[1]));
    server.sin_addr.s_addr=INADDR_ANY;
    bindfd=bind(sockfd,(struct sockaddr *)&server,sizeof(server));
    if(bindfd<0)
    {
        printf("could not bind.");
    }
    fromlen=sizeof(from);
    while(1)
    {
        n=recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&from,&fromlen);
        if(n<0)
        {
            printf("No data to receive.");
        }
        printf("Message received from client is:%s",buff);

        printf("type the response to be sent:");
        fgets(buff,sizeof(buff),stdin);		
        n=sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&from,fromlen);
        if(n<0)
        {
            printf("could not send message.");
        }
    
    }
    return 0;
}

 

Execution Instruction:

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.

 

TCP client-server programming in C

TCP Client Server-Socket Programming in C

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 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

 

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;
}

PHP beginner tutorials – zero to hero PHP course 2020

 

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.