Showing posts with label networking. Show all posts
Showing posts with label networking. Show all posts

Friday, May 18, 2018

UDP Server-Client Chat Implementation in C

UDP Server

Source code:= https://github.com/shouvik126/Networking-UDP-chat-
//Programme for UDP server

#include<stdio.h>
#include<string.h>
#include <sys/types.h>     
#include <sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netdb.h>
#include<fcntl.h>
#define PORT_NUM 1500  //port number on which message will recieve and send
int i=1;
int main()
{
 int sock;      //variable for socket descriptor(server)
 int len;       //variable for initialize structure length
 char out_buf[100],in_buf[100];   //variable for input and output message initialize

 struct sockaddr_in server,client;
 
 sock=socket(AF_INET,SOCK_DGRAM,0);       //socket descripter for server(using ipv4)
 server.sin_family=AF_INET;
 server.sin_port=htons(PORT_NUM);         //host to network short byte word
 server.sin_addr.s_addr=htonl(INADDR_ANY);//host to network long byte order
 len=sizeof(struct sockaddr_in);          //storing length of structure (define below)
 bind(sock,(struct sockaddr*)&server,len);//binding the socket with port num and ip

 printf("server is waiting.....\n");        //server goes to listen mode(explicitely define in tcp server)

 while(1)
 {
  recvfrom(sock,in_buf,sizeof(in_buf),0,(struct sockaddr *)&client,&len);//recieve byte by byte stream from client 
                if(i==1)    //at 'in_buf' through server socket 
  {
   printf("\nrequest accepted.................\n");
  }
  printf("\nrecieve from client...message:=%s\n",in_buf);    //display message recieve from client
         printf("\n\tEnter Message:=");
  scanf ("%[^\n]%*c",out_buf);//input along with space
  sendto(sock,out_buf,(strlen(out_buf)+1),0,(struct sockaddr *)&client,len);//stream send to client through socket
                                                                                  //(here length is increase by 1 because to assign null                                                                                        //explicitely at the end of the stream 'out_buf')
  i++;
 }
 close(sock); 
}
 

Screenshot


UDP Client 

Source code:= https://github.com/shouvik126/Networking-UDP-chat-
//programmme for udp client
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netdb.h>
#include<fcntl.h>
#define PORT_NUM 1500  //port number on which message will recieve and send
#define SERVER_IP "127.127.127.127"//loop back address as i am using same system for botth server and client

int main()
{
 int sock;  //variable for socket descriptor(client)
 int len;   //variable for initialize structure length
 char out_buf[100],in_buf[100];  //variable for input and output message initialize
 
 struct sockaddr_in server;        //server address is required by client but viceversa is not needed
 sock=socket(AF_INET,SOCK_DGRAM,0);//socket descripter for server(using ipv4)
 server.sin_family=AF_INET;
 server.sin_port=htons(PORT_NUM);  //host to network short byte word
 server.sin_addr.s_addr=inet_addr(SERVER_IP);//function 'inet_addr()' converts  the Internet host address cp from
                                                    // IPv4 numbers-and-dots notation into binary data in network byte  order.

 len=sizeof(struct sockaddr_in);    //storing length of structure (define below)
 //strcpy(out_buf,"hey server i am connecting with you..");//storing stream at out_buf for sending to server
 while(1)
 {
  printf("\n\tEnter MEssage:=");
  scanf("%[^\n]%*c",out_buf);
  sendto(sock,out_buf,(strlen(out_buf)+1),0,(struct sockaddr *)&server,len);//stream send to client through socket
                                                                                   //(here length is increase by 1 because to assign null 
             //explicitely at the end of the stream 'out_buf')

  recvfrom(sock,in_buf,sizeof(in_buf),0,(struct sockaddr *)&server,&len);  //recieve byte by byte stream from client 
            //at 'in_buf' through server socket
  printf("recieve from server message:=%s\n",in_buf);
 }
 close(sock);


}
 

Screenshot 


Multicast chat session based on multi-client-server based archi tecture

Server

Source Code:= https://github.com/shouvik126/multi-client-server-Simple-server-
//programme for simple server
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/un.h>
void main()
{
 int server_sockid, client_sockid;//to store socket descriptor for server and client
 int server_len,client_len;       //to store client amd server structure size
 char out_buf[100],in_buf[100];   //to store the sent and recieve message
 struct sockaddr_un server_address;//structure variable for server
 struct sockaddr_un client_address;//structure variable for client

 unlink("server_socket");          //deleate the name from the file system
       //means the name or process which  last
       //link to the file"server_socket" and ensure 
       //that no other process also opened that file
       //then the space used by that name or process
       //is deleated 
 server_sockid=socket(AF_UNIX, SOCK_STREAM, 0);//socket descripter for server
 server_address.sun_family=AF_UNIX;            //define the family EX-AF_UNIX(for local communication),AF_INET(ipv4)
 strcpy(server_address.sun_path,"server_socket");//assign "server_socket" file to current socket descriptor
 server_len=sizeof(server_address);              //size of the structure sockaddr_un
 bind(server_sockid,(struct sockaddr *)&server_address,server_len);//bind the server socket with the family
           //and path defined above
 listen(server_sockid,5);
 while(1)
 {
  //listen(server_sockid,5);                        //server goes to listen mode and wait for client
  client_len=sizeof(client_address);              //size of the structure sockaddr_un
  client_sockid=accept(server_sockid, (struct sockaddr *)&client_address, &client_len);//connection established
                //store the client descriptor at 
                //'client_sockid'
   printf("\nEnter message:=");//store message at'out_buf' for sending to client
   scanf("%[^\n]%*c",out_buf);
   send(client_sockid, out_buf, (strlen(out_buf)+1), 0);   //message is sent to client descriptor through 'out_buf'
        //and length is increase by 1 because to explicitely assign null 
        //at the end of the stream
   recv(client_sockid,in_buf,sizeof(in_buf),0);            //recieving message from client socket descriptor through 'in_buf'
   printf("\nRecieved from client----data=%s\n",in_buf);     //printing message
 close(client_sockid); 
 }
 //close(client_sockid);
 close(server_sockid);//closing 2 descriptor
}
 

Screenshot :=

Client

//programme for simple client
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/un.h>
void main()
{
 int client_sockid;//to store socket descriptor for client
 int client_len;    //to store client amd server structure size
 char out_buf[100],in_buf[100];//to store the sent and recieve message


 struct sockaddr_un client_address;//structure variable for client
 while(1)
 {
 client_sockid=socket(AF_UNIX,SOCK_STREAM,0);//client descripter for client
 client_address.sun_family=AF_UNIX; //define the family EX-AF_UNIX(for local communication),AF_INET(ipv4)
 strcpy(client_address.sun_path,"server_socket");//assign "server_socket" file to current socket descriptor
 client_len=sizeof(client_address);//size of the structure sockaddr_un
  connect(client_sockid,(struct sockaddr *)&client_address,client_len);//connect to the server which is 
            //in listen mode
  recv(client_sockid,in_buf,sizeof(in_buf),0);          //recieve message from server through client socket
             //in 'in_buf'
  printf("recieve from server data......'%s'\n",in_buf);//printing recieved message
  printf("\nEnter a message:=");//store message at'out_buf' for sending to servr
  scanf("%[^\n]%*c",out_buf);
  send(client_sockid,out_buf,(strlen(out_buf)+1),0);//message is sent to server descriptor through 'out_buf'
  close(client_sockid);
 }                                                 //and length is increase by 1 because to explicitely assign null 
                                                          //at the end of the stream
                                                                 
 //close(client_sockid);//close client socket descriptor
}
 

Screenshot :=

Monday, May 14, 2018

TCP Server-Client implementation in C

TCP Server - Client Interaction

TCP SERVER

  //Program for tcp server...
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netdb.h>
#include<fcntl.h>
#define PORT_NUM 2500 //port number on which message will recieve and send
int main() {

int server_sockfd, client_sockfd; //variable for socket descriptor(server and client)
int server_len, client_len;       //variable for initialize structure length
char out_buf[100];
char in_buf[100];                 //variable for input and output message initialize

struct in_addr client_ip_addr;    //structure variable for 'in_addr'to store IP address of client

struct sockaddr_in server_address;//structure variable to store server data
struct sockaddr_in client_address;//structure variable to store client data

unlink("server_socket");          //deleate the name from the file system
       //means the name or process which  last
       //link to the file"server_socket" and ensure 
       //that no other process also opened that file
       //then the space used by that name or process
       //is deleated 
server_sockfd=socket(AF_INET, SOCK_STREAM, 0);//socket descripter for server(using ipv4)

server_address.sin_family=AF_INET;//define the family EX-AF_UNIX(for local communication),AF_INET(ipv4)
server_address.sin_port=htons(PORT_NUM);//host to network short byte word
server_address.sin_addr.s_addr=htonl(INADDR_ANY);//host to network long byte order

server_len=sizeof(server_address);//storing length of structure (define below)
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);//binding the socket with port num and ip
listen(server_sockfd, 5);//server goes to listen mode and wait for client (max 5 client can connect)

client_len=sizeof(client_address);//storing length of structure (define below)

client_sockfd=accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);//connection established
                //store the client descriptor at 
                //'client_sockid'

memcpy(&client_ip_addr, &client_address.sin_addr.s_addr,4);//memspy()function 
      //copies 4 byte(32 bit) from 
      //source (&client_address.sin_addr.s_addr)client Ip address 
      //to destination(client_ip_addr)

printf("Accept completed (IP address of client=%s port=%d) \n",inet_ntoa(client_ip_addr), ntohs(client_address.sin_port));
      //printing IP address present in(client_ip_addr) and port number

while(1) {
printf("\nEnter ur Mesg...:=");
scanf("%s",out_buf);//taking string from user for sending to client
send(client_sockfd, out_buf, (strlen(out_buf)+1), 0);//message is sent to client descriptor through 'out_buf'
        //and length is increase by 1 because to explicitely assign null 
        //at the end of the stream

recv(client_sockfd, in_buf, sizeof(in_buf), 0);//recieving message from client socket descriptor through 'in_buf'
printf("\n\tReceived from client...Mesg='%s'\n", in_buf);//printing the message
}
close(server_sockfd);//closing two socket descriptor
close(client_sockfd);
} 
 
Screenshot:= 

TCP CLIENT 

//Program for tcp client...
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<fcntl.h>
#include<unistd.h>

#define PORT_NUM 2500  //port number on which message will recieve and send
#define IP_ADDR "127.127.127.127"//Loop back ADDRESS as I am using same machine     
                                 //for both server and reciever

int main() {

int client_sockfd;//variable for socket descriptor(client)
int server_len;//variable for initialize structure length
char out_buf[100];//variable for input and output message initialize
char in_buf[100];

struct sockaddr_in server_address;//structure variable to store server data

client_sockfd=socket(AF_INET, SOCK_STREAM, 0);//socket descripter for server(using ipv4)
server_address.sin_family=AF_INET;//define the family EX-AF_UNIX(for local communication),AF_INET(ipv4)
server_address.sin_port=htons(PORT_NUM);//host to network short byte word
server_address.sin_addr.s_addr=inet_addr(IP_ADDR);//assigning IP address defined above

server_len=sizeof(server_address);//storing length of structure (define below)
connect(client_sockfd, (struct sockaddr *)&server_address, server_len);//connect to the server which is 
              //in listen mode

while(1) {//this infinite loop for continuous chat
recv(client_sockfd, in_buf, sizeof(in_buf), 0);//recieving message from server through client socket descriptor in 'in_buf'
printf("Received from server...Mesg='%s'\n", in_buf);//printing the message

printf("ENter ur Mesg...");
scanf("%s",out_buf);//taking string from user for sending to server
send(client_sockfd, out_buf, (strlen(out_buf)+1), 0);//message is sent to client descriptor through 'out_buf'
        //and length is increase by 1 because to explicitely assign null 
        //at the end of the stream

}
close(client_sockfd);//closing connection bye bye
}
 
Screenshot:=
  

Sunday, May 13, 2018

Simple Serve-Client programme in C(AF_UNIX)

SIMPLE SERVER

 Source Code:=https://github.com/shouvik126/networking-simple-server-client-
//programme for simple server
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/un.h>
void main()
{
 int server_sockid, client_sockid;//to store socket descriptor for server and client
 int server_len,client_len;       //to store client amd server structure size
 char out_buf[100],in_buf[100];   //to store the sent and recieve message
 struct sockaddr_un server_address;//structure variable for server
 struct sockaddr_un client_address;//structure variable for client

 unlink("server_socket");          //deleate the name from the file system
       //means the name or process which  last
       //link to the file"server_socket" and ensure 
       //that no other process also opened that file
       //then the space used by that name or process
       //is deleated 
 server_sockid=socket(AF_UNIX, SOCK_STREAM, 0);//socket descripter for server
 server_address.sun_family=AF_UNIX;            //define the family EX-AF_UNIX(for local communication),AF_INET(ipv4)
 strcpy(server_address.sun_path,"server_socket");//assign "server_socket" file to current socket descriptor
 server_len=sizeof(server_address);              //size of the structure sockaddr_un
 bind(server_sockid,(struct sockaddr *)&server_address,server_len);//bind the server socket with the family
           //and path defined above
 
 listen(server_sockid,5);                        //server goes to listen mode and wait for client
 client_len=sizeof(client_address);              //size of the structure sockaddr_un
 client_sockid=accept(server_sockid, (struct sockaddr *)&client_address, &client_len);//connection established
                //store the client descriptor at 
                //'client_sockid'
 strcpy(out_buf, "A text message from server to client");//store message at'out_buf' for sending to client
 send(client_sockid, out_buf, (strlen(out_buf)+1), 0);   //message is sent to client descriptor through 'out_buf'
        //and length is increase by 1 because to explicitely assign null 
        //at the end of the stream
 recv(client_sockid,in_buf,sizeof(in_buf),0);            //recieving message from client socket descriptor through 'in_buf'
 printf("Recieved from client----data=%s\n",in_buf);     //printing message

 close(server_sockid);//closing 2 descriptor
 close(client_sockid);
}
 
Screenshot:= 

SIMPLE CLENT 

 Source code:=https://github.com/shouvik126/networking-simple-server-client-
//programme for simple client
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/un.h>
void main()
{
 int client_sockid;//to store socket descriptor for client
 int client_len;    //to store client amd server structure size
 char out_buf[100],in_buf[100];//to store the sent and recieve message


 struct sockaddr_un client_address;//structure variable for client
 client_sockid=socket(AF_UNIX,SOCK_STREAM,0);//client descripter for client
 client_address.sun_family=AF_UNIX; //define the family EX-AF_UNIX(for local communication),AF_INET(ipv4)
 strcpy(client_address.sun_path,"server_socket");//assign "server_socket" file to current socket descriptor
 client_len=sizeof(client_address);//size of the structure sockaddr_un

 connect(client_sockid,(struct sockaddr *)&client_address,client_len);//connect to the server which is 
              //in listen mode
 recv(client_sockid,in_buf,sizeof(in_buf),0);          //recieve message from server through client socket
             //in 'in_buf'
 printf("recieve from server data......'%s'\n",in_buf);//printing recieved message
 strcpy(out_buf,"text message from client to server....");//store message at'out_buf' for sending to servr
 send(client_sockid,out_buf,(strlen(out_buf)+1),0);//message is sent to server descriptor through 'out_buf'
                                                          //and length is increase by 1 because to explicitely assign null 
                                                          //at the end of the stream
                                                                 
 close(client_sockid);//close client socket descriptor
}
 
Screenshot:= 

*Defination of all function and structure is defined in UDP portion
https://letstartprogramme.blogspot.in/2018/05/udp-server-programme-for-udp.html 
 

UDP Server-Client implementation in C

UDP Server-Client interaction




UDP SERVER

 
//Programme for UDP server

#include<stdio.h>
#include<string.h>
#include <sys/types.h>     
#include <sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netdb.h>
#include<fcntl.h>
#define PORT_NUM 1500  //port number on which message will recieve and send
int main()
{
 int sock;      //variable for socket descriptor(server)
 int len;       //variable for initialize structure length
 char out_buf[100],in_buf[100];   //variable for input and output message initialize

 struct sockaddr_in server,client;
 
 sock=socket(AF_INET,SOCK_DGRAM,0);       //socket descripter for server(using ipv4)
 server.sin_family=AF_INET;
 server.sin_port=htons(PORT_NUM);         //host to network short byte word
 server.sin_addr.s_addr=htonl(INADDR_ANY);//host to network long byte order
 len=sizeof(struct sockaddr_in);          //storing length of structure (define below)
 bind(sock,(struct sockaddr*)&server,len);//binding the socket with port num and ip

 printf("server is waiting.....\n");        //server goes to listen mode(explicitely define in tcp server)

 recvfrom(sock,in_buf,sizeof(in_buf),0,(struct sockaddr *)&client,&len);//recieve byte by byte stream from client 
                                                                        //at 'in_buf' through server socket 
 printf("request accepted.................\n");
 printf("recieve from client...message:=%s\n",in_buf);    //display message recieve from client
 strcpy(out_buf,"HI mr.client I have got your message"); //storing string at 'out_buf'
 sendto(sock,out_buf,(strlen(out_buf)+1),0,(struct sockaddr *)&client,len);//stream send to client through socket
            //(here length is increase by 1 because to assign null           //explicitely at the end of the stream 'out_buf')
 close(sock); 
}

  • SCREENSHOT------

UDP CLIENT

 
//programmme for udp client
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netdb.h>
#include<fcntl.h>
#define PORT_NUM 1500  //port number on which message will recieve and send
#define SERVER_IP "127.127.127.127"//loop back address as i am using same system for botth server and client

int main()
{
 int sock;  //variable for socket descriptor(client)
 int len;   //variable for initialize structure length
 char out_buf[100],in_buf[100];  //variable for input and output message initialize
 
 struct sockaddr_in server;        //server address is required by client but viceversa is not needed
 sock=socket(AF_INET,SOCK_DGRAM,0);//socket descripter for server(using ipv4)
 server.sin_family=AF_INET;
 server.sin_port=htons(PORT_NUM);  //host to network short byte word
 server.sin_addr.s_addr=inet_addr(SERVER_IP);//function 'inet_addr()' converts  the Internet host address cp from
                                                    // IPv4 numbers-and-dots notation into binary data in network byte  order.

 len=sizeof(struct sockaddr_in);    //storing length of structure (define below)
 strcpy(out_buf,"hey server i am connecting with you..");//storing stream at out_buf for sending to server
 sendto(sock,out_buf,(strlen(out_buf)+1),0,(struct sockaddr *)&server,len);//stream send to client through socket
                                                                                   //(here length is increase by 1 because to assign null 
             //explicitely at the end of the stream 'out_buf')

 recvfrom(sock,in_buf,sizeof(in_buf),0,(struct sockaddr *)&server,&len);  //recieve byte by byte stream from client 
            //at 'in_buf' through server socket
 printf("recieve from server message:=%s\n",in_buf);
 close(sock);


}

 
  • SCREENSHOT--------
 
  • Explanation of different functions and structure 

    1. Data structure used to hold address information

      • struct sockaddr{
      unsigned short sa_family;
      char sa_data[14];
      sa_family is the domain type ex-
      AF_UNIX, AF_LOCAL   Local communication           
      AF_INET                       IPv4 Internet protocols         
      AF_INET6                     IPv6 Internet protocols         
      AF_IPX                          IPX - Novell protocols
      AF_NETLINK                Kernel user interface device

      • struct sockaddr_in{
      short sin_family;
      unsigned short sin_port;
      struct in_addr sin_addr;
      char sin_zero[8]; 
      }  
      sin_family is same as above sa_family i,e domain type ex- 
      AF_UNIX, AF_LOCAL   Local communication           
      AF_INET                       IPv4 Internet protocols         
      AF_INET6                     IPv6 Internet protocols         
      AF_IPX                          IPX - Novell protocols
      AF_NETLINK                Kernel user interface device
      sin_port---It is for assignning port number
      in_addr sin_addr----here in_addr is a structure whose variable is sin_addr,it is used for assigning ip address.
      sin_zero----It is used for padding

      • struct in_addr{
      unsigned long s_addr; //4 byte
      }
      s_addr---- it is used for storing 4 byte IP address(32 bit IP
    2. socket() - A Connection End point

      • This creates an end point for network connection.
       int socket(int domain,int type,int protocol)
      domin=AF_INET(ipv4 protocol)
      type=   SOCK_STREAM(TCP), SOCK_DGRAM(UDP)
      protocol=0
      • Eample :=socket(AF_INET,SOCK_STREAM,0)
      this will create a TCP socket
      • This al returns 1 ,when socket descriptor on success and -1 on an error
       
    3. bind() - Attaching to an IP and Port 

      • Aserver call bind to attach itself to a specific port and IP address
      int bind(int sockfd, const struct sockaddr *my_address, socklen_t address_length);
      my_addr=pointer to a valid sockaddr_in structure cast as a sockaddr *pointer
      addrlen=lngth of the sockaddr_in structur 
    4. listen() - wait for a connection

      • The serverprocess calls listen to tell the kernal to initialize a wait queueof connection for this socket

      listen(int sock, int backlog); 

      sock=socket returned by socket()
      backlog=maximum length of the pending connections queue

      • Example:listen(sock,10);
      This will allow a maximum of 10 connections to be in pending state
  • Accept() - A new connection! 

           > Accept is called by a Server process to accept new connections from      new clients trying to connect to the server

accept(int soccket, (struct sockaddr *)&client, socklen_t *client_len) 

socket= the socket in listen state
client=will hold new clients information when accept returns
client_len=pointer to size of the client structure

>Example:
struct sockaddr_in client;
int len=sizeof(client);
accept(sock, (struct sockaddr *)&client, &len); 

  • connect() - connect to a service

    >Connect is automatically called by a client to connect to a serverport

    conect(int sock, (struct sockaddr *)&server_addr, socklen_t len)

    sock : a socket returned by socket()
    server_addr : a sockaddr_in struct pointer filled with all the remote server detailes and cast as a sockaddr struct pointer
    len : size of the server_addr struct

    >Example:
    connect(sock, (struct sockaddr *)&server_addr, len); 
    • Send/Recv - Finally Data !!

      > send(),recv(),read(),write() etc calls are used to send or recieve data
      int send(int sock, void *mesg, size_t len,int flag)
      int recv(int sock, void *mesg, size_t len, int flag)

      sock=Aconnected socket
      mesg=pointer to a buffered to send /recieve data from/in
      len=size of the message buffer
      flags=0

      The return value is the number of bytes actually sent/recieved

      >EXAMPLE:

      char send_buffer[1024];
      char recv_buffer[1024];
      int sent_bytes;
      int recv_bytes;

      sent_bytes=send(sock,send_buffer,1024,0);
      recv_bytes=recv(sock, recv buffer,1024,0);

      • close) - T termiate the connection

        >close() =This signal indicates the end of the communication between server &c lient, this also closes the socket

        close(int sock)

        sock=the socketto close

        >EXAMPLE:close(sock);
       

                 

Fibonacci Series Using Dynamic Programming in C++

Fibonacci Series Using Dynamic Programming #include<iostream> #include<vector> using namespace std; int fibo(in...