08 November, 2010

C program in UNIX to for TCP communication through sockets

Here is a C program in  Unix that introduces the basics of tcp communication by creating sockets. Here the cleint program and server programs communicates by passing messages to the sockets. 

Inorder to run this program store each program in  separate C files and compile it. Inorder to make it working run the programs simultaneosly in two terminals. 

tcp client program
/*program to create tcp client*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<limits.h>
#include<fcntl.h>
#include<sys/shm.h>
main()
{
    int fd1,sid,f2,f3,f4,f1;
    char msg1[25],msg2[25];
   
    struct sockaddr_in client,server;
    client.sin_family=AF_INET;
    client.sin_port=htons(0);
    client.sin_addr.s_addr=htonl(INADDR_ANY);
    server.sin_family=AF_INET;
    server.sin_port=htons(1235); //please change this number while copying....
    server.sin_addr.s_addr=htonl(INADDR_ANY);
    sid=socket(AF_INET,SOCK_STREAM,0);
    if(sid<0)
        printf("socket not created\n");//please change this number while copying....
    else
    {
        printf("socket created\n");
        f1=bind(sid,(struct sockaddr*)&client,sizeof(struct sockaddr_in));
        if(f1==-1)
            printf("socket not bind\n");
        else
        {
            printf("socket binded\n");           
            f2=connect(sid,(struct sockaddr*)&server,sizeof(struct sockaddr_in));
            if(f2==-1)
                printf("connection request not send\n");
            else
            {               
                printf("\n\nconnection established\n\n");
                printf("Enter msg\n");
                scanf("%s",msg1);
                send(sid,msg1,sizeof(msg1),0);
                printf("msg send\n");
                recv(sid,msg2,sizeof(msg2),0);
                printf("received\n");
                printf("msg received is:%s\n",msg2);
            }
        }
    }
}







tcp server program

/*program to create tcp server*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
int main()
{
    int sid,f1,f2,f3,f4,f5,p;
    char msg1[25],msg2[25];
    struct sockaddr_in client,server;
    server.sin_family=AF_INET;
    server.sin_port=htons(1235);  //please change this number while copying....
    server.sin_addr.s_addr=htonl(INADDR_ANY);
    sid=socket(AF_INET,SOCK_STREAM,0);
    if(sid<0)
        printf("socket not created\n");
    else
    {
        printf("socket created\n");
        f1=bind(sid,(struct sockaddr*)&server,sizeof(struct sockaddr_in));
        if(f1<0)
            printf("socket not bind\n");
        else
        {
            printf("socket bind\n");
            f2=listen(sid,5);
            p=sizeof(struct sockaddr_in);
            f3=accept(sid,(struct sockaddr*)&client,&p);
            if(f3<0)
                printf("connection not accepted\n");
            else
            {
                printf("connection accepted\n");
                recv(f3,msg1,sizeof(msg1),0);
                printf("Message recieved from client:%s\n",msg1);
                printf("Enter the msg\n");
                scanf("%s",msg2);
                f5=send(f3,msg2,sizeof(msg2),0);
                printf("Message send\n");
            }
        }
    }
    return 1;
}

2 comments:

Anonymous said...

hi..
This blog post is great.. Is it possible for u to just put in some comment lines for making the code a bit more clear..
thanx in advance..

jiNtU jAcoB on Thursday, May 12, 2011 said...

thank you for the comment... i am working on it...

Post a Comment

speak out... itz your time !!!

 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger