EscapeCharacter
03-30-2002, 07:30 AM
ive been reading the beej socket programming tutorial and something is bugging me, i made the sample client server app in the tut and im getting some weird results. when i go into the the accept loop the first connection to the server give this error "send: Socket operation on non-socket" spit out by perror ofcourse. the weird thing is if i disconnect the client then reconnect the server wont give the error and will do what its suppose to. heres the code i have for the accept loop.
while(1){
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size) == -1)){
perror("accept");
continue;
}
printf("server: got connection from %s\nsock is %d\n",
inet_ntoa(their_addr.sin_addr), new_fd);
if(!fork()){
close(sockfd);
if(send(new_fd, "Sup yo\n", sizeof("Sup yo\n"), 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
the send thats giving the error is right after fork.
while(1){
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size) == -1)){
perror("accept");
continue;
}
printf("server: got connection from %s\nsock is %d\n",
inet_ntoa(their_addr.sin_addr), new_fd);
if(!fork()){
close(sockfd);
if(send(new_fd, "Sup yo\n", sizeof("Sup yo\n"), 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
the send thats giving the error is right after fork.