UDP server with two ports in C

Hello, I'm having a little bit of an issue with this code. It would run and eventually time out, but not get any data from the client.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#ifndef S_SPLIT_S     // Workaround for splint.
#include <unistd.h>
#endif

int main(int argc, char ** argv)
{
    int sockfd, sockfd2;
    int n;
    struct sockaddr_in client_address;
    struct sockaddr_in servaddr;
    socklen_t len;
    char workspace[128];
    int ready;

    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    sockfd2 = socket(AF_INET, SOCK_DGRAM, 0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port=htons(0);
    bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    //servaddr.sin_port=htons(9001);
    bind(sockfd2,(struct sockaddr *)&servaddr,sizeof(servaddr));

    fd_set socks;
    FD_ZERO( &socks);
    FD_SET(sockfd, &socks);
    FD_SET(sockfd2, &socks);

    int nsocks;
    nsocks = sockfd;
    if(sockfd2 > nsocks)
    {
        nsocks = sockfd2;
    }
    nsocks++;

    struct timeval timing;
    timing.tv_sec = 30;
    timing.tv_usec = 0;

    if((ready = select(nsocks, &socks, NULL, NULL, &timing)) == -1)
    {
        printf("error\n");
    }
    else if (ready == 0)
    {
        printf("timeout\n");
    }
    else
    {
        if(FD_ISSET(sockfd, &socks))
        {
            len = sizeof(client_address);
            recvfrom(sockfd,workspace,128,0,(struct sockaddr *)&client_address, &len);
            printf("%s\n", workspace);
        }
        if(FD_ISSET(sockfd2, &socks))
        {
            len = sizeof(client_address);
            recvfrom(sockfd2,workspace,128,0,(struct sockaddr *)&client_address, &len);
            printf("%s\n", workspace);
        }
    }

}


I'm a total noob in this network programming and honestly have no idea what I'm doing. If someone could help me it would be helpful. Note: this is the server code.
Last edited on
Topic archived. No new replies allowed.