Simple C Client/Server Codes For Socket Programing in Linux



Here is the codes for client/server program. And note these c sockets codes are in its simplest form without any error handling function or routine.

For TCP Sockets : 

Server.c
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<string.h>  
 #include<unistd.h>  
 #include<arpa/inet.h>  
 #include<sys/types.h>  
 #include<netinet/in.h>  
 #include<sys/socket.h>  
   
 #define PORTNUM 9999  
   
 int main(int argc, char *argv[])  
 {  
     char *msg = "Hello world!\n";  
   
     struct sockaddr_in clnt; //socket info for client machine  
     struct sockaddr_in serv; //socket info for our server   
     int mysocket;  
   
     socklen_t socksize = sizeof(struct sockaddr_in);  
   
     memset(&serv, 0, sizeof(serv));  
   
     serv.sin_family = AF_INET;  
     serv.sin_addr.s_addr = htonl(INADDR_ANY);  
     serv.sin_port = htons(PORTNUM);  
       
     mysocket = socket(AF_INET, SOCK_STREAM, 0);  
   
     bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));  
     // bind serv info to mysocket   
   
     listen(mysocket, 1);  
     int consocket = accept(mysocket, (struct sockaddr *)&clnt, &socksize);  
   
     while(consocket)  
     {  
         printf("Incoming connection from %s - sending welcome\n", inet_ntoa(clnt.sin_addr));  
   
         send(consocket, msg, strlen(msg), 0);  
         close(consocket);  
         consocket = accept(mysocket, (struct sockaddr *)&clnt, &socksize);  
     }  
   
     close(mysocket);  
     return EXIT_SUCCESS;  
 }  

Client.c

 #include<stdio.h>  
 #include<stdlib.h>  
 #include<string.h>  
 #include<unistd.h>  
 #include<arpa/inet.h>  
 #include<sys/types.h>  
 #include<netinet/in.h>  
 #include<sys/socket.h>  
   
 #define MAXRECVLEN 500  
 #define PORTNUM 9999  
   
   
 int main(int aregc, char *argv[])  
 {  
     char buffer[MAXRECVLEN]; // +1 for null terminator  
     int len, mysocket;  
     struct sockaddr_in dest; // for access in sockaddr_in struct  
   
     mysocket = socket(AF_INET, SOCK_STREAM, 0);  
   
     memset(&dest, 0, sizeof(dest)); // zero the sockaddr_in struct  
   
     dest.sin_family = AF_INET;  
     dest.sin_addr.s_addr = htonl(INADDR_ANY);  
     dest.sin_port = htons(PORTNUM);  
   
     connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));  
   
     len = recv(mysocket, buffer, MAXRECVLEN - 1, 0);  
   
     buffer[len] = '\0';  
   
     printf("Received bytes : %s. \n", buffer);  
   
     close(mysocket);  
     return EXIT_SUCCESS;  
 }  

Now compile the both codes with gcc and run it. I run these programs in Ubuntu 11.10.

First compile it.

$ gcc -o server server.c  
$ gcc -o client client.c  

but remember always run server first and then client.



For UDP Sockets :

udp_server.c
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
 #include<errno.h>  
 #include<string.h>  
 #include<sys/types.h>  
 #include<sys/socket.h>  
 #include<netinet/in.h>  
 #include<arpa/inet.h>  
 #include<netdb.h>  
   
 #define MYPORT 9999  
 #define MAXBUFLEN 100  
   
 int main(int argc, char *argv[])  
 {  
     struct sockaddr_in clnt;  
     struct sockaddr_in serv;  
     char BUFF[MAXBUFLEN];  
   
     int mysocket;  
     int numbytes;  
   
     memset(&serv, 0, sizeof(serv));  
   
     serv.sin_family    = AF_INET;  
     serv.sin_addr.s_addr = htonl(INADDR_ANY);  
     serv.sin_port = htons(MYPORT);  
   
     mysocket = socket(AF_INET, SOCK_DGRAM, 0);  
   
     bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));  
   
     int clnt_len = sizeof(clnt);  
   
     while(1){  
         if((numbytes = recvfrom(mysocket, BUFF, MAXBUFLEN-1, 0, (struct sockaddr *)&clnt, &clnt_len)) == -1) {  
             perror("died in recvfrom.\n");  
             exit(1);  
         }  
   
         BUFF[numbytes] = '\0';  
         printf("Recevied Packet from %s:%d\nData: %s\n\n", inet_ntoa(clnt.sin_addr), ntohs(clnt.sin_port), BUFF);  
     }  
 close(mysocket);  
 return 0;  
 }  
   

udp_client.c

 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
 #include<errno.h>  
 #include<string.h>  
 #include<sys/types.h>  
 #include<sys/socket.h>  
 #include<netinet/in.h>  
 #include<arpa/inet.h>  
 #include<netdb.h>  
   
 int main(int argc, char *argv[])  
 {  
     if(argc != 4){  
         printf("Usage : %s <IP> <PORT> <message>\n", argv[0]);  
         return 1;  
     }  
   
     int mysocket, PORT;  
     struct sockaddr_in dest;  
     socklen_t dest_len = sizeof(dest);  
       
     PORT = atoi(argv[2]);  
   
     memset(&dest, 0, sizeof(dest));  
   
     dest.sin_family = AF_INET;  
     dest.sin_addr.s_addr = inet_addr(argv[1]);  
     dest.sin_port = htons(PORT);  
   
     mysocket = socket(AF_INET, SOCK_DGRAM, 0);  
   
     sendto(mysocket, argv[3], strlen(argv[3]), 0, (struct sockaddr *)&dest, dest_len);  
       
     printf("Sent bytes : %s\n", argv[3]);  
     close(mysocket);  
     return 0;  
 }  
   

Compile it and run it.


So this is it. thanks for visiting. and for detailed explanations in Linux/Unix Socket Programing checkout the below given link.

Beej's Guide To Network Programing : http://beej.us/guide/bgnet/