[prog] [C] network programming

Shuying shuyingw at cse.unsw.EDU.AU
Mon Oct 14 07:40:22 EST 2002


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