Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

problem with reading from socket

by MacScissor (Acolyte)
on Dec 03, 2012 at 11:04 UTC ( #1006848=perlquestion: print w/replies, xml ) Need Help??
MacScissor has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I have the following code:
#!/usr/bin/perl use strict; use warnings; use Socket; use FileHandle; my $request; my $port = 6699; my $proto = getprotobyname('tcp'); socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!\n"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsockopt: + $!\n"; my $socketaddress = sockaddr_in($port, INADDR_ANY); bind(SERVER, $socketaddress) or die "bind: $!\n"; listen(SERVER, SOMAXCONN) or die "listen: $!\n"; print "starting on port: $port...\n"; my $client; for (; $client = accept(CLIENT, SERVER); close CLIENT) { print CLIENT "ready\015\012"; while (1) { CLIENT->autoflush(1); my $request; print $request = <CLIENT>; #print $request; chomp $request; #chomp $request; if ($request =~ /^help/) { print CLIENT "help!\015\012"; } elsif ($request =~ /^quit/) { last; } } }
This script is executed under Windows. I connect to the socket via telnet with putty. The first line that I send via telnet is received by the server, but I have strange chars before the received command. For example, when i type "help", the server receives the following: Thanks in advance!

Replies are listed 'Best First'.
Re: problem with reading from socket
by rjt (Deacon) on Dec 03, 2012 at 11:56 UTC

    You most likely have tried to use a "telnet" connection in PuTTY, which results in some control characters being sent when the connection is established. You need a raw connection. Select "Raw" instead of "Telnet" when you create the connection, or try this from a command prompt (assuming putty.exe is in your path or in the current directory):

    putty -raw localhost 6699

    Then it works.

    Secondly, have you considered using the OO interface to sockets in Perl? In can clean up your code significantly:

    #!/usr/bin/perl use strict; use warnings; use IO::Socket qw/:DEFAULT :crlf/; use FileHandle; use 5.014; my $port = 6699; my $server = IO::Socket::INET->new( Proto => 'tcp', LocalHost => 'localhost', LocalPort => $port, Listen => 1, Reuse => 1); die "Server setup failed: $!" unless $server; say "starting on port: $port..."; while (my $client = $server->accept()) { print $client "ready$CRLF"; local $/ = LF; # Be robust about accepting LF or CRLF endings on +input CLIENT: while (<$client>) { s/$CR?$LF//; # chomp CRLF or LF say "Client request: `$_'"; for ($_) { print $client "help!$CRLF" when /^help/; last CLIENT when 'quit'; } } say "Disconnected"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006848]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-06-24 04:06 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.