Thread: how to install socket server/client???

  1. #1
    Registered User
    Join Date
    Mar 2015
    Location
    86A Vườn Lài, P.Tân Thành, Q.Tân Phú
    Posts
    1

    how to install socket server/client???

    Server
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <stdlib.h>
    
    /**
     * ham doc data tu clien va upper string ky tu
     */
    void read_from_client(int mysock)
    {
        char buff[1024];
        memset(buff, 0, sizeof(buff));
        int rval;
        if((rval = recv(mysock, buff, sizeof(buff), 0))<0)
                        perror("doc stream that bai");
                    else
                        if(rval==0)
                            printf("Ending connection\n");
                        else
                        {
                            printf("tin nhan duoc: %s\n", buff);
                            int n = strlen(buff),i;
                            for(i=0;i<n;i++)
                            {
                                buff[i] = toupper(buff[i]);
                            }
                            printf("tin gui tro lai: %s\n", buff);
                        }
    
                    printf("got the message (rval = %d)\n",rval);
                    if(send(mysock,buff,sizeof(buff),0)<0)
                    {
                        perror("gui nguoc that bai");
                        close(mysock);
                        exit(1);
                    }
    }
    
    //ham tao ket noi
    int make_new_connection(int sock)
    {
        int mysock;
        mysock = accept(sock,(struct sockaddr *)0,0);
        return mysock;
    }
    
    
    
    /**
     * ham tao socket
     */
    int make_socket()
    {
        int sock;
        struct sockaddr_in server;
        /*Tao socket*/
            sock = socket(AF_INET, SOCK_STREAM, 0);
            if(sock<0){
                perror("Tao socket that bai !");
                exit(1);
            }
    
        /*Tao thong tin cho socket*/
                server.sin_family = AF_INET;
                server.sin_addr.s_addr = INADDR_ANY;
                server.sin_port = htons(5000);
    
        /*tai su dung port*/
    
                int reuse =1;
                setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (int *) &reuse, sizeof(reuse));
    
    
    
        /*Call bind*/
                if(bind(sock, (struct sockaddr *)&server, sizeof(server)))
                {
                        perror("bind that bai");
                        exit(1);
                }
        return sock;
    }
    
    
    int main(int argc, char *argv[])
    {
    
    
        /*variables*/
        int sock ;
        int mysock;
    
    
        sock = make_socket();
        /*listen*/
                listen(sock, 5);
    
        /*accept*/
        do{
            if((mysock = make_new_connection(sock))<0)
            {
                perror("ket noi that bai");
                continue;
            }
            else
            {
                read_from_client(mysock);
                close(mysock);
            }
    
        }while(1);
    
        return 0;
    }
    Client
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int make_socket(char *addr)
    {
        int sock;
        struct sockaddr_in server;
        struct hostent *hp;
        sock = socket(AF_INET, SOCK_STREAM, 0);
        if(sock<0)
        {
            perror("tao socket that bai");
            exit(1);
        }
        server.sin_family = AF_INET;
    
        /*lay thong tin host*/
        hp = gethostbyname(addr);
        if(hp==0)
        {
            perror("gethostbyname failed");
            close(sock);
            exit(1);
        }
        memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
        server.sin_port = htons(5000);
    
    
        /*ket noi server*/
        if(connect(sock, (struct sockaddr *)&server, sizeof(server))<0)
            {
                perror("ket noi that bai");
                exit(1);
            }
    
    
        return sock;
    }
    
    
    
    void gui_nhan_data(int sock)
    {
        char buff[1024];
        int rval;
        char *DATA = NULL;
        size_t thesize;
    
    
        /*nhap du lieu gui*/
            while(DATA == NULL)
            {
                printf("nhap:");
                getline(&DATA,&thesize,stdin);
                printf("your input is: %s\n", DATA);
                printf("size = %d, sizeof = %d\n", thesize,strlen(DATA));
            }
    
    
        /*send data*/
    
                if(send(sock,DATA,strlen(DATA), 0)<0)
                        {
                            perror("gui that bai");
                            close(sock);
                            exit(1);
                        }
                        printf("tin da gui: %s\n", DATA);
    
    
        /*receive data*/
                memset(buff, 0, sizeof(buff));
                if((rval = recv(sock,buff,sizeof(buff),0))<0)
                    perror("doc lai that bai");
                else
                    if(rval==0)
                    {
                        printf("khong co chuoi nao duoc gui");
                    }
                    else
                    {
                        printf("tin nhan lai: %s\n", buff);
                    }
    }
    
    int main(int argc, char *argv[])
    {
        /*khai bao bien*/
    
        int sock;
    
    
        if(argc < 2)
        {
            printf("chua nhap may chu\n");
            return(EXIT_FAILURE);
        }
    
        if((sock = make_socket(argv[1]))<0)
        {
            printf("tao ket noi that bai !");
        }
        else
        {
            gui_nhan_data(sock);
        }
    
        close(sock);
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I guess you open two console windows.

    In one, you type
    ./server

    In the other, you type
    ./client 127.0.0.1


    The bugs in your code can be fixed later.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Salem, rhianna is just a bot, using text from an existing post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket ( server client Get pid )
    By Syd in forum C Programming
    Replies: 0
    Last Post: 02-15-2011, 08:00 AM
  2. Raw Socket Client and Server Communication Problem
    By rplumii in forum C Programming
    Replies: 6
    Last Post: 02-10-2011, 07:02 AM
  3. C-Windows-Socket-(Client/Server)
    By EyesOnly in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-06-2009, 11:12 AM
  4. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM
  5. Code for Client/Server Socket
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2002, 09:30 AM