0
votes

Why is this unicast not being received by the client? I've been hammering away at this for hours. Grr.. ;(

  • I'm sending price ticks in the form nn.dd; e.g., 42.23, 93.75. Just 5 bytes for test purposes.
  • The server's (tick generator) IP address is 192.168.1.101
  • The client's address is 192.168.1.102
  • The return value of the sendto command is 5, as it should be.
  • I can successfully ping in both directions: client from server, and server from client; about a 3ms response time.
  • Both machines are on the same network.
  • The prices are being generated and sent as shown below
  • A client process on the same machine, (with same IP address of 192.168.1.101) in a different terminal, works fine, as shown in snippet 2 below.
  • The complete code is included in snippet 3 and 4
  • Linksys WRT54GL v1.1 with stock BIOS
  • This code is a modified version of Beej's UDP example here: http://beej.us/guide/bgnet/output/html/multipage/clientserver.html#datagram
  • NOTE: Start server with tickGenerator 192.168.1.101 helloWorld (of course use your IP address(es)). The "helloWorld" from Beej's example will be ignored; I've hacked in a random price generator.

Tick generator output: properly generating tick data from 192.168.1.101:

Price unicasted: 49.58
Price unicasted: 50.00
Price unicasted: 50.24
...

Client in separate terminal window on the same machine (also from from 192.168.1.101) properly receives multicast:

listener: waiting to recvfrom...
listener: got packet from 192.168.1.101
listener: packet is 5 bytes long
listener: packet contains "49.58"
listener: got packet from 192.168.1.101
listener: packet is 5 bytes long
listener: packet contains "50.00"
listener: got packet from 192.168.1.101
listener: packet is 5 bytes long
listener: packet contains "50.24"
...

Separate machine at 192.168.1.102 just hangs...

Tick generator:

/*
 ** talker.c -- a datagram "client" demo
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <chrono>
#include <thread>
#include <iostream>

#define SERVERPORT "4950"    // the port users will be connecting to

void reverse(char *str, int len) {
    int i=0, j=len-1, temp;
    while (i<j)
    {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++; j--;
    }
}
int intToStr(int x, char str[], int d)
{
    int i = 0;
    while (x) {
        str[i++] = (x%10) + '0';
        x = x/10;
    }
    while (i < d)
        str[i++] = '0';

    reverse(str, i);
    str[i] = '\0';
    return i;
}
void ftoa(float n, char *res, int afterpoint) {
    int ipart = (int)n;
    float fpart = n - (float)ipart;
    int i = intToStr(ipart, res, 0);
    if (afterpoint != 0) {
        res[i] = '.';
        fpart = fpart * 100;
        intToStr((int)fpart, res + i + 1, afterpoint);
    }
}
float randPrice() {
    int b;
    float d;
    b = 4950 + rand() % 100 + 1;
    d = (float)b/100;
    return d;
}
int main(int argc, char *argv[])
{
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    int numbytes;

    if (argc != 3) {
        fprintf(stderr,"usage: talker hostname message\n");
        exit(1);
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;

    if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and make a socket
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                             p->ai_protocol)) == -1) {
            perror("talker: socket");
            continue;
        }

        break;
    }

    if (p == NULL) {
        fprintf(stderr, "talker: failed to create socket\n");
        return 2;
    }

    char myPrice[6];
    float f;

    while(1) {
        f = randPrice();
        ftoa(f, myPrice, 2); // Convert price to string
        std::cout << "Price multicasted: " << myPrice << std::endl;
        if ((numbytes = sendto(sockfd, myPrice, strlen(myPrice), 0, p->ai_addr, p->ai_addrlen)) == -1) {
            perror("talker: sendto");
            exit(1);
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }

    freeaddrinfo(servinfo);

    printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);
    close(sockfd);

    return 0;
}

Tick listener:

/*
 ** listener.c -- a datagram sockets "server" demo
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MYPORT "4950"    // the port users will be connecting to

#define MAXBUFLEN 100

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }

    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    int numbytes;
    struct sockaddr_storage their_addr;
    char buf[MAXBUFLEN];
    socklen_t addr_len;
    char s[INET6_ADDRSTRLEN];

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                             p->ai_protocol)) == -1) {
            perror("listener: socket");
            continue;
        }

        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("listener: bind");
            continue;
        }

        break;
    }

    if (p == NULL) {
        fprintf(stderr, "listener: failed to bind socket\n");
        return 2;
    }

    freeaddrinfo(servinfo);

    printf("listener: waiting to recvfrom...\n");

    addr_len = sizeof their_addr;

    while(1) {
        if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
                                 (struct sockaddr *)&their_addr, &addr_len)) == -1) {
            perror("recvfrom");
            exit(1);
        }

        printf("listener: got packet from %s\n",
               inet_ntop(their_addr.ss_family,
                         get_in_addr((struct sockaddr *)&their_addr),
                         s, sizeof s));
        printf("listener: packet is %d bytes long\n", numbytes);
        buf[numbytes] = '\0';
        printf("listener: packet contains \"%s\"\n", buf);
    }

    close(sockfd);

    return 0;
}
1
Maybe your network switch doesn't support multicast forwarding?Barmar
I don't see any attempt to use multicast here. How exactly are you calling the sender? Also, are you able to send between two machines with unicast?dbush
Yes, good point; I meant UDP datagram; this is essentially a unicast. The next step is to make it a multicast, so that many market participants can subscribe to the data feed :). Updated.kmiklas
Could be a firewall issue. Is iptables enabled? You can stop it by running /etc/init.d/iptables stopdbush
@dbush neither iptables nor ip6tables are running. I did a tcpdump host 192.168.1.100 and 192.168.1.101 and got 22:46:43.284008 IP 192.168.1.100.49386 > 192.168.1.101.4950: UDP, length 5, followed by...kmiklas

1 Answers

0
votes

The problem is the bind in the listener. You should bind to INADDR_ANY rather than a specific IP address.

It would also be interesting to see what actual IP address the sender is sending to.