Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?

by p4R4d0x (Initiate)
on Oct 26, 2009 at 16:58 UTC ( [id://803309]=note: print w/replies, xml ) Need Help??


in reply to Re: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?
in thread Is it possible to create a socket client and server that can send information back and forth using IO::Socket?

Maybe I'm just going about sending data back in the wrong way? I'm unsure of how to get the handle to send from the 'server' to the 'client'. Here's the socket portion of what I have so far:

'Server'

my $clientSocket = new IO::Socket::INET( PeerAddr => $ipSend, PeerPort => $sendPort, Proto => 'tcp', ); $clientSocket or die "Could not create socket:$!\n"; print $clientSocket "HELLO 1.0"; sleep(3); close $clientSocket;

'Client'

my $servSocket = IO::Socket::INET->new( #LocalHost => 'localhost', LocalPort => $listenPort, #7890 Proto => 'tcp', Listen => 2, Reuse => 1 ); $servSocket or die "Could not create socket:$!\n"; my($tempSocket, $clientAddr, $buffer); while (($tempSocket, $clientAddr) = $servSocket->accept()) { my ($clientPort, $clientIP) = sockaddr_in($clientAddr); my $clientIPNum = inet_ntoa($clientIP); my $clientHost = gethostbyaddr($clientIP, AF_INET); while (defined ($buffer = <$tempSocket>)) { print $buffer; if ($buffer =~ m/^HELLO (\d+.\d+)/) { my $version = $1; if ($version == "1.0") { #This doesn't work #Want to send "HELLO 1.0" back through the socke +t #Is the $tempSocket handle just wrong, or #can I not use print to do this? print $tempSocket "HELLO 1.0"; } } last; } }
  • Comment on Re^2: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?
by ikegami (Patriarch) on Oct 26, 2009 at 17:11 UTC

    Assuming you meant to send a newline after the HELLO,

    Side that initiates the handshake:

    print $sock "HELLO 1.0"; chomp( my $greet = <$sock> ); die if $greet ne 'HELLO 1.0'; ...continue...

    Side that responds to the handshake:

    chomp( my $greet = <$sock> ); die if $greet ne 'HELLO 1.0'; print $sock "HELLO 1.0"; ...continue...

    Without the newline, you'll need touse something other than <>. It won't return until a newline is received (by default).

Re^3: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?
by weismat (Friar) on Oct 26, 2009 at 17:33 UTC
    Two obvious problems:
    - Your client does not flush the buffer - thus nothing is sent, either call directly or check the autoflush property.
    - Your server can handle only client and needs to flush as well.
      No, all sockets are being flushed. IO::Socket turns on autoflushing on the underlying handle.
Re^3: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?
by NetWallah (Canon) on Oct 27, 2009 at 00:16 UTC
    Not sure if this is the cause of your problems, but there are some coding issues you should fix:
    if ($buffer =~ m/^HELLO (\d+.\d+)/) { my $version = $1; if ($version == "1.0") {
    should be:
    if (my ($version) = $buffer =~ m/^HELLO (\d+\.\d+)/) { ## Esca +pe the "." if ($version eq "1.0") { # Do a "text" compare, no +t numeric

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://803309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-18 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found