0
votes

I'm working on a cool project and I need to send 100,000 UDP Packets from a Server to a Client with a Packet-to-Packet Delay of 10ms. The Server is Debian Server with a public IP Address. The Client is an other Debian PC with LTE USB-Modem. The Client has no public IP Address and knows the public IP Address of my Server. My 4G/LTE Provider doesn't provide public IP Adresses to thier clients. I need to build a stable UDP Socket Connection but I'm struggling keeping the socket connection alive.

Can somebody help me?

thx

best regards

/**** CLIENT ****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define PORT 20009

int main()
{
        int sock;
        int size;
        int nbytes, flags;
        int i;
        int a = 0;
        char * cp;
        char buffer[] = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";

        struct sockaddr_in target_pc, me;

        sock = socket(PF_INET,SOCK_DGRAM,0);
        if(sock < 0)
        {
                printf("socket error = %d\n", sock);
                return -1;
        }

        target_pc.sin_family = PF_INET;
        target_pc.sin_port = htons(PORT);

        me.sin_family = PF_INET;
        me.sin_port = htons(0);
        me.sin_addr.s_addr = htonl(INADDR_ANY);
        i = bind(sock, (struct sockaddr *) &me, sizeof(me));

        if( i < 0)
        {
                printf("bind result: %d\n", i);
                return -1;
        }

        nbytes = 200;
        char str_addr[] = "155.55.25.25";
        target_pc.sin_addr.s_addr = inet_addr(&str_addr[0]);

        while(1)
        {
                nbytes = strlen(buffer);
                flags = 0;
                sendto(sock, (char *) buffer, nbytes,flags,(struct sockaddr *)&target_pc,sizeof(target_pc));
                int addrlen = sizeof(target_pc);
                size = recvfrom(sock, buffer, nbytes, flags, (struct sockaddr *)&target_pc,&addrlen);

                if((size > 0) && (size < 200))
                {
                        buffer[size] = '\0';
                        i = puts((char *) buffer);
                }
                printf("%i --- Size: %lu\n", a, sizeof(buffer));
                a = a + 1;

        }
        return 0;
}

Below my Server Code:

/*** SERVER ***/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 20009

void SleepMs(int ms)
{
    usleep(ms*1000); //convert to microseconds
    return;
}

int main()
{
        int sock;
        int size;
        int nbytes, flags;
        socklen_t addrlen;

        int i;

        char buffer[100];
        char buffer2[] = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\0";
        struct sockaddr_in server;
        struct sockaddr_in from;

        sock = socket(AF_INET,SOCK_DGRAM,0);
        if(sock < 0)
        {
                printf("socket error = %d\n", sock);
                return -1;
        }
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = htonl(INADDR_ANY);

        server.sin_port = htons(PORT);

        i = bind(sock, (struct sockaddr *) &server, sizeof(server));

                if( i < 0){
                printf("bind result: %d\n", i);
                return -1;
        }
        else{
                printf("Simple UDP server is ready!\n");
        }

        nbytes = 200;
        flags = 0;

        while(1)
        {
                addrlen = sizeof(from);
                size = recvfrom(sock, buffer, nbytes, flags, (struct sockaddr *)&from, &addrlen);


                if((size > 0) && (size < 200))
                {
                        buffer[size] = '\0';
                        i = puts((char *) buffer);
                }
                printf("\n");
                sock = socket(PF_INET,SOCK_DGRAM,0);
                if(sock < 0)
                {
                        printf("socket error = %d\n", sock);
                        return -1;
                }

                sendto(sock, buffer2, nbytes, flags, (struct sockaddr *)&from,addrlen);
                SleepMs(10); // Packet-to-Packet Delay Time
      }
        return 0;
}
1
Can you possibly use a TCP connection instead? UDP hasn't any protocol-level sense of a connection by which a firewall could be expected to recognize that data coming from your server are responsive to a request from your client. - John Bollinger
There is no such thing as a UDP Socket Connection,, certainly not something that must be "kept alive". Are you just getting a lot of dropped packets or something? - yano
I need to work with UDP. Sorry... Socket Connection for UDP Packets-transmission is right thx - joy.boy
You are leaking filedescriptors (one per message received/sent) in the server code. - wildplasser
Forget about the idea of a "socket connection". There is no such thing, period. The "socket error = -1" is something else and has nothing to do with any imaginary "connections". Just because there is an error doesn't mean that mythical creatures are involved (your approach was as if it was mythos). Please print out the full error message. - Kuba hasn't forgotten Monica

1 Answers

-1
votes

You can have the connection using IP of the server in this case 192.168.1.2

server.sin_addr.s_addr = inet_addr("192.168.1.2");

using this in both codes