By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,176 Members | 984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,176 IT Pros & Developers. It's quick & easy.

TCP echo client & server (socket about)-- network programming with perl

P: 3
hello everyone!
i am a freshman of computer science and not a native English speaker, so if i made some stupid grammatic or spelling mistake please tell me. i'll correct it as soon as possible and thank you very much.

i am reading network programming with perl by Lincoln D. Stein
i believe many of you guys know about this book . Now i got stuck in chapter4 "The tcp protocols".

Here is the backgroound of the problem:
the author gives two examples , a tcp echo client and a tcp echo server.these two examples were supposed to be tested respectively like below:

-----------------------
% tcp_echo_cli1.pl www.modperl.com
How now brown cow?
How now brown cow?

% tcp_echo_serv1.p1
waiting for incoming connections on port 2007...
Connection from [127.0.0.1,2865]
Connection from [127.0.0.1,2865] finished
Connection from [192.168.3.2,2901]
Connection from [192.168.3.2,2901] finished
bytes_sent = 26, bytes_received = 26
-----------------------

since the web server provided is not avaiable now and i have scripts of both client and server, so i started try to run these two simultaneously on my computer.

OK! here is the problem:
PS: I'm using Ubuntu 8.10
------------------command line---------------------
1 whsdu@dell-desktop:~/Show$ ./tcp_echo_server.pl&
2 [1] 16848
3 whsdu@dell-desktop:~/Show$ waiting for incoming connection on port 2007...
4 ./client_echo.pl 127.0.0.1 2007
5 the host is  ,the port is 2007,the protocol is 6! (CLIENT)
6 Connection from [127.0.0.1] port number:55144.
7 whsdu
8 ^CConnection from [127.0.0.1] finished (SER5).

9 whsdu@dell-desktop:~/Show$ fg
10 ./tcp_echo_serve.pl
11 ^CThis is the serve side:bytes_sent=5,bytes_receive=6 (SER1)
12 whsdu@dell-desktop:~/Show$
------------------command line---------------------
As you can see I set server as backstage running in line 1 and start client in line 4,the ipadress is localaddress and the portnumber is 2007.
Line 5 and 6 tell me that the connection is established.
But when i typed 'whsdu' and pressed 'Enter' there was no response in line 8. And pressing 'Ctrl+D' is useless I have to exit by pressing 'Ctrl+C' which leaves the client part unfinished!.
I guss maybe it is not right to set the server as backstage running or there is something wrong with client part.

Here are the scripts below.

client_echo.pl:
Expand|Select|Wrap|Line Numbers
  1.       1 #!/usr/bin/perl
  2.       2 use strict;
  3.       3 use Socket;
  4.       4 use IO::Handle;
  5.       5 
  6.       6 my ($bytes_in,$bytes_out)=(0,0);
  7.       7 
  8.       8 my $host= shift||'localhost';
  9.       9 my $port= shift||getservbyname('echo','tcp');
  10.      10 
  11.      11 my $protocol=getprotobyname('tcp');
  12.      12 my $host=inet_aton($host) or die "$host:unkown host";
  13.      13 
  14.      14 print" the host is $host ,the port is $port,the protocol is $protocol!  (CLIENT)\n";
  15.      15 
  16.      16 socket(SOCK,AF_INET,SOCK_STREAM,$protocol) or die "socket() filed:$!";
  17.      17 my $des_addre=sockaddr_in($port,$host);
  18.      18 
  19.      19 connect(SOCK,$des_addre)or die"can't connect!$!";
  20.      20 
  21.      21 SOCK->autoflush(1);
  22.      22 
  23.      23 
  24.      24 while(<>){         ## Ctrl+D is useless here !!!WHY!????WHY WHY Why why!!!
  25.      25         print SOCK $_;
  26.      26         my $msg_in=<SOCK>;
  27.      27         print $msg_in;
  28.      28 
  29.      29         $bytes_in+=length($msg_in);
  30.      30         $bytes_out+=length($_);
  31.      31 }
  32.      32 
  33.      33 close SOCK;
  34.      34 print STDERR"bytes_sent=$bytes_out,bytes_received=$bytes_in  (CLIENT)\n";
  35.  
tcp_echo_server.pl:
Expand|Select|Wrap|Line Numbers
  1.       1 #!/usr/bin/perl
  2.       2 #file:tcp_echo_serve.pl
  3.       3 #usage: tcp_echo_serve
  4.       4 
  5.       5 use strict;
  6.       6 use Socket;
  7.       7 use IO::Handle;
  8.       8 use constant MY_ECHO_PORT=>'2007';
  9.       9 
  10.      10 my ($bytes_in,$bytes_out)=(0,0);
  11.      11 
  12.      12 my $port=shift||MY_ECHO_PORT;
  13.      13 my $protocol=getprotobyname('tcp');
  14.      14 
  15.      15 $SIG{INT}=sub{
  16.      16         print STDERR"This is the serve side:bytes_sent=$bytes_out,bytes_receive=$bytes_in (SER1)\n";
  17.      17         exit 0;
  18.      18 };
  19.      19 
  20.      20 socket(SOCK,AF_INET,SOCK_STREAM,$protocol);
  21.      21 setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1);
  22.      22 
  23.      23 my $my_addr=sockaddr_in($port,INADDR_ANY);
  24.      24 bind(SOCK,$my_addr) or die"bind() failed:$!";
  25.      25 listen(SOCK,SOMAXCONN) or die "listen() failed:$!";
  26.      26 
  27.      27 warn"waiting for incoming connection on port $port...\n";
  28.      28 
  29.      29 while(1){
  30.      30         next unless my $remote_addr=accept(SESSION,SOCK);
  31.      31         my($port,$hiaddr)=sockaddr_in($remote_addr);
  32.      32         warn"Connection from [",inet_ntoa($hiaddr),"] port number:$port.\n";
  33.      33 
  34.      34         SESSION->autoflush(1);
  35.      35 
  36.      36         while(<SESSION>){
  37.      37         $bytes_in +=length($_);
  38.      38         chomp;
  39.      39 
  40.      40         my $msg_out=$_;
  41.      41         print SESSION $msg_out;
  42.      42         $bytes_out+=length($msg_out);
  43.      43         }
  44.      44         warn"Connection from [",inet_ntoa($hiaddr),"] finished  (SER5).\n";
  45.      45         close SESSION;
  46.      46 }
  47.      47 
  48.      48 close SOCK;
  49.      49 
  50.  
I've been working on this for two days and still cannot finger out what's wrong.
maybe I should keep going. maybe a week later when i review all these stuffs eveything will be solved automatically.
After all thanks for reading this much words.
Aug 7 '10 #1
Share this Question
Share on Google+
1 Reply


P: 3
sorry! I misunderstood the useage of CODE TAGS!Thanks very much for your help. i won't make this kind of mistake again.
Aug 15 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.