473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Perl socket problem

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
Oct 4 '05 #1
3 21522
I ran it on windows XP and tested it with telnet... Make sure the VB client sends a newline... You made me late for psychology class :p
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use IO::Socket;
  7.  
  8. my $client;
  9. my $socket;
  10. my $message;
  11.  
  12. $socket = IO::Socket::INET->new(
  13.         "Proto" => "tcp",
  14.         "LocalPort" => "8177",
  15.         "Listen" => 1) or die "ERROR: $!\n";
  16.  
  17. print "Waiting for connection...\n";
  18.  
  19. # Blocks untill connection is made
  20. $client = $socket->accept();
  21.  
  22. print "Connection received\n";
  23.  
  24. # Blocks untill message is received
  25. $message = <$client>;
  26.  
  27. print "Incoming message: $message\n";
  28.  
  29. close $client;
  30. close $socket;
  31.  
Oct 5 '05 #2
#!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!


In my opinion you should use function $char=getc($server) in the loop and
analyze text you reach in this way.
In my programs it works very well.

Nov 22 '05 #3
>> 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![/color]

>In my opinion you should use function $char=getc($server) in the loop and
>analyze text you reach in this way.
>In my programs it works very well.
See: http://poe.perl.org/?POE_RFCs/Window..._compatibility

The problem is that non-blocking sockets don't work on NT. All sockets are forced to block and wait for a new-line or EOF. Using getc() and testing for some end marker is one way around it, but using sysread() works better. It reads as many chars as are in the buffer without blocking.

Ex.

$retries = 30;
$bytes_to_read = 1024;
do {
while ( sysread( $socket, $data, $bytes_to_read ) == $bytes_to_read )
{
$xyz .= $data;
}
last if $xyz =~ /endmarker/;
sleep 1;
} while --$retries;

This will try to read from the socket for about 30 seconds or until the end marker is found.
BTW, DO NOT try to use read() or recv() on the socket! See the perl docs on sysread() for more info.
Jun 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
0
by: Danny Jensen | last post by:
I need to test if certain processes on a unix box were running. I wanted to use whatsup gold to do the testing. First I needed to go to the whatsup configure>monitors & services menu to add this...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
4
by: Ignoramus6539 | last post by:
There were some strange requests to my server asking for config.php file (which I do not have in the requested location). I did some investigation. Seems to be a virus written in perl,...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
0
by: muraley | last post by:
Hi, This client-server script does bi-directional communication. Setup i used: The server script on windows 2003 server and the client on a linux machine. Since i faced issues in getting the...
0
by: crawfordr | last post by:
Hello, I have created a perl script that connects to a specific socket (Ip address/port) using protocall of TCP. It is the server socket script. There is also coding to manage multiple handles by...
0
by: cheguvera | last post by:
Hi All, I want to have a Perl script which will act as HTTP client. Basically, it should be able to get (read) a given HTML page from server. I have this code with me, #...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.