zulihomevn
Posts: 1
Joined: Mon Mar 16, 2020 3:08 am
Contact: Website

UDP Problem

Mon Mar 16, 2020 3:58 am

I created a socket

Code: Select all

Code: Select all

int rx_sock;

struct sockaddr_in server, from;

int UDP_Init(void)
{
    rx_sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (rx_sock < 0)
    {
        printf("socket create error\n");
        return -1;
    }
    else
    {
        printf("socket created %d\n", rx_sock);
    }

    #if 1
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(8101);

    if (bind(rx_sock, (struct sockaddr *) &server, sizeof(server)) != 0)
    {
        printf("socket bind error\n");
        return -1;
    }
    else
    {
        printf("socket bind success\n", rx_sock);
    }
    #endif
    return 0;
}

int UDP_Read(uint8_t *rx_buf, int rx_size)
{
    int len;
    socklen_t socklen;
    len = recvfrom(rx_sock, rx_buf, rx_size, 0, (struct sockaddr *)&from, &socklen);

    return len;
}

Heater
Posts: 17374
Joined: Tue Jul 17, 2012 3:02 pm

Re: UDP Problem

Mon Mar 16, 2020 4:49 pm

zulihomevn wrote:
Mon Mar 16, 2020 3:58 am
I created a socket
That sounds like the start of a great story.

Can't wait to hear what happened next...
Memory in C++ is a leaky abstraction .

StuartF
Posts: 35
Joined: Sun Feb 02, 2014 5:41 pm

Re: UDP Problem

Mon Mar 16, 2020 10:19 pm

I'm wondering where the CODE: was SELECT from.... :shock:

LdB
Posts: 1670
Joined: Wed Dec 07, 2016 2:29 pm

Re: UDP Problem

Tue Mar 17, 2020 2:28 am

Change the 0 in recvfrom to MSG_WAITALL .. it isn't zero on all versions of linux

That assumes you are actually calling the UDP_Read because you don't show us that and I am assuming you have a "main"

Return to “C/C++”