Skip to main content

Posts

Showing posts with the label Socket Program

Addition of two numbers on Server sent from Client [TCP] using C

/* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <strings.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define CLIENT_ADDR "127.0.0.1" #define SERVER_PORT 3786 #define CLIENT_PORT 8229  main () {   int sd, rc, i,n;   struct sockaddr_in clientAddr, servAddr;   char line[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   servAddr.sin_port = htons(SERVER_PORT); /*   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   inet_aton(SERVER_ADDR, &servAddr.sin_addr);   servAddr.sin_port = htons(SERVER_PORT); */   /*****

Concatenation of two strings sent from Client on the Server - [ TCP ] using C

/* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <strings.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define CLIENT_ADDR "127.0.0.1" #define SERVER_PORT 3786 #define CLIENT_PORT 8229  main () {   int sd, rc, i,n;   struct sockaddr_in clientAddr, servAddr;   char line[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   servAddr.sin_port = htons(SERVER_PORT); /*   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   inet_aton(SERVER_ADDR, &servAddr.sin_addr);   servAddr.sin_port = htons(SERVER_PORT); */   /*****

UDP Client-Server Program (Command Line) using C

/* udpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include<stdlib.h> #include <unistd.h> #include <string.h> #include <strings.h> #include <sys/time.h> #define MAX_MSG 100  main(int argc,char *argv[] ) {   int i, sd, rc, tempLen, n, portno;   struct sockaddr_in remoteServAddr, tempAddr;   struct sockaddr_in cliAddr;   char server[20];   char msg[MAX_MSG];   if(argc<3)     {          fprintf(stderr,"Usage : %s hostname port\n Press Ctrl+C",argv[0]);           }   portno=atoi(argv[2]);   for(i=0;i<20;i++)      server[i]=argv[1][i];     if(server==NULL)     {           printf("Error, no such host");         }   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&remoteServAddr, sizeof(remoteServAddr));   remoteServA

UDP Client-Server Program using C

/* udpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/time.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define SERVER_PORT 9227  main( ) {   int sd, rc, tempLen, n;   struct sockaddr_in cliAddr, remoteServAddr, tempAddr;   char msg[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&remoteServAddr, sizeof(remoteServAddr));   remoteServAddr.sin_family = AF_INET;   remoteServAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   remoteServAddr.sin_port = htons(SERVER_PORT);   /**************************/   /* create datagram socket */   /**************************/   sd = socket(AF_INET,SOCK_DGRAM,0);   printf("successfully created datagram socket\n");   do {     /

TCP Echo Server - Echo Client Program using C

/* tcpechoServer.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <strings.h> #include <string.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define SERVER_PORT 3786  main ( ) {   int sd, newSd, cliLen, n;   struct sockaddr_in cliAddr, servAddr;   char line[MAX_MSG], line_r[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   servAddr.sin_port = htons(SERVER_PORT); /*   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   inet_aton(SERVER_ADDR, &servAddr.sin_addr);   servAddr.sin_port = htons(SERVER_PORT); */   /************************/   /* create strea