C - Problems while setting up a simple UDP server
===

I'm going into sockets programming in C, but I have a problem that -I'm sure- it's coming from my confusion: How can I set up a server using *getaddrinfo()*, and how to get it's address from a client? I'm using UDP.

(I come from [Beej's tutorial][1], but I still not understand that)

This is the code of my server (I'm trying to run it at *mhgc.net* and port 3051, although I haven't found the way to set the... hostname? Sorry, I'm confused):

    int sockd;
	
	/* Get my IP address */
	struct addrinfo hints, * servinfo;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_PASSIVE;  // use my own address
	if( getaddrinfo(NULL, "3051", &hints, &servinfo) != 0 ) {
        /* Yes, below is not... 'pretty code'; this is just an attempt */
		errorf("Failed to get server address: %s", gai_strerror( getaddrinfo(NULL, "3051", &hints, &servinfo) ));
		exit(EXIT_FAILURE);
		return 1;
	}
	
	struct addrinfo * p = NULL;
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if( (sockd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1 ) {
			perror("Cannot open socket");
			continue;
        }
        
        if(bind(sockd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockd);
			perror("Cannot bind to port");
			continue;
        }
        
        break;
	}
	
	if(p == NULL) {
		error("listener: failed to bind socket");
		exit(EXIT_FAILURE);
		return 0;
	}
	
	freeaddrinfo(servinfo);
	
	struct sockaddr_storage clientinfo;
	socklen_t clientinfo_len = sizeof(clientinfo);
    
    char buffer[15];
    memset(buffer, 0, sizeof(buffer));
    if( recvfrom(sockd, buffer, sizeof(buffer), 0, (struct sockaddr *)&clientinfo, &clientinfo_len) == -1 ) {
        perror("Error receiving packet");
        exit(EXIT_FAILURE);
        return 0;
    }
    
    char s[INET6_ADDRSTRLEN];
    printf("listener: got packet from %s\n", inet_ntop(clientinfo.ss_family, get_in_addr((struct sockaddr *)&clientinfo), s, sizeof(s)));
    printf("listener: packet contains \"%s\"\n", buffer);
    	
	/* Close the socket descriptor */
	close(sockd);
        
        /* And now, exit, or whatever I need ... ... ... */

and for my client:

    int sockd;
	
	struct addrinfo hints, * servinfo;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	//hints.ai_flags = AI_CANONNAME;
	if( getaddrinfo(MKS_HOSTNAME, "www", &hints, &servinfo) != 0 ) {
		errorf("Failed to get server address: %s", gai_strerror( getaddrinfo(NULL, "3051", &hints, &servinfo) ));
		return 1;
	}
	
	struct addrinfo * p = NULL;
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if((sockd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
			perror("Cannot open socket");
			continue;
		}
		
		break;
	}
	
	if(p == NULL) {
		error("talker: failed to bind socket");
		return 2;
	}
	
	if( sendto(sockd, "this-is-a-test", strlen("this-is-a-test")+1, 0, p->ai_addr, p->ai_addrlen) == -1 ) {
		perror("Error sending packet");
		return 1;
	}
	
	freeaddrinfo(servinfo);
	
	close(sockd);

*error* and *errorf* are macros to help me debugging the application. Don't worry about them.

Another tip, if you allow me: talker.c, from Beej's tutorial, uses a hostname to 'find' the server. I downloaded listener.c and talker.c, compiled it (make listener && make talker) but, I didn't know what to pass as talker argument. What is the hostname of listener? How can I find it? Sorry again. I'm really confused about that :S

Working on Debian GNU/Linux "squeeze", using gcc compiler v4.4.5


  [1]: http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html