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

Commmunicate between server and client

P: 2
Hi,
I was trying to commmunicate between a server and a client. The SERVER and CLIENT scripts are given below. With this script i was able to get the message from the server only if i type something in the client and viceversa.
My requirement is that even if someone doesnt type anything,message from other side should reach them. i believe we can use threads for this purpose; but i am not quite sure to use them.Please help.....

Expand|Select|Wrap|Line Numbers
  1. ####SERVER
  2.  
  3. #!/usr/bin/perl -w
  4.  
  5. use warnings;
  6.  
  7. use strict;
  8.  
  9. use threads;
  10.  
  11. use IO::Socket;
  12.  
  13. my $sock = new IO::Socket::INET(LocalPort => '7745',
  14.  
  15.                                  Proto => 'tcp',
  16.  
  17.                                  Listen => 10,
  18.  
  19.                                  Reuse => 1,);
  20.  
  21. #creating a socket                                                                                             
  22.  
  23. my $client;
  24.  
  25. die "Socket Error: $!\n" unless $sock;
  26.  
  27. STDOUT->autoflush(1);
  28.  
  29. my($new_sock, $buf);
  30.  
  31. while(1){
  32.  
  33. while ($new_sock = $sock->accept())
  34.  
  35.             {print "*************Connected\n";
  36.  
  37.  
  38.  
  39.                         while (defined($buf = <$new_sock>))
  40.  
  41.                                     {print "Connected\n";
  42.  
  43.  
  44.  
  45.                                                                         print scalar $buf;
  46.  
  47.                                                                         print "Enter msg for the client\n";
  48.  
  49.                                                                         chomp(my $msg=<STDIN>);
  50.  
  51.                                                                         print $new_sock "from server:$msg\n";
  52.  
  53.  
  54.  
  55.                                     }
  56.  
  57.                         close $new_sock;
  58.  
  59.             }
  60.  
  61. }
  62.  
  63. #####CLIENT
  64.  
  65.  
  66.  
  67. #!/usr/bin/perl -w
  68.  
  69. use warnings;
  70.  
  71. use strict;
  72.  
  73. use IO::Socket;
  74.  
  75. my $sock = new IO::Socket::INET(   PeerAddr => '10.10.97.23',
  76.  
  77.                                  PeerPort => '7745',
  78.  
  79.                                  Proto => 'tcp',
  80.  
  81.                                  Reuse => 1,);
  82.  
  83. die "Socket Error: $!\n" unless $sock;
  84.  
  85. #creating socket for the client
  86.  
  87. my($new_sock, $buf);
  88.  
  89. while(1)
  90.  
  91.             {
  92.  
  93.                         print "Enter msg for the server\n";
  94.  
  95.                         chomp(my $msg=<STDIN>);
  96.  
  97.                         print $sock "from client:$msg\n";
  98.  
  99.                         # writing into the socket
  100.  
  101.                         print scalar <$sock>;
  102.  
  103.                         # print server response to STDOUT
  104.  
  105.             }
  106.  

regards,
Thumban
Oct 31 '07 #1
Share this Question
Share on Google+
2 Replies


eWish
Expert 100+
P: 971
Unfortunately, I am not well versed in this category and wish I could help. Have you resolved your issue? Hopefully, one of our experts will be able to help.

--Kevin
Nov 3 '07 #2

100+
P: 154
Just a suggestion I do not know if this will for work you.

Did you try using a condition?

e.g
if ($msg eq"") { code}
Nov 6 '07 #3

Post your reply

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