Bidirectional Sockets

2002-04-15 Thread James Taylor

I'm sure this is frequently asked, and I am truly sorry if it is, but 
reading through my mail I don't see this question anywhere:

Curious as to how I would go about creating bidirectional sockets, ie. 
client sends information to the server, then server responds to the 
client.  I need to know how to do this so that I can tell whether or not 
an operation was successful or not. The server is currently set up like:

while ($new_sock = $sock->accept()) {
   while (defined ($buf = <$new_sock>)) {
   (do something);
}
}

I tried printing to $new_sock, but the client obviously isn't listening 
- Do I have to create a client/server for both the actual client, AND 
the server? Can someone point me in the right direction here? Thanks


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Bidirectional Sockets

2002-04-15 Thread bob ackerman


On Monday, April 15, 2002, at 02:31  PM, James Taylor wrote:

> I'm sure this is frequently asked, and I am truly sorry if it is, but 
> reading through my mail I don't see this question anywhere:
>
> Curious as to how I would go about creating bidirectional sockets, ie. 
> client sends information to the server, then server responds to the 
> client.  I need to know how to do this so that I can tell whether or not 
> an operation was successful or not. The server is currently set up like:
>
> while ($new_sock = $sock->accept()) {
>   while (defined ($buf = <$new_sock>)) {
>   (do something);
>}
> }
>
> I tried printing to $new_sock, but the client obviously isn't listening - 
> Do I have to create a client/server for both the actual client, AND the 
> server? Can someone point me in the right direction here? Thanks

only one side has to be a server, but both sides have do have listen.
if client doesn't listen how would it get anything in from the server? so,
  yes.
you might want to set up some protocol so each side can identify a start 
and end of a message.
client doesn't do $sock->accept. That's only difference.
both side do:
read using: $message = <$new_sock>;
write using: print $new_sock, $messsage;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




More Bidirectional Sockets w/ Win32 Client

2002-04-16 Thread James Taylor

I have figured out how to set up bidirectional communication through 
sockets by forking the server and the client - The client forks just 
fine under Linux, but if I take the script to Win32 fork doesn't seem to 
work.  This is the code for both the server and client:

Here's the server:

#!/usr/bin/perl

use IO::Socket;
$SIG{CHLD} = sub {wait ()};

$main_sock = new IO::Socket::INET (LocalHost=> 'shell',
   LocalPort=> 1200,
   Listen   => 5,
   Proto=> 'tcp',
   Reuse=> 1,) or die "Couldn't 
create socket: $!\n";

while ($new_sock = $main_sock->accept()) {
   $pid = fork();
   die "Cannot fork: $!\n" unless defined($pid);

   if ($pid == 0) {
  # Child Process
  while (defined ($buf = <$new_sock>)) {
 chomp($buf);
 print "Client said: $buf\n";
 print $new_sock "You said: $buf\n";
  }
  exit(0);
   }
}

close ($main_sock);


And here's the client:

#!/usr/bin/perl

use IO::Socket;
$client = new IO::Socket::INET (PeerAddr => 'shell',
  PeerPort => 1200,
  Proto => 'tcp'
 ) or die "Couldn't create socket: $!\n";


my $pid = fork();  die "Cannot fork\n" unless defined $pid;

if ($pid) {
write_sock();
waitpid($pid, 0);
} else {
read_sock();
}

sub write_sock {
   for (1 .. 10) {
  print $client "$_\n";
   }

}

sub read_sock {
while (my $line = <$client>) {
print $line;   # report to stdout
}
}



Like I said, this works just fine on Linux->Linux.  When the client is 
Win32 however - it writes to the server just fine, but will just sit 
there and never read.  When I press control-C to escape out of the 
program, it THEN reads the data from the server.   I tried reading into 
IO::Select to see if that would fix my problem with the client, but I 
just can't figure it out. Can someone help me with this??




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]