Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the return value from a socket connection 1

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
Hi there,
I am trying to open a socket connection to send some data, in order to get the response data from the other end.

I found the way to send the data over, but can any one point me to how I can receive the response data from "testhost"?


Code:
 1 use IO::Socket;
 2 my $sock = new IO::Socket::INET (
 3                                  PeerAddr => 'testhost',
 4                                  PeerPort => '7070',
 5                                  Proto => 'tcp',
 6                                 );
 7 die "Could not create socket: $!\n" unless $sock;
 8 print $sock "get number users";
 9 close($sock);
 
The while loop seemd to have made my code hang. Perhaps I am not getting the right data back here?

When I try to print the first line of the response data, I get this:

IO::Socket::INET=GLOB(0x824c72c)
 
Are you sure you're printing <$sock> and not $sock?
If your code still hangs, you can try and edit the $/ variable. <$sock> will return when a string ending with a newline (the value of $/) can be read, change this value to something else to define your "end-of-message".

Also see perldoc perlvar .
 
Thanks so much for this Knights.
Appending a newline character to the end of the data I was sending did just the trick, and I can print the response data now just fine.
 
hummm still seem to be one slight hangling problem.

I now have
Code:
while ($line = <$sock>) {
    chomp $line;
    print $line."++++++\n";
}

Looking at the browser source code for the output, i get:
Code:
line 1 data ++++++
line 2 data ++++++
line 3 data ++++++
.
.
.
line n data

Looking at this, it seems like the code is hanging trying to read in line n. Is there anyway to break this loop here so that it doesn't try to wait for more incoming data.
 
Either send something that your script will recognize to manually break out of the loop, or close the connection on the remote machine.

Code:
while (<$sock>) { [COLOR=blue]# will return eof if connection on $sock dies[/color]
    chomp;
    print "$_++++++\n";
    /^exit/ and last; [COLOR=blue]# break out if 'exit' received[/color]
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top