home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / programm / 5855 < prev    next >
Encoding:
Text File  |  1993-01-07  |  3.3 KB  |  108 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!gumby!wupost!eclnews!cec2!mjb1
  3. From: mjb1@cec2.wustl.edu (Lord of flying horned octopi)
  4. Subject: Socketing question
  5. Message-ID: <1993Jan7.184830.7491@wuecl.wustl.edu>
  6. Sender: usenet@wuecl.wustl.edu (News Administrator)
  7. Nntp-Posting-Host: cec2
  8. Organization: Washington University, St. Louis MO
  9. Date: Thu, 7 Jan 1993 18:48:30 GMT
  10. Lines: 96
  11.  
  12. I'm fooling around trying to write a server that will accept telnet connections
  13. and then route them to a different location.  I am having loads of troubles
  14. getting even the first part of this to work.  I have this code, which I know
  15. is messy and hard to read, which is supposed to accept a connection and then 
  16. echo's back everything sent to it and exiting when it receives "quit".  And
  17. BTW I am running on a Sun 670MP with SunOS 4.1.2:
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <strings.h>
  24.  
  25. #define PORT 3000
  26. #define BUFSIZE 256
  27.  
  28. main() {
  29.  
  30.     struct sockaddr_in recv_addr, their_addr;
  31.     int addrlen, sockfd, recv_sockfd;
  32.     char buf[BUFSIZE];
  33.  
  34.     bzero( (char *) &recv_addr, sizeof(recv_addr));
  35.     recv_addr.sin_family = AF_INET;
  36.     recv_addr.sin_addr.s_addr = htonl( INADDR_ANY);
  37.     recv_addr.sin_port = htons( PORT);
  38.     
  39.     if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0)) < 0) {
  40.     fprintf( stderr, "can't create stream socket\n");
  41.     exit(-1);
  42.     }
  43.     if( bind( sockfd, ( struct sockaddr *) &recv_addr,
  44.          sizeof( recv_addr)) < 0) {
  45.     fprintf( stderr, "can't bind local address\n");
  46.     exit(-1);
  47.     }
  48.     listen( sockfd, 5);
  49.  
  50.     addrlen = sizeof( their_addr);
  51.     if( ( recv_sockfd = accept( sockfd, ( struct sockaddr *) &their_addr,
  52.                  &addrlen)) < 0) {
  53.     fprintf( stderr, "accept error\n");
  54.     exit(-1);
  55.     
  56.     }
  57.     send( recv_sockfd, "Connect...\n", 11);
  58.     do {
  59.     bzero( buf, BUFSIZE);
  60.     recv( recv_sockfd, buf, BUFSIZE);
  61.     send( recv_sockfd, buf, strlen( buf));
  62.     } while( strncmp( buf, "quit", 4));
  63.     send( recv_sockfd, "Bye bye...\n", 11);
  64.     close( sockfd);
  65.     close( recv_sockfd);
  66. }
  67.  
  68. Everything works except the line:
  69.  
  70.         send( recv_sockfd, buf, strlen( buf));
  71.  
  72. If I replace this line with:
  73.  
  74.         send( recv_sockfd, buf, BUFSIZE);
  75.  
  76. everything is hunky dorie.  But do I really need to send all 256 characters?
  77. I really only want to send up to the first NULL.  Why doesn't the 1st line
  78. work?  I checked and strlen() is returning the correct value.  I also tried
  79. the lines:
  80.  
  81.         send( recv_sockfd, buf, BUFSIZE);
  82.         send( recv_sockfd, buf, BUFSIZE);
  83.  
  84. This did what I expected.  It echoed buf twice.  BUT, and this is where it
  85. gets really weird, when I tried:
  86.  
  87.         send( recv_sockfd, buf, BUFSIZE);
  88.     strlen( buf);
  89.         send( recv_sockfd, buf, BUFSIZE);
  90.  
  91. nothing was echoed!  WHY?  How is the presense of strlen() effecting parts
  92. of the program below and *above* it???  I'm really bufuddled by all this.
  93.  
  94.  
  95. Couple other things too...
  96.  
  97. I first tried using write() and read() instead of
  98. send() and receive() without success.  Why don't they work?  
  99.  
  100. When the above code exits via "quit", and I try to subsequently execute it
  101. again, bind() fails.  If I exit my telnet session to it with the telnet
  102. command "quit" the program exits and I do not have the subsequent problems
  103. executing it again.  Why?
  104.  
  105. Any help would be highly appreciated.
  106.  
  107. MikeB
  108.