earlati2 has asked for the wisdom of the Perl Monks concerning the following question:

I suppose my question is a very simple one, but I didn't find a real solution so I hope someone ca help.

I have 2 programma which send each other data using the socket via module Io::Socket

the server is somethink like this:

use IO::Socket; my ( $stmp, $nchar ); $| = 1; my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '2040', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; $sock->autoflush(); while( 1 ) { my $new_sock = $sock->accept(); $| = 1; $new_sock->autoflush(); while(1) { $nchar = read( $new_sock, $stmp, 1 ); print "$stmp"; print $new_sock "$stmp"; } } close($sock);

-------  while the cliente is as follow

my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => $remote_port,&nbsp; Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; $sock->autoflush(); ae_util::mylog( "SendSOCKT: $msg " ); print $sock "$msg\n"; $stmp = <$sock>; chomp $stmp; ae_util::mylog( "AnswSOCKT: $stmp " ); close( $sock );

The problem is that if I send a message terminated with new-line ( as print $sock "$msg\n"; ) is all ok

If I send the message without the newline at the end the program hangs, the server receive the data , and mybe send the answer , but the cliente didn't receive the answer.

Where I' m wrong in this and how can send message without end-line terninator on the socket ?

thank for the help, Enzo

Formatting cleansed by holli as per consideration

Replies are listed 'Best First'.
Re: how send message without new-line terminator on IO::Socket
by Corion (Patriarch) on Jul 26, 2006 at 08:32 UTC
      I only need to say why if I send a message terminated by a 
      new-line the server read the message , without the new-line 
      I got no answer.
      
      

      If the server is something like this : while( 1 ) { my $new_sock = $sock->accept(); $stmp = <$new_sock>; print "$stmp"; print $new_sock "$stmp\n"; close($new_sock); } close($sock); the server hang waiting for a newline. what cha I do to avoid the server waiting for the newline.

        You cannot use <$new_sock> anymore, because that waits for a newline character. You need to use read instead. You also likely want nonblocking IO using the four-argument version of select. Likely, IO::Select wraps this up nicely for you. There is example code on how to use IO::Select in its documentation. For select, I didn't find any nice documentation.

        Basically, select and IO::Select return once a socket is ready to receive more data or to send more data, and tell you which socket(s).

        There are three multiplexing frameworks I know of that handle nonblocking sockets in a manner that is more or less inconveniencing - POE, Danga::Socket and Coro. All three have different uses and different shortcomings.