home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!gumby!wupost!eclnews!cec2!mjb1
- From: mjb1@cec2.wustl.edu (Lord of flying horned octopi)
- Subject: Socketing question
- Message-ID: <1993Jan7.184830.7491@wuecl.wustl.edu>
- Sender: usenet@wuecl.wustl.edu (News Administrator)
- Nntp-Posting-Host: cec2
- Organization: Washington University, St. Louis MO
- Date: Thu, 7 Jan 1993 18:48:30 GMT
- Lines: 96
-
- I'm fooling around trying to write a server that will accept telnet connections
- and then route them to a different location. I am having loads of troubles
- getting even the first part of this to work. I have this code, which I know
- is messy and hard to read, which is supposed to accept a connection and then
- echo's back everything sent to it and exiting when it receives "quit". And
- BTW I am running on a Sun 670MP with SunOS 4.1.2:
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <strings.h>
-
- #define PORT 3000
- #define BUFSIZE 256
-
- main() {
-
- struct sockaddr_in recv_addr, their_addr;
- int addrlen, sockfd, recv_sockfd;
- char buf[BUFSIZE];
-
- bzero( (char *) &recv_addr, sizeof(recv_addr));
- recv_addr.sin_family = AF_INET;
- recv_addr.sin_addr.s_addr = htonl( INADDR_ANY);
- recv_addr.sin_port = htons( PORT);
-
- if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0)) < 0) {
- fprintf( stderr, "can't create stream socket\n");
- exit(-1);
- }
- if( bind( sockfd, ( struct sockaddr *) &recv_addr,
- sizeof( recv_addr)) < 0) {
- fprintf( stderr, "can't bind local address\n");
- exit(-1);
- }
- listen( sockfd, 5);
-
- addrlen = sizeof( their_addr);
- if( ( recv_sockfd = accept( sockfd, ( struct sockaddr *) &their_addr,
- &addrlen)) < 0) {
- fprintf( stderr, "accept error\n");
- exit(-1);
-
- }
- send( recv_sockfd, "Connect...\n", 11);
- do {
- bzero( buf, BUFSIZE);
- recv( recv_sockfd, buf, BUFSIZE);
- send( recv_sockfd, buf, strlen( buf));
- } while( strncmp( buf, "quit", 4));
- send( recv_sockfd, "Bye bye...\n", 11);
- close( sockfd);
- close( recv_sockfd);
- }
-
- Everything works except the line:
-
- send( recv_sockfd, buf, strlen( buf));
-
- If I replace this line with:
-
- send( recv_sockfd, buf, BUFSIZE);
-
- everything is hunky dorie. But do I really need to send all 256 characters?
- I really only want to send up to the first NULL. Why doesn't the 1st line
- work? I checked and strlen() is returning the correct value. I also tried
- the lines:
-
- send( recv_sockfd, buf, BUFSIZE);
- send( recv_sockfd, buf, BUFSIZE);
-
- This did what I expected. It echoed buf twice. BUT, and this is where it
- gets really weird, when I tried:
-
- send( recv_sockfd, buf, BUFSIZE);
- strlen( buf);
- send( recv_sockfd, buf, BUFSIZE);
-
- nothing was echoed! WHY? How is the presense of strlen() effecting parts
- of the program below and *above* it??? I'm really bufuddled by all this.
-
-
- Couple other things too...
-
- I first tried using write() and read() instead of
- send() and receive() without success. Why don't they work?
-
- When the above code exits via "quit", and I try to subsequently execute it
- again, bind() fails. If I exit my telnet session to it with the telnet
- command "quit" the program exits and I do not have the subsequent problems
- executing it again. Why?
-
- Any help would be highly appreciated.
-
- MikeB
-