36 thoughts on “TCP/IP Programming in C

  1. Socket Programming User Datagram Protocol (UDP):

    -Please have look at [TCP Programming in C – youtube.com/vinodthebest]

    -Server & Client Concept.

    -Major steps.

    -Server:

    => Create a socket with the socket()

    => Bind the socket to an address using the bind().

    => Send and receive data, use the recvfrom() and sendto() system calls.

    -Client:

    => Create a socket with the socket()

    => Send and receive data, use the recvfrom() and sendto() system calls.

    -Server coding.

    -Client coding.

    -Running.

    Coding

    Server:

    /* Creates a datagram server. The port number is passed as an argument. This server runs forever */

    #include

    #include

    #include

    #include

    #include

    void error(char *msg)

    {

    perror(msg);

    exit(0);

    }

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

    {

    int sock, length, fromlen, n;

    struct sockaddr_in server;

    struct sockaddr_in from;

    char buf[1024];

    if (argc < 2)

    {

    fprintf(stderr, "ERROR, no port providedn");

    exit(0);

    }

    sock=socket(AF_INET, SOCK_DGRAM, 0);

    if (sock < 0)

    {

    error("Opening socket");

    }

    length = sizeof(server);

    bzero(&server,length);

    server.sin_family=AF_INET;

    server.sin_addr.s_addr=INADDR_ANY;

    server.sin_port=htons(atoi(argv[1]));

    if (bind(sock,(struct sockaddr *)&server,length)<0)

    {

    error("binding");

    }

    fromlen = sizeof(struct sockaddr_in);

    while (1)

    {

    n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);

    if (n < 0)

    {

    error("recvfrom");

    }

    write(1,"Received a datagram: ",21);

    write(1,buf,n);

    n = sendto(sock,"Got your messagen",17,

    0,(struct sockaddr *)&from,fromlen);

    if (n < 0)

    {

    error("sendto");

    }

    }

    }

    Client:

    /* UDP client in the internet domain */

    #include

    #include

    #include

    #include

    #include

    #include

    void error(char *);

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

    {

    int sock, length, n;

    struct sockaddr_in server, from;

    struct hostent *hp;

    char buffer[256];

    if (argc != 3)

    {

    printf("Usage: server portn");

    exit(1);

    }

    sock= socket(AF_INET, SOCK_DGRAM, 0);

    if (sock h_addr, (char *)&server.sin_addr,hp->h_length);

    server.sin_port = htons(atoi(argv[2]));

    length=sizeof(struct sockaddr_in);

    printf("Please enter the message: ");

    bzero(buffer,256);

    fgets(buffer,255,stdin);

    n=sendto(sock,buffer,strlen(buffer),0,&server,length);

    if (n < 0)

    {

    error("Sendto");

    }

    n = recvfrom(sock,buffer,256,0,&from, &length);

    if (n < 0)

    {

    error("recvfrom");

    }

    write(1,"Got an ack: ",12);

    write(1,buffer,n);

    }

    void error(char *msg)

    {

    perror(msg);

    exit(0);

    }

    Socket Programming:

    -Minimum Requirement.

    -Linux OS [Lib. Header Files]

    -IP & Port No.

    -Server & Client Concept.

    -Major steps.

    -Server:

    =Create a socket with the socket()

    =Bind the socket to an address using the bind().

    =Listen for connections with the listen().

    =Accept a connection with the accept().

    =Send and receive data, use the read() and write() system calls.

    -Client:

    =Create a socket with the socket()

    =Connect the socket to the address of the server using the connect() system call.

    =Send and receive data, use the read() and write() system calls.

    -Server coding.

    -Client coding.

    -Running.

    Coding:

    Server :

    /* A simple server in the internet domain using TCP the port number is passed as an argument */

    #include

    #include

    #include

    #include

    void error(char *msg)

    {

    perror(msg);

    exit(1);

    }

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

    {

    int sockfd, newsockfd, portno, clilen;

    char buffer[256];

    struct sockaddr_in serv_addr, cli_addr;

    int n;

    if (argc < 2)

    {

    fprintf(stderr,"ERROR, no port providedn");

    exit(1);

    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0)

    {

    error("ERROR opening socket");

    }

    bzero((char *) &serv_addr, sizeof(serv_addr));

    portno = atoi(argv[1]);

    serv_addr.sin_family = AF_INET;

    serv_addr.sin_addr.s_addr = INADDR_ANY;

    serv_addr.sin_port = htons(portno);

    if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)

    {

    error("ERROR on binding");

    }

    listen(sockfd,5);

    clilen = sizeof(cli_addr);

    newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

    if (newsockfd < 0)

    {

    error("ERROR on accept");

    }

    bzero(buffer,256);

    n = read(newsockfd,buffer,255);

    if (n < 0)

    {

    error("ERROR reading from socket");

    }

    printf("Here is the message: %sn",buffer);

    n = write(newsockfd,"I got your message",18);

    if (n < 0)

    {

    error("ERROR writing to socket");

    }

    return 0;

    }

    Client :

    #include

    #include

    #include

    #include

    #include

    void error(char *msg)

    {

    perror(msg);

    exit(0);

    }

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

    {

    int sockfd, portno, n;

    struct sockaddr_in serv_addr;

    struct hostent *server;

    char buffer[256];

    if (argc < 3)

    {

    fprintf(stderr,"usage %s hostname portn", argv[0]);

    exit(0);

    }

    portno = atoi(argv[2]);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);

    serv_addr.sin_port = htons(portno);

    if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)

    {

    error("ERROR connecting");

    }

    printf("Please enter the message: ");

    bzero(buffer,256);

    fgets(buffer,255,stdin);

    n = write(sockfd,buffer,strlen(buffer));

    if (n < 0)

    {

    error("ERROR writing to socket");

    }

    bzero(buffer,256);

    n = read(sockfd,buffer,255);

    if (n < 0)

    {

    error("ERROR reading from socket");

    }

    printf("%sn",buffer);

    return 0;

    }

  2. Hello, I have made my a program very similar to this. Just like yours, my programs client can send data and there server can receive it but I don't know how we can have the server not only receive from client but also send data back.

  3. What should I do to send a message from the server to the client side as soon as client connected to the server…?let's say for an example server should send a welcome message to each and every client that connect to the server without getting any message from the client. How to implement that one?

  4. how do we modify the code such that we can apply multi threading using pthreads and the server can process and respond to atleast 2 HTTP requests concurrently? can anyone comment down the code for such an operation?

  5. sir, our teacher have given us a work to send one file to another system using FTP and we are also using ubuntu and connected to same lan. where should i search for it( we need to use c language)

  6. With respect sir,
    Improve your presentation so that evey function,it's parameters explained …And don't worry about time…Go slow and clear in words so that everybody should understand everything about your tutorial…

  7. Why is my compiler replace the output for example i compile client.c , got that a.out, then i compile the serve.c it replace the a.out and use the name a.out again please help me sir

Leave a Reply

Your email address will not be published. Required fields are marked *