[prog] RE: [Courses] [C] network programming

Anand R anand.r at cybertech.co.in
Thu Oct 17 16:24:48 EST 2002


Shuying, I have picked this up directly from Beej's guide to Network
Programming to quickly help you troubleshoot this. 

regards,
andy

Credits: <Ctrl C> Beej's guide to network Programming <Ctrl v>

Here is the source for listener.c14:
/*
** listener.c - a datagram sockets "server" demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MYPORT 4950 // the port users will be connecting to
#define MAXBUFLEN 100
int main(void)
{
int sockfd;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int addr_len, numbytes;
char buf[MAXBUFLEN];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
if ((numbytes=recvfrom(sockfd,buf, MAXBUFLEN-1, 0,(struct sockaddr
*)&their_addr, &addr_len)) == -1) {

perror("recvfrom");
exit(1);
}
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
printf("packet contains \"%s\"\n",buf);
close(sockfd);
return 0;
}

Note: 

Notice that in our call to socket() we're finally using SOCK_DGRAM. Also,
note that there's no need to listen()
or accept(). This is one of the perks of using unconnected datagram sockets!

-----Original Message-----
From: Shuying [mailto:shuyingw at cse.unsw.EDU.AU]
Sent: Monday, October 14, 2002 3:10 AM
To: programming at linuxchix.org
Cc: courses at linuxchix.org
Subject: [Courses] [C] network programming


Hi,

Sorry about the cross posting, I wasn't sure which list is more
appropriate so I've sent it to both.

I've got a client and server, which sends UDP packets to each other.
The client seems to have sent a packet but the server doesn't seem to
be getting it. Below is a snippet of the relevant code (from the server
side) in question:

int main(..){
    int sockfd,s;
    struct sockaddr_in host, mips;
    struct hostent *h;
    ...
    if((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
        perror("socket error");
        return -1;
    }
    if((h = gethostbyname(argv[1])) == 0){
        herror("unknown host");
        return -1;
    }
    host.sin_family = AF_INET; // host byte order
    host.sin_port = htons (PORT_NUMBER); // short, network byte order
    host.sin_addr.s_addr = INADDR_ANY; //auto-fill with this host IP
    memcpy(&host.sin_addr,h->h_addr, h->h_length);
    memset(&(host.sin_zero), '\0', 8); // zero out rest of struct

    memset(&(mips.sin_zero), '\0', sizeof(mips));
    mips.sin_family = AF_INET;
    mips.sin_port = htons(PORT_NUMBER);

    if((bind(sockfd, (struct sockaddr *) &host, sizeof(host))) < 0) {
        perror("bind");
        close(sockfd);
        return(-1);
    }

	while(1){
		f((pktlen = recvfrom(sockfd, buf, BUFLEN, 0, (struct
sockaddr *)&mips, &addrlen))> -1){
            printf("command is %d\n", ((pkt_t *)(&buf))->hdr);
            switch(ntohs(((pkt_t *)(&buf))->hdr)){
	 	...
	    }
		sendto(sockfd, (pkt_t *) &pkt, sizeof(pkt), 0, (struct
sockaddr *)&host, sizeof(host)) ;


	}



Can anyone tell me what I've done wrong?


thanks,
Shuying




More information about the Programming mailing list