Sign in to follow this  
Hernan13

datagram sockets not recieving packets (winsock)

Recommended Posts

Hernan13    122
I'm working on a multiplayer game, using code developed from beej's guide for the net code. It used to work fine, and now after coming back to it after a couple of months holiday, the game doesn't detect the packets reaching the computer, I can't remember making any changes to the relevant bits of code. I think I've narrowed the problem down to something wrong with either the initialization of my socket, or with my call to select. The socket is initialized without errors. Debugging has found that select is timing out and returning 0, even after waiting for .8s, before select is called the sockets fd is definately in the fd_set. I've used wireshark to ensure that the packets are reaching my computer, and that they're destined for the port which the socket is connected to. Heres the code initializing the socket: 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, SERVERPORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); } // 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) { closesocket(sockfd); perror("listener: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "listener: failed to bind socket\n"); } } and heres the code which should recieve the packages: FD_ZERO(&readfds); FD_SET(sockfd, &readfds); int testint; testint = select(sockfd + 1, &readfds, NULL, NULL, &tv); printf("listener: waiting to recvfrom %s, testint = %i\n", SERVERPORT, testint); if(FD_ISSET(sockfd, &readfds)) { addr_len = sizeof their_addr; if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, (int *)&addr_len)) == -1) { perror("recvfrom"); exit(1); } Thanks in advance

Share this post


Link to post
Share on other sites
Hernan13    122
I'm still having the same problem, even after looking at it for a few hours more. Still fairly sure I havn't changed this code since it last worked. The packets which are being sent are a little larger now, but still fairly small (on the order of 100 bytes), could that possibly be it?

Share this post


Link to post
Share on other sites
Codeka    1239
Are you sure it's not binding to an IPv6 address? If you use AF_UNSPEC then you'll get IPv6 back as one of the possible addresses, but if you're connecting to an IPv4 address, you're not going to see anything. Possibly you'll want to bind to all of the addresses returned by getaddrinfo, rather than just the first one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this