UDP recvfrom not blocking? help

Jul 29, 2011 at 12:05am
I am writing some simple client/server code using UDP. The program works fine, but if I only start the client, the recvfrom method does not block. However, when I remove the sendto method, recvfrom starts to block. Any idea of what is going on?

Here is the client side code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    int server_length;                      /* Length of server struct */
    char send_buffer[256] = "hi";           /* Data to send */
    time_t current_time;                    /* Time received */

    while(true)
    {

        /* Tranmsit data to get time */
        server_length = sizeof(struct sockaddr_in);
        if (sendto(m_oSocket, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&m_oServer, server_length) == -1)
        {
            fprintf(stderr, "Error transmitting data.\n");
            continue;
        }

        /* Receive time */

        if (recvfrom(m_oSocket, (char *)&current_time, (int)sizeof(current_time), 0, (struct sockaddr *)&m_oServer, &server_length) < 0)
        {
            fprintf(stderr, "Error receiving data.\n");
            continue;
        }

        /* Display time */
        printf("Current time: %s\n", ctime(&current_time));

        Sleep(1000);
    }


I am pretty sure the sockets and stuff got initialized properly
Jul 29, 2011 at 1:42am
Let me try to understand and maybe just a guess. When you do a sendto(..) you send data to some place and that some place will send a response back to you correct? Then you do a recvfrom and you get that response and it moves along.

Now if you remove sendto(..) and straight go to recvfrom, are there any response going to come in? If no then it block isn't it ? I don't know if you have explored any non-blocking API besides recvfrom?
Topic archived. No new replies allowed.