Thread: Changing UDP communication from local machine to remote machine.

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    74

    Changing UDP communication from local machine to remote machine.

    Hi,

    I'm hoping someone can help me. My brain feels fried from trying to figure this out.

    I have two programs, client.c and server.c that will connect via UDP. The sever sends a code and the client receives it. All on a local machine. I would like to move the client to a remote machine and achieve the same functionality.

    server.c
    Code:
     // Declare includes.
    #include <arpa/inet.h>
    #include <errno.h>
    #include <libgen.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <termios.h>
    #include <unistd.h>
    
    // Declare defines.
    #define PORT_1           50036
    #define PORT_2           50037
    #define PORT_3           50038
    #define PORT_NAME_SIZE       6
    
    // Declare defines.
    #define PROGRAM_NAME_SIZE   50
    
    // Declare stuctures.
    typedef struct _ports
    {
        int fd;
        char name[PORT_NAME_SIZE + 1];
        struct sockaddr_in serveraddr;
    } PORTS;
    
    // Declare global variables.
    char program_name[PROGRAM_NAME_SIZE + 1] = {0};
    
    // Declare function prototypes.
    void open_port (PORTS *port, int port_num);
    PORTS * open_ports (void);
    void run_announcer (PORTS *ports);
    void start_process (void);
    
    int main (int argc, char *argv[])
    {
    // Get program name for error reporting.
        strcpy(program_name, basename(argv[0]));
    
    // Check number of arguments.
        if(argc != 1)
        {
            fprintf(stderr, "Usage: %s\n", program_name);
            exit(EXIT_FAILURE);
        }
    
    // Start the process.
        start_process();
    
    // Exit cleanly.
        exit(EXIT_SUCCESS);
    }
    
    void start_process (void)
    {
    // Declare variables.
        PORTS *ports = NULL;
    
    // Open the outgoing ports.
        ports = open_ports();
    
    // Run announcer.
        run_announcer(ports);
    
    // Close all the sockets.
        close(ports[0].fd);
        close(ports[1].fd);
        close(ports[2].fd);
    }
    
    PORTS * open_ports (void)
    {
    // Delcare variables.
        static PORTS ports[3] = {0};
    
    // Name all ports first.
        strcpy(ports[0].name, "PORT_1");
        strcpy(ports[1].name, "PORT_2");
        strcpy(ports[2].name, "PORT_3");
    
    // Open UDP port.
        open_port(&(ports[0]), PORT_1);
    
    // Open UDP port.
        open_port(&(ports[1]), PORT_2);
    
    // Open UDP port.
        open_port(&(ports[2]), PORT_3);
    
    // Return the ports array.
        return(ports);
    }
    
    void open_port (PORTS *port, int port_num)
    {
    // Delcare variables.
    
    // Open socket for UDP operation.
        if((port->fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        {
            fprintf(stderr, "%s: open_port error: socket failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Clear variables.
        memset(&port->serveraddr, 0, sizeof(port->serveraddr));
    // Load server variables.
        port->serveraddr.sin_family = AF_INET;
        port->serveraddr.sin_port = htons(port_num);
    // Set to loopback address '127.0.0.1'.
    //    port->serveraddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        port->serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    }
    
    void run_announcer (PORTS *ports)
    {
    // Delcare variables.
        char code[2] = {0, 0};
    
    // Loop forever.
        while((code[0] = getch()))
        {
    // Send out message.
            if(sendto(ports[0].fd, code, 1, 0, (struct sockaddr *) &ports[0].serveraddr, sizeof(ports[0].serveraddr)) == -1)
            {
                fprintf(stderr, "%s: run_announcer error: sendto failed (%s) (%s)\n", program_name, strerror(errno), ports->name);
                exit(EXIT_FAILURE);
            }
    
            if(sendto(ports[1].fd, code, 1, 0, (struct sockaddr *) &ports[1].serveraddr, sizeof(ports[1].serveraddr)) == -1)
            {
                fprintf(stderr, "%s: run_announcer error: sendto failed (%s) (%s)\n", program_name, strerror(errno), ports->name);
                exit(EXIT_FAILURE);
            }
    
            if(sendto(ports[2].fd, code, 1, 0, (struct sockaddr *) &ports[2].serveraddr, sizeof(ports[2].serveraddr)) == -1)
            {
                fprintf(stderr, "%s: run_announcer error: sendto failed (%s) (%s)\n", program_name, strerror(errno), ports->name);
                exit(EXIT_FAILURE);
            }
    
            printf("message sent\n");
        }
    }
    
    int getch (void)
    {
    // Declare variables.
        int c = {0};
        struct termios oldt = {0};
        struct termios newt = {0};
    
    // Get terminal attributes.
        tcgetattr(STDIN_FILENO, &oldt);
    // Store attributes.
        newt = oldt;
    // Set terminal to non-buffered.
        newt.c_lflag &= ~(ICANON | ECHO);
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    // Get user input.
        c = getchar();
    // Reset terminal attributes.
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    
    // Return character.
        return(c);
    }
    client.c
    Code:
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <unistd.h>
    
    #define PORT   50037
    
    int main( void )
    {
    // Declare variables.
        int i = {0};
        int fd = {0};
        int length = {0};
        char buffer[200] = {0};
        struct sockaddr_in serveraddr = {0};
    
        if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            perror("socket failed");
            return 1;
        }
    
        memset(&serveraddr, 0, sizeof(serveraddr));
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_port = htons(PORT);
    //    serveraddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
            serveraddr.sin_addr.s_addr = inet_addr("192.168.1.103");
    
        if(bind(fd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
        {
            perror( "bind failed" );
            return 1;
        }
    
        while(1)
        {
            length = recvfrom(fd, buffer, sizeof(buffer) - 1, 0, NULL, 0);
    
            if(length < 0)
            {
                perror("recvfrom failed");
                break;
            }
    
            buffer[length] = '\0';
            printf("%d bytes: '%s'\n", length, buffer);
        }
    
    // Close the socket.
        close(fd);
    }
    I get an error on the client of `bind failed: Cannot assign requested address`.

    What am I missing? I belive I'm using the correct struct. Can someone please help?

    Ty,
    Yonut

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    74
    Sorry, no edit button is available to me.

    I don't know why I can't find an example that shows sending data over a network. They all seem to send data to the same machine, even when stating "How to send data over the network".

    How can I send data to another machine?

    Yonut

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    70
    You don't bind a name to a socket on the client side. Just create the socket and then send/receive data. (You should send first so that the server, which is the more permanent process, knows to begin sending. Unless you have the server just broadcasting data wildly, which is valid in UDP.)

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    892

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy file from network to local machine
    By sarojini in forum C++ Programming
    Replies: 0
    Last Post: 02-26-2010, 03:27 AM
  2. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  3. Obtain local machine IP address
    By magic.mike in forum Windows Programming
    Replies: 4
    Last Post: 03-18-2005, 10:40 AM
  4. Get mapped network drives on local machine
    By nvoigt in forum Windows Programming
    Replies: 4
    Last Post: 10-19-2004, 10:15 AM
  5. IDEA: A Slot Machine (aka a fruit machine)
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:13 PM

Tags for this Thread