Skip to main content

Program For Implementing the TCP client and Server


     C program to implemet TCP Server

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
int main(void)
{
    int s, len;
    char buf[BUFSIZ];
    struct sockaddr_in remote_addr;
    memset(&remote_addr, 0, sizeof(remote_addr));
    remote_addr.sin_family = AF_INET;
    remote_addr.sin_addr.s_addr = inet_addr(“127.0.0.1″);
    remote_addr.sin_port = htons(8000);
    if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf(“Error\n”);
        return -1;
    }
    if( connect(s, (struct sockaddr*)&remote_addr, sizeof(struct sockaddr)) < 0 )
    {
        printf(“Error\n”);
        return -1;
    }
    printf(“Connection established\n”);
    len = recv(s, buf, BUFSIZ, 0);
    buf[len] = ”;
    printf(“%s\n”, buf);
    while(1)
    {
        printf(“Enter text: “);
        scanf(“%s%”, buf);
        if( !strcmp(buf, “quit”) )
        break;
        len = send(s, buf, strlen(buf), 0);
        len = recv(s, buf, BUFSIZ, 0);
        buf[len] = ”;
        printf(“From server: %s\n”, buf);
    }
    close(s);
    return 0;
}


/*  C program to implemet TCP Client*/

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
 
int main(void)
{
        char buf[BUFSIZ], ubuf[BUFSIZ];
        int s, fd, len;
        int sin_size;
        struct sockaddr_in my_addr;
        struct sockaddr_in remote_addr;
 
        memset(&my_addr, 0, sizeof(my_addr));
        my_addr.sin_family = AF_INET;
        my_addr.sin_addr.s_addr = INADDR_ANY;
        my_addr.sin_port = htons(8000);
 
        if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
               printf("Error\n");
               return -1;
        }
 
        if( bind(s, (struct sockaddr*)&my_addr, sizeof(struct sockaddr))  < 0)
        {
               printf("Error\n");
               return -1;
        }
 
        listen(s, 6);
 
        sin_size = sizeof(struct sockaddr_in);
        if( (fd = accept(s, (struct sockaddr*)&remote_addr, &sin_size)) < 0 )
        {
               buf[len] = '';
               buf[0] = toupper(buf[0]);
               send(fd, buf, len, 0);
        }
 
        close(fd);
        close(s);
        return 0;
}

Comments

Popular posts from this blog

Multiple Inheritance

Multiple Inheritance In multiple inheritance a class can access the property of more than one class. This inheritance is use for multiple accessing of properties. In multiple inheritance ambiguty is  occured when same name function is declare in both class and the object is confuse with function is called.  syntax − class derived-class: access baseA, access baseB.... Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following  example − #include <iostream.h> #include <conio.h>  // Base Class class Shape  {    public:       void getwidth(int w) {          width = w;       }       void getheight(int h) {          height = h;       }           prot...

CODD RULES of RDBMS

Codd’s rules for RDBMS (i)                 The information rule. (ii)              Guaranteed access of   data (iii)            Systematic treatment of null value. (iv)            On line DB support. (v)              Comprehensive sup-language rule. (vi)            The view update rule. (vii)         Insert update rule. (viii)       Physical data independence. (ix)            Logical data independence. (x)              Integrity rule (xi)  ...