home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!rsiatl!karl
- From: karl@dixie.com (Karl Klingman)
- Subject: Re: Socket Programming Help
- Message-ID: <1h_qdmj@dixie.com>
- Date: Thu, 05 Nov 92 14:20:45 GMT
- Organization: Dixie Communications Public Access. The Mouth of the South.
- References: <1992Nov4.143004.22495@nuscc.nus.sg>
- Lines: 47
-
- scstech@solomon.technet.sg (Kenneth Soh) writes:
-
-
- >I guess the problem is in the use of accept() but I don't
- >understand want actually went wrong. Below is my "program":
-
- >------------------------------------------------------
- /* OK here you go */
- /* I changed your reference to sin to srvaddr for clarity */
-
- struct sockaddr_in srvaddr, cliaddr;
-
- >universal_socket = socket( AF_INET, SOCK_STREAM, 0 );
-
- >srvaddr.sin_family = AF_INET;
- >srvaddr.sin_addr.s_addr = htonl(INADDR_ANY); <- NOTE htonl
- >srvaddr.sin_port = htons(atoi(argv[1])); <-- NOTE htons
- /* note cast below */
- >bind( universal_socket,(struct sockaddr *) &srvaddr, sizeof( srvaddr ) )
-
- >listen( universal_socket, 3 ); <- NOTE: normally one would use 5 (max)
-
- for (;;) {
- /* >client_socket = accept( universal_socket, 0, 0 ); <- nope */
- clilen = sizeof(cliaddr); <- you'll need this below
- /* notice that the 2nd and 3rd arguments must be an addresses */
- client_socket = accept(universal_socket, (struct sockaddr *) &cli_addr,
- &clilen);
- /* check to see if client_socket is valid */
- /* fork */
- /* check for successful fork */
- if (childpid == 0) { /* child process */
- close(universal_socket);
- /* do your stuff here */
- close(client_socket); /* optional since exit will do it */
- exit(0);
- } else { /* parent */
- close(client_socket);
- }
- } /* for */
- >------------------------------------------------------
-
- I hope this helps.
- --
- He who would trade his liberty for | Karl Klingman
- short-term security deserves | American Research Group, Inc.
- neither... Ben Franklin | emory!slammer!arg!karl
-