LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2005, 06:29 AM   #1
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Rep: Reputation: 30
problems reading back from socket using Perl


Hi guys,

I have a socket set up with IO::Socket::INET. It works in that I am able to write to the socket, however I can't capture what the remote host sends back.

I know the connection works, because I can use telnet to drive the dialogue manually and when I send the 'status' request, I get a dump of data back. How can I get my socket to capture this so that I can do something useful with it?

Here's a snip of the code I'm using:

Code:
 my $sock = new IO::Socket::INET (
      PeerAddr => "$hostname",
      PeerPort => "$port",
      Blocking => 0,
      Proto => 'tcp',
 );

 print $sock "Action: Login\r\n";
 print $sock "UserName: $foo\r\n";
 print $sock "Secret: $bar\r\n\r\n";
 print $sock "Action: status\r\n\r\n";

 $/="\r\n";
 open(FH,">>/home/chris/statusdump.txt");
 while(my $data=<$sock>){
      $data =~ /[a-z]/ || next;
      print FH $data;
 }
 close FH;

cheers


christo

Last edited by chr15t0; 11-16-2005 at 07:54 AM.
 
Old 11-16-2005, 08:35 AM   #2
fvgestel
LQ Newbie
 
Registered: Oct 2005
Location: Netherlands
Posts: 21

Rep: Reputation: 15
Your telnet client does a lot more then just read and write usernames and commands to the socket. There's a lot of handshaking going on before any userdata is passed between server and client. I think the smartest thing to do is look at the Net::Telnet module, which takes care of all the handshake issues.

Hope this helps...
 
Old 11-16-2005, 08:40 AM   #3
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
don't worry - I fixed it with seek:

# rewind to first byte
seek ( $sock , 0 , 0 );

and then read from $sock to get what bounces back..


christo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with reading back serial binary data esi-eric Linux - Software 15 10-03-2018 09:13 AM
Why Does a Broadcast on a Raw Socket get echoed back to me? btn_paragon Programming 5 09-09-2005 07:32 AM
fgets never stop reading from socket!? Thinking Programming 1 04-06-2005 09:38 AM
reading back a tape Wimpie22 Linux - General 1 09-13-2003 07:15 PM
Socket and Select() problems strikernzl Programming 2 09-01-2003 08:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration