CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2014
    Posts
    1

    Data exchange between Clients in UDP

    Hello

    Here the program echos the same messages which was sent by the client to the the same client. I want to interchange the data between clients. for example if two clients are client0, client1. what ever client 1 sends should be echoed to client 0 and similarly what ever client 0 sends should be echoed to client 1. Thank you.


    Code:
    #include <stdio.h>
    #include <string.h>   //strlen
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>   //close
    #include <arpa/inet.h>    //close
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
    
    #define TRUE   1
    #define FALSE  0
    #define PORT 1901
    
    int main(int argc , char *argv[])
    {
        int opt = TRUE;
        int master_socket , addrlen , new_socket , client_socket[2] , max_clients = 2 , activity,     i , valread , sd;
        int max_sd;
        int port_array[30];
        struct sockaddr_in address;
    
        char buffer[1025];  //data buffer of 1K
    
    //set of socket descriptors
    fd_set readfds;
    
    //a message
    char *message = "ECHO Daemon v1.0 \r\n";
    
    //initialise all client_socket[] to 0 so not checked
    for (i = 0; i < max_clients; i++) 
    {
        client_socket[i] = 0;
    
    }
    
    //create a master socket
    if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0) 
    {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
    
    //set master socket to allow multiple connections , this is just a good habit, it will work without this
    if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 )
    {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    
    //type of socket created
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.37.129");
    address.sin_port = htons( PORT );
    
    //bind the socket to localhost port 8888
    if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0) 
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    printf("Listener on port %d \n", PORT);
    
    //try to specify maximum of 3 pending connections for the master socket
    if (listen(master_socket, 3) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    
    //accept the incoming connection
    addrlen = sizeof(address);
    puts("Waiting for connections ...");
    
    while(TRUE) 
    {
        //clear the socket set
        FD_ZERO(&readfds);
    
        //add master socket to set
        FD_SET(master_socket, &readfds);
        max_sd = master_socket;
    
        //add child sockets to set
        for ( i = 0 ; i < max_clients ; i++) 
        {
            //socket descriptor
            sd = client_socket[i];
    
            //if valid socket descriptor then add to read list
            if(sd > 0)
                FD_SET( sd , &readfds);
    
            //highest file descriptor number, need it for the select function
            if(sd > max_sd)
                max_sd = sd;
        }
    
        //wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
        activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);
    
        if ((activity < 0) && (errno!=EINTR)) 
        {
            printf("select error");
        }
    
        //If something happened on the master socket , then its an incoming connection
        if (FD_ISSET(master_socket, &readfds)) 
        {
            if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
            {
                perror("accept");
                exit(EXIT_FAILURE);
            }
    
            //inform user of socket number - used in send and receive commands
            printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
    
            port_array[i] = ntohs(address.sin_port);
    
            //send new connection greeting message
            if( send(new_socket, message, strlen(message), 0) != strlen(message) ) 
            {
                perror("send");
            }
    
            puts("Welcome message sent successfully");
    
            //add new socket to array of sockets
            for (i = 0; i < max_clients; i++) 
            {
                //if position is empty
                if( client_socket[i] == 0 )
                {
                    client_socket[i] = new_socket;
                    printf("Adding to list of sockets as %d\n" , i);
    
                    break;
                }
            }
        }
    
        //else its some IO operation on some other socket :)
        for (i = 0; i < max_clients; i++) 
        {
            sd = client_socket[i];
    
            if (FD_ISSET( sd , &readfds)) 
            {
                //Check if it was for closing , and also read the incoming message
                if ((valread = read( sd , buffer, 1024)) == 0)
                {
                    //Somebody disconnected , get his details and print
                    getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&addrlen);
                    printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
    
                    //Close the socket and mark as 0 in list for reuse
                    close( sd );
                    client_socket[i] = 0;
                }
    
                //Echo back the message that came in
                else
                {
    
               if (client_socket[0]==port_array[0]) {
                //client 0 talking send to client 1
                printf("Sedning message to client 1\n");
                if (send(client_socket[1] , buffer , strlen(buffer) , 0 )==-1)
                {
    
                    err("send()");
                }
    
                }else {
                //client 1 talking send to client 0
                printf("Sending message to client 0\n");
    
                if (send(client_socket[0] , buffer , strlen(buffer) , 0 )==-1)
                {
    
                   err("send()");
    
                }
    
    
                }
            }
        }
    }
    
    
    } 
        return 0;
    }

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Data exchange between Clients in UDP

    This
    Code:
    if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
    creates a TCP socket, not an UDP one. Later on, you bind that socket to an address and listen on it.
    UDP sockets work a bit different. They are said to be connectionless, so you simply read/write on a UDP socket. I don't want to go into detail here.

    This comment
    Code:
    //set master socket to allow multiple connections , this is just a good habit, it will work without this
    if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 )
    is not exactly true. It allows for a server socket to be reused even if it is in a "waiting" status, e.g. after a server crash and immediate restart of the server.


    What exactly is your problem?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured