Problem with perl sockets

Problem with perl sockets

am 05.10.2005 01:00:20 von Daniel Moree

I'm attempting to use a perl script to interface with my Visual Basic 6
program using Winsock. I've got my program setup to connect and works
great if i connect to another winsock program, but it acts funny
connecting to my perl script. The Perl code is below:

#!c:/Perl/bin/Perl.exe
use IO::Socket;
$server = IO::Socket::INET->new(LocalAddr => '10.40.0.10',
LocalPort => '8777',
Proto => 'tcp',
Listen => 1,
Reuse => 1);

die "ERROR: $!\n" unless $server;

print "Waiting on connections...\n";
while($client = $server->accept()){
print "Connection made, reading data...\n";
print <$client>;
print "Connection closed...\n";
}

$server->close();

All the program is supposed to do is open a socket on port 8777 for tcp.
Listen for any incomming connections then read the one line of data
comming in. I've read on the internet and in my perl in a nutshell book
that the above code should work. But they all say the same thing. You
must get the line of data and scan it for a character that lets the perl
script that the line is done. Problem is, when the script runs, i get as
far a connection made then it will do nothing until i close the VB Winsock.

Anyone got a reader for sockets that will know when to stop reading? I
haven't found any examples on the internet on how to do it, just that
everyone says it can be done. I'd really appreciate the help!

Daniel Moree

Re: Problem with perl sockets

am 05.10.2005 20:46:51 von Jim Gibson

In article <9OD0f.1300$ZY2.881@bignews6.bellsouth.net>, Daniel Moree
wrote:

> I'm attempting to use a perl script to interface with my Visual Basic 6
> program using Winsock. I've got my program setup to connect and works
> great if i connect to another winsock program, but it acts funny
> connecting to my perl script. The Perl code is below:
>
> #!c:/Perl/bin/Perl.exe
> use IO::Socket;
> $server = IO::Socket::INET->new(LocalAddr => '10.40.0.10',
> LocalPort => '8777',
> Proto => 'tcp',
> Listen => 1,
> Reuse => 1);
>
> die "ERROR: $!\n" unless $server;
>
> print "Waiting on connections...\n";
> while($client = $server->accept()){
> print "Connection made, reading data...\n";
> print <$client>;
> print "Connection closed...\n";
> }
>
> $server->close();
>
> All the program is supposed to do is open a socket on port 8777 for tcp.
> Listen for any incomming connections then read the one line of data
> comming in. I've read on the internet and in my perl in a nutshell book
> that the above code should work. But they all say the same thing. You
> must get the line of data and scan it for a character that lets the perl
> script that the line is done. Problem is, when the script runs, i get as
> far a connection made then it will do nothing until i close the VB Winsock.
>
> Anyone got a reader for sockets that will know when to stop reading? I
> haven't found any examples on the internet on how to do it, just that
> everyone says it can be done. I'd really appreciate the help!

Are you sending an end-of-line terminator in the client program? You
are using line-oriented reading in the server, so it will block until
it receives a valid end-of-line marker, which can vary on different
platforms (although you seem to be running both programs on the same
system) or until the client closes the socket.

You should also try byte-oriented reading if your line-oriented reading
is failing:

$nread = $client->read($buf,80);

See documentation for IO::Handle and IO::Socket modules and
'perldoc perlipc'.

Write a small Perl client to connect to your server for testing.

Re: Problem with perl sockets

am 06.10.2005 03:59:33 von Daniel Moree

Jim Gibson wrote:
> In article <9OD0f.1300$ZY2.881@bignews6.bellsouth.net>, Daniel Moree
> wrote:
>
>
>>I'm attempting to use a perl script to interface with my Visual Basic 6
>>program using Winsock. I've got my program setup to connect and works
>>great if i connect to another winsock program, but it acts funny
>>connecting to my perl script. The Perl code is below:
>>
>>#!c:/Perl/bin/Perl.exe
>>use IO::Socket;
>>$server = IO::Socket::INET->new(LocalAddr => '10.40.0.10',
>> LocalPort => '8777',
>> Proto => 'tcp',
>> Listen => 1,
>> Reuse => 1);
>>
>>die "ERROR: $!\n" unless $server;
>>
>>print "Waiting on connections...\n";
>>while($client = $server->accept()){
>> print "Connection made, reading data...\n";
>> print <$client>;
>> print "Connection closed...\n";
>>}
>>
>>$server->close();
>>
>>All the program is supposed to do is open a socket on port 8777 for tcp.
>>Listen for any incomming connections then read the one line of data
>>comming in. I've read on the internet and in my perl in a nutshell book
>>that the above code should work. But they all say the same thing. You
>>must get the line of data and scan it for a character that lets the perl
>>script that the line is done. Problem is, when the script runs, i get as
>>far a connection made then it will do nothing until i close the VB Winsock.
>>
>>Anyone got a reader for sockets that will know when to stop reading? I
>>haven't found any examples on the internet on how to do it, just that
>>everyone says it can be done. I'd really appreciate the help!
>
>
> Are you sending an end-of-line terminator in the client program? You
> are using line-oriented reading in the server, so it will block until
> it receives a valid end-of-line marker, which can vary on different
> platforms (although you seem to be running both programs on the same
> system) or until the client closes the socket.
>
> You should also try byte-oriented reading if your line-oriented reading
> is failing:
>
> $nread = $client->read($buf,80);
>
> See documentation for IO::Handle and IO::Socket modules and
> 'perldoc perlipc'.
>
> Write a small Perl client to connect to your server for testing.

the platform is windows 2000. I've tried \n to end the line, but maybe
its in the documentation. I'll write a perl client. I don't want to use
byte length to get the information because the line varies. It contains
the name of a person, their company name, and a serial number. the only
way to do it is to add 255, 255, 4, and 20. But that would result in odd
amounts. If you have any ideas for linux or windows 2000, just let me know.

Thanks for you help, i really appreciate it.

Re: Problem with perl sockets

am 06.10.2005 04:43:01 von Daniel Moree

Jim Gibson wrote:
> In article <9OD0f.1300$ZY2.881@bignews6.bellsouth.net>, Daniel Moree
> wrote:
>
>
>>I'm attempting to use a perl script to interface with my Visual Basic 6
>>program using Winsock. I've got my program setup to connect and works
>>great if i connect to another winsock program, but it acts funny
>>connecting to my perl script. The Perl code is below:
>>
>>#!c:/Perl/bin/Perl.exe
>>use IO::Socket;
>>$server = IO::Socket::INET->new(LocalAddr => '10.40.0.10',
>> LocalPort => '8777',
>> Proto => 'tcp',
>> Listen => 1,
>> Reuse => 1);
>>
>>die "ERROR: $!\n" unless $server;
>>
>>print "Waiting on connections...\n";
>>while($client = $server->accept()){
>> print "Connection made, reading data...\n";
>> print <$client>;
>> print "Connection closed...\n";
>>}
>>
>>$server->close();
>>
>>All the program is supposed to do is open a socket on port 8777 for tcp.
>>Listen for any incomming connections then read the one line of data
>>comming in. I've read on the internet and in my perl in a nutshell book
>>that the above code should work. But they all say the same thing. You
>>must get the line of data and scan it for a character that lets the perl
>>script that the line is done. Problem is, when the script runs, i get as
>>far a connection made then it will do nothing until i close the VB Winsock.
>>
>>Anyone got a reader for sockets that will know when to stop reading? I
>>haven't found any examples on the internet on how to do it, just that
>>everyone says it can be done. I'd really appreciate the help!
>
>
> Are you sending an end-of-line terminator in the client program? You
> are using line-oriented reading in the server, so it will block until
> it receives a valid end-of-line marker, which can vary on different
> platforms (although you seem to be running both programs on the same
> system) or until the client closes the socket.
>
> You should also try byte-oriented reading if your line-oriented reading
> is failing:
>
> $nread = $client->read($buf,80);
>
> See documentation for IO::Handle and IO::Socket modules and
> 'perldoc perlipc'.
>
> Write a small Perl client to connect to your server for testing.

OK, i've checked out the perl line feed and carriage return characters.
They are just general ascii codes. My Visual Basic 6 program does
something like this

Winsock1.SendData "REG:[user's name];[company name];[serial number]" +
chr(13)

now that line sends the relevent information to the perl script and
sends a carriage return after the text. if i use just a CR, it prints my
closing connection text ontop of the information, if i use both CR and
LF in that order, it works fine. Is there a specific method that reads
only to a line other than read()? i'm just using the most common method
i could find on the internet. Never done perl and sockets. The perl guru
at school has never done it either and he's been messing with perl for
years.

Re: Problem with perl sockets

am 06.10.2005 16:03:54 von Daniel Moree

Daniel Moree wrote:
> I'm attempting to use a perl script to interface with my Visual Basic 6
> program using Winsock. I've got my program setup to connect and works
> great if i connect to another winsock program, but it acts funny
> connecting to my perl script. The Perl code is below:
>
> #!c:/Perl/bin/Perl.exe
> use IO::Socket;
> $server = IO::Socket::INET->new(LocalAddr => '10.40.0.10',
> LocalPort => '8777',
> Proto => 'tcp',
> Listen => 1,
> Reuse => 1);
>
> die "ERROR: $!\n" unless $server;
>
> print "Waiting on connections...\n";
> while($client = $server->accept()){
> print "Connection made, reading data...\n";
> print <$client>;
> print "Connection closed...\n";
> }
>
> $server->close();
>
> All the program is supposed to do is open a socket on port 8777 for tcp.
> Listen for any incomming connections then read the one line of data
> comming in. I've read on the internet and in my perl in a nutshell book
> that the above code should work. But they all say the same thing. You
> must get the line of data and scan it for a character that lets the perl
> script that the line is done. Problem is, when the script runs, i get as
> far a connection made then it will do nothing until i close the VB Winsock.
>
> Anyone got a reader for sockets that will know when to stop reading? I
> haven't found any examples on the internet on how to do it, just that
> everyone says it can be done. I'd really appreciate the help!
>
> Daniel Moree

OK guys, i got a working copy. Don't know why it started working, but it
did! Below is the code if anyone wants to know. If you have any ideas on
how to clean it up, let me know, I'll appreciate it!

#!c:/Perl/bin/Perl.exe
use IO::Socket qw(:DEFAULT :crlf);
$server = IO::Socket::INET->new(LocalAddr => 'localhost',
LocalPort => 8777,
Proto => 'tcp',
Listen => 5,
Reuse => 1);
die "Unable to start server: $!" unless $server;

print "Server started! Listening for connections...\n";

while($client = $server->accept()){
#client->autoflush(1);
while(<$client>){
print $_;
} continue {
}
close $client;
}

$server->close();