Archived

This topic is now archived and is closed to further replies.

vaceX

Strange problem... need help

Recommended Posts

This is the source of a very simple UDP-server. It works great - except for the first recvfrom()-call, which somehow doesn't give the correct address. This means; the first " cout << inet_ntoa(sender.sin_addr) << endl; " prints "0.0.0.0" on the screen. What causes this? What can I do to fix it?
    
#include <iostream>

#include <iomanip>

#include <sys/types.h>

#include <sys/socket.h>

#include <sys/time.h>

#include <time.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <errno.h>

#include <fcntl.h>

#include <netdb.h>

#include <signal.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/uio.h>

#include <unistd.h>

#include <sys/wait.h>

#include <sys/un.h>

#include <sys/select.h>

int     listen_fd, nready;
fd_set  rset;
socklen_t       len;
struct sockaddr_in      servaddr;
const int       on = 1;


void
network_startup()
{

        listen_fd = 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(12345);

        setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
        bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));

        listen(listen_fd, 5);

        FD_ZERO(&rset);
}
        
int
recv_data()
{
        char            in_data[512];
        uint16_t        ack_nummer;
        unsigned short  data_fran;

        struct sockaddr_in sender;
        memset(&(sender), '\0', sizeof(sender));
        recvfrom(listen_fd, in_data, 512, 0, (struct sockaddr *) &sender, &len);


        cout << inet_ntoa(sender.sin_addr) << endl;
}
                                                                                                
                                                                                                
                                                                                                









void
neverending_loop()
{
        FD_SET(listen_fd, &rset);
        if ( (nready = select(listen_fd+1, &rset, NULL, NULL, NULL)) < 0)
        {
                if (!(errno == EINTR))
                {
                        cout << "fel i select!\n";
                }
        }

        if (FD_ISSET(listen_fd, &rset))
        {
                recv_data();
        }
}


void
main(int argc, char **argv)
{

        network_startup();

        while(true)
        {
                neverending_loop();
        }
}
    
[edited by - vaceX on March 8, 2003 4:21:20 AM]

Share this post


Link to post
Share on other sites
you don''t use SO_REUSEADDR or listen with UDP.

len has to equal the size of a sockaddr_in structure before you pass it to recvfrom. It will have to be a different size if you start using ip6.

You should get a copy of Unix Network Programming. It helps a lot. I made the mistake of not setting len to the right size recently myself. I figured out the problem by looking at the examples again.

kdIXfA.gamedev.10.coreyh@xoxy.net
www.ipeg.com/~rlfc

Share this post


Link to post
Share on other sites