[prog] [C] network programming
Cliff Crawford
cjc26 at cornell.edu
Sun Oct 13 18:38:41 EST 2002
On Mon, Oct 14, 2002 at 07:40:22AM +1000, Shuying wrote:
> [snip]
> if((bind(sockfd, (struct sockaddr *) &host, sizeof(host))) < 0) {
> perror("bind");
> close(sockfd);
> return(-1);
> }
>
> while(1){
> if((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?
It looks like you're missing calls to listen() and accept(). IIRC
(been a while since I've played with sockets :), you should be doing
something like the following:
listen(sockfd, blah, blah);
while(1) {
accept(sockfd, ...);
/* now you can call recv(), send(), etc. on the socket */
}
--
Cliff Crawford :: cjc26 at cornell dot edu
We allow our ignorance to prevail upon us and make us think we can
survive alone, alone in patches, alone in groups, alone in races, even
alone in genders. -- Maya Angelou
More information about the Programming
mailing list