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

Simple TCP/IP server/client - help!

P: 1
Hello

I am very new to perl, however i managed to make this server/client work with udp, now i would like to do the same thing but with TCP/IP, what must i do?

Server:

Expand|Select|Wrap|Line Numbers
  1.  #!perl -w
  2. # Server Program
  3. use IO::Socket::INET;
  4. print ">> Server Program <<\n";
  5.  
  6. # Create a new socket
  7. $MySocket=new IO::Socket::INET->new(
  8.     LocalPort=>1234,
  9.     Proto=>'udp'
  10. );
  11.  
  12.  
  13. # Keep receiving messages from client
  14. $def_msg="\nReceiving message from client.....\n";
  15. $sum=0;
  16.  
  17. while(1)
  18. {
  19.         $MySocket->recv($number,128);
  20.         $sum=$sum+$number;
  21.         $MySocket->send($sum);
  22.  
  23.     if($number ne '0')
  24.     {
  25.         print "\nReceived message '", $number,"'\n";
  26.  
  27.     }
  28.     # If client message is empty exit
  29.     else
  30.     {
  31.         print "Cilent has exited!";
  32.         $MySocket->send($number);
  33.         exit 1;
  34.     }
  35. }
Client:
Expand|Select|Wrap|Line Numbers
  1. #!perl -w
  2. # Client Program
  3. use IO::Socket::INET;
  4. print ">> Client Program <<";
  5.  
  6. # Create a new socket
  7. $MySocket=new IO::Socket::INET->new(
  8.     PeerPort=>1234,
  9.     Proto=>'udp',
  10.     PeerAddr=>'localhost'
  11.     );
  12.  
  13. # Send messages
  14. $def_msg="client: ";
  15. print "\n",$def_msg;
  16. while($msg=<STDIN>)
  17. {
  18.     chomp $msg;
  19.     if($msg ne '0')
  20.     {
  21.  
  22.        if(print $MySocket($msg))
  23.         {
  24.             print "";
  25.         }
  26.         if($MySocket->recv($text,128))
  27.         {
  28.             print "\server: '", $text,"'\n";
  29.             print $def_msg;
  30.         }
  31.     }
  32.     else
  33.     {
  34.         # Send an empty message to server and exit
  35.         $MySocket->send('0');
  36.         exit 1;
  37.     }
  38. }
  39.  
  40.  
  41.  
Feb 7 '09 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
Did you try changing the protocol?

Proto=>'udp'
Feb 7 '09 #2

Icecrack
Expert 100+
P: 174
First Off in the Client script on line 28.

Expand|Select|Wrap|Line Numbers
  1. print "\server: '", $text,"'\n";
  2.  
Should be:
Expand|Select|Wrap|Line Numbers
  1. print "Server: '", $text,"'\n";
  2.  

now for server code you don't need the var or line of:

Expand|Select|Wrap|Line Numbers
  1. $def_msg="\nReceiving message from client.....\n";
  2.  
if you want to use it add
Expand|Select|Wrap|Line Numbers
  1. print $def_msg;
in between the while and if statement.
Feb 9 '09 #3

Post your reply

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