Maybe, socket() and fork() related bug.

Ahn, Kyung-Hwa. khan@tobest.com
Sun Dec 6 08:29:00 GMT 1998


Help me.
this daemon can't disconnect child socket.
After running this daemon, you can try to connect daemon by telnet.
But, nerver can't disconnect.
Maybe , socket() and fork() related bug.
Of course, other unix os(solaris 2.x, linux 2.x) disconnect child
socket fd
after connected.
My Com : P-II, Ram 64, Windows 95 korean version.
/* this is simple echo server source */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <varargs.h>
int main(argc,argv)
int argc;
char *argv[];
{
 int sockfd, /* socket file descripter */
 newsockfd, /* socket file descripter */
 cli_len, /* for cli_addr */
 childpid, /* for child process */
 j; /* for temp */
 char *ip;
 struct sockaddr_in cli_addr,serv_addr;
 if ( argc!=2 ) {
 printf("Usage : %s #port\n\n",argv[0]);
 exit(0);
 }
 if ( (sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 ) {
 exit(0);
 }
 bzero( (char *) &serv_addr, sizeof(serv_addr) );
 serv_addr.sin_family = AF_INET;
 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 serv_addr.sin_port = htons( atoi(argv[1]) ) ;
 if ( bind(sockfd, (struct sockaddr *) & serv_addr, sizeof(serv_addr))
<0) {
 exit(0);
 }
 listen ( sockfd, 5 );
 while ( 1 ) {
 /* Wait for a connection from a cli_lent process. */
 cli_len=sizeof(cli_addr);
 newsockfd = accept(sockfd, (struct sockaddr *) & cli_addr,
&cli_len);
 if (newsockfd<0) perror("server: accept error");
 /* Make child process */
 if ( (childpid=fork()) <0 ) {
 perror("fork()");
 }
 else if ( childpid==0) {
 /* for child */
 close(sockfd);
 SendtoSocket(newsockfd,"Hello? Why don't you exit?\n");
 exit(0);
 }
 signal(SIGCHLD, SIG_IGN);
 close(newsockfd);
 }
 return(0);
}
int SendtoSocket(sockfd,str)
int sockfd;
char *str;
{
 if ( writen(sockfd,str,strlen(str)) !=strlen(str) )
 perror("str_echo: writen error");
}
int writen(fd,ptr,nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
 int nleft, nwritten;
 nleft=nbytes;
 while ( nleft>0 ) {
 nwritten=write(fd,ptr,nleft);
 if ( nwritten <=0 ) return(nwritten); /* error */
 nleft -= nwritten;
 ptr += nwritten;
 }
 return (nbytes - nleft);
}
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


More information about the Cygwin mailing list

AltStyle によって変換されたページ (->オリジナル) /