Re: help with sockets

2006-11-08 Thread Alejandro Santillan
> Untested code :
>
> use strict;
> use warnings;
> use IO::Select;
>
> # Your socket opening code here
>
> my $read_set = new IO::Select();
> $read_set->add($handle);
>
> my $file = ''; # accumulated stream data
>
> while (1) {
>
> my @ready = $read_set->can_read(0.1);
>
> # do any other stuff that you need before trying another read here
>
> next if not @ready; # no data to read yet, loop back
>
> my $buffer = '';
> my $ret = read_data ($handle, \$buffer);
> if ($ret < 0) {
> print "Error reading socket\n";
> } elsif ($bytes == 0) {
> print "EOF on socket\n";
> } else {
> print "buffer=$buffer\n";
> $file .= $buffer; # add to saved data
> my $found = index $file, '!END!;
> last if $found >= 0; # break out of loop
> }
> }
>
> # finish processing data
>
> exit;
>
> sub read_data {
> my $fh = shift;
> my $bufref = shift; # reference to read buffer
> my $len = shift || 8192;
>
> my $bytes = sysread ($fh, $$bufref, 1024);
> if (not defined $bytes or $bytes < 0) {
> return -1;
> } elsif ($bytes == 0) {
> return 0;
> }
> return $bytes;
>
> }
>
> __END__


Bill, this code you wrote is just precious. I think I won't arrive to write
it in a million years.
Besides, I was able to learn a lot from it, to correct some minor mistakes
and to test it. It works perfect now.
For the records, I type below the final code. Thank you!

#!/usr/bin/perl

#this code reads a socket that sends a message in chunks
#as the message has as terminator string !END! it will end its work
#when that string is read. Here it is:
#use strict;
use warnings;
use IO::Select;
# Your socket opening and request code here
ConnectAndRequest();
$finalreading=readthesocket();
print $finalreading;

exit;
### SUB PART 
sub readthesocket{
sub leomas(){
my $read_set = new IO::Select();
$read_set->add($handle);
my $file = ''; # accumulated stream data

while (1) {
my @ready = $read_set->can_read(0.1);
# do any other stuff that you need before trying another read here
next if not @ready; # no data to read yet, loop back
my $buffer = '';
my $ret = read_data ($handle, \$buffer);
if ($ret < 0) {
print "Error reading socket\n";
} elsif ($ret == 0) {
print "EOF on socket\n";
} else {
#print "buffer=$buffer\n";
$file .= $buffer; # add to saved data
my $found = index $file, '!END!';
last if $found >= 0; # break out of loop
}
}
# finish processing data
return $file;
}


sub read_data {
my $fh = shift;
my $bufref = shift; # reference to read buffer
my $len = shift || 8192;
my $bytes = sysread ($fh, $$bufref, 1024);
if (not defined $bytes or $bytes < 0) {
return -1;
} elsif ($bytes == 0) {
return 0;
}
return $bytes;
}
### END SUB PART 





___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-11-07 Thread $Bill Luebkert
Alejandro Santillan wrote:

> The $i reaches the number 57, before hanging. It seems that well before
> reaching
> the !END! string it reads some char undef or something.

You need to check $bytes for error and EOF before reading again.

>>>whereas:
>>>
>>>
>my $buffer;
>my $bytes = sysread ($handle, $buffer, 1024);
>if (not defined $bytes or $bytes < 0) {
>#err
>print "error!!!";
>} elsif ($bytes == 0) {
># EOF

print "EOF\n";

>}
>return $buffer;

The return would only be there if this is actually a subroutine.

> I have several questions here:
> I don't know what the EOF indicator should be. 

An EOF on the stream would be a 0 return from sysread.  Your !END!
pattern would be a logical EOF indication.

> I don't know how to read a
> second time, do I have to do select($handle) and then sysread again?

See below.

> What is to block?

Blocking is when you hang a read and there is no data coming in -
it will block your task from further execution until the read
completes or errs off.  You can avoid that by doing a select
until the select says there is data available or by using non-blocking
IO on the socket.  Non-blocking IO will return immediately from the
read even if there is no data (the return value will let you know if
there was data or not).

> In the first read I get:
> ^BF^D3765,4443734
> 
> !APID3765
> ^F^,938252,2 ,1386.25 ,FILL746419490 ,,|
> ^F^,938263,1 ,1385.75 ,FILL746428495 ,,|
> ^F^,938335,1 ,1386.25 ,FILL746498613 ,,|
> ^F^,938969,1 ,1385.50 ,FILL7471301676 ,,|
> ^F^,938972,1 ,1385.50 ,FILL7471331679 ,,|
> ^F^,939684,3 ,1389.50 ,FILL7478422788 ,,|
> ^F^,939684,1 ,1389.50 ,FILL7478422789 ,,|
> ^F^,939704,2 ,1754.75 ,FILL7478622814 ,,|
> ^F^,939704,1 ,1754.75 ,FILL7478622813 ,,|
> ^F^,939704,1 ,1754.75 ,FILL7478622812 ,,|
> 
> It lacks 3 or 4 lines.

How many characters is that ?  Doesn't look to be anywhere near your
8K read.  It must be coming in chunks of like 1K.  After you finish
one read, you can partially process whatever you can and then use
IO::Select::can_read before the next read (unless you don't mind
blocking).

Make sure you check for error (undef) and EOF (0) before reading again
or you could hang.  Make the code into a sub if it already isn't and
just keep calling it until it returns error, EOF or you get the EOR
pattern.

Untested code :

use strict;
use warnings;
use IO::Select;

# Your socket opening code here

my $read_set = new IO::Select();
$read_set->add($handle);

my $file = '';  # accumulated stream data

while (1) {

my @ready = $read_set->can_read(0.1);

# do any other stuff that you need before trying another read here

next if not @ready; # no data to read yet, loop back

my $buffer = '';
my $ret = read_data ($handle, \$buffer);
if ($ret < 0) {
print "Error reading socket\n";
} elsif ($bytes == 0) {
print "EOF on socket\n";
} else {
print "buffer=$buffer\n";
$file .= $buffer;   # add to saved data
my $found = index $file, '!END!;
last if $found >= 0;# break out of loop
}
}

# finish processing data

exit;

sub read_data {
my $fh = shift;
my $bufref = shift; # reference to read buffer
my $len = shift || 8192;

my $bytes = sysread ($fh, $$bufref, 1024);
if (not defined $bytes or $bytes < 0) {
return -1;
} elsif ($bytes == 0) {
return 0;
}
return $bytes;

}

__END__
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-11-07 Thread Alejandro Santillan
> > It seems that increasing the buffer more thant 1024 didn't help in all
> > cases. It seems that this stream of data comes in several packets and
the
> > terminator string is !END!
> > I've tried reading one byte at a time, using an $i as offset, and
checking
> > whenever the END pattern showed up
> >
> > my $buffer;
> > $i=1;
> > while($buffercomplete=~/END/){
>
> Don't you want !~ instead of =~ ?

You are completely right. I've changed for !~ and now it reads something.

>
> > $bytes = sysread ($handle, $buffer, 1,$i);
> > $buffercomplete=$buffercomplete.$buffer;
> > $i++;
> > }
> > return $buffer;
> >
> > but it doesn't read a line,
>
> What is in $i after the read ?  What should it be ?

The $i reaches the number 57, before hanging. It seems that well before
reaching
the !END! string it reads some char undef or something.



> > whereas:
> >
> >>>my $buffer;
> >>>my $bytes = sysread ($handle, $buffer, 1024);
> >>>if (not defined $bytes or $bytes < 0) {
> >>>#err
> >>>print "error!!!";
> >>>} elsif ($bytes == 0) {
> >>># EOF
> >>>}
> >>>return $buffer;
> >
> > does read seven lines, and increasing the 1024 to 8192 the reading is
the
> > same, and it does not include the !END! pattern
>
> I don't follow what you're saying here.  If you're not getting the
> EOR indicator, try a bigger read or read a second time if you don't
> get it in the first buffer.  Do a select before the read if you
> don't want to block.  How much data do you get on the first read ?
> How big is the full record supposed to be ?
>
> Please don't CC me on your replies.

I have several questions here:
I don't know what the EOF indicator should be. I don't know how to read a
second time, do I have to do select($handle) and then sysread again?
What is to block?
In the first read I get:
^BF^D3765,4443734

!APID3765
^F^,938252,2 ,1386.25 ,FILL746419490 ,,|
^F^,938263,1 ,1385.75 ,FILL746428495 ,,|
^F^,938335,1 ,1386.25 ,FILL746498613 ,,|
^F^,938969,1 ,1385.50 ,FILL7471301676 ,,|
^F^,938972,1 ,1385.50 ,FILL7471331679 ,,|
^F^,939684,3 ,1389.50 ,FILL7478422788 ,,|
^F^,939684,1 ,1389.50 ,FILL7478422789 ,,|
^F^,939704,2 ,1754.75 ,FILL7478622814 ,,|
^F^,939704,1 ,1754.75 ,FILL7478622813 ,,|
^F^,939704,1 ,1754.75 ,FILL7478622812 ,,|

It lacks 3 or 4 lines.






___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-11-07 Thread $Bill Luebkert
Alejandro Santillan wrote:

>>Alejandro Santillan wrote:
>>
>>
>>>Bill, I was using your solution successfully to read several messages
> 
> sent
> 
>>>by the server, but when trying to work some request,
>>>which had a longer answer, your buffer only gets:
>>
>>...
>>
>>>How could I modify your routine, which is the following:
>>>
>>>my $buffer;
>>>my $bytes = sysread ($handle, $buffer, 1024);
>>>if (not defined $bytes or $bytes < 0) {
>>>#err
>>>print "error!!!";
>>>} elsif ($bytes == 0) {
>>># EOF
>>>}
>>>return $buffer;
>>>
>>>In order to get the full answer and avoid the program to hang.
>>
>>1) Increase the size of the read to more than 1024.
>>2) Optionally add a print on the error and EOF lines and return
>>-1 and 0 to the caller to indicate err and EOF.  Test for those
>>two conditions in the calling code and handle them accordingly
>>(at least indicate to the user what happened with a diagnostic
>>message).
>>3) Before doing the next read, make sure you do a select on the
>>socket to make sure that data is available.
>>4) Since the data in the buffer is in a stream, you will need to
>>find where each record begins and ends and possibly concatenate
>>data from two reads to complete a single record when the record
>>spans the read boundary.
> 
> 
> It seems that increasing the buffer more thant 1024 didn't help in all
> cases. It seems that this stream of data comes in several packets and the
> terminator string is !END!
> I've tried reading one byte at a time, using an $i as offset, and checking
> whenever the END pattern showed up
> 
> my $buffer;
> $i=1;
> while($buffercomplete=~/END/){

Don't you want !~ instead of =~ ?

> $bytes = sysread ($handle, $buffer, 1,$i);
> $buffercomplete=$buffercomplete.$buffer;
> $i++;
> }
> return $buffer;
> 
> but it doesn't read a line,

What is in $i after the read ?  What should it be ?

> whereas:
> 
>>>my $buffer;
>>>my $bytes = sysread ($handle, $buffer, 1024);
>>>if (not defined $bytes or $bytes < 0) {
>>>#err
>>>print "error!!!";
>>>} elsif ($bytes == 0) {
>>># EOF
>>>}
>>>return $buffer;
> 
> does read seven lines, and increasing the 1024 to 8192 the reading is the
> same, and it does not include the !END! pattern

I don't follow what you're saying here.  If you're not getting the
EOR indicator, try a bigger read or read a second time if you don't
get it in the first buffer.  Do a select before the read if you
don't want to block.  How much data do you get on the first read ?
How big is the full record supposed to be ?

Please don't CC me on your replies.

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-11-07 Thread Alejandro Santillan
> Alejandro Santillan wrote:
>
> > Bill, I was using your solution successfully to read several messages
sent
> > by the server, but when trying to work some request,
> > which had a longer answer, your buffer only gets:
> ...
> > How could I modify your routine, which is the following:
> >
> > my $buffer;
> > my $bytes = sysread ($handle, $buffer, 1024);
> > if (not defined $bytes or $bytes < 0) {
> > #err
> > print "error!!!";
> > } elsif ($bytes == 0) {
> > # EOF
> > }
> > return $buffer;
> >
> > In order to get the full answer and avoid the program to hang.
>
> 1) Increase the size of the read to more than 1024.
> 2) Optionally add a print on the error and EOF lines and return
> -1 and 0 to the caller to indicate err and EOF.  Test for those
> two conditions in the calling code and handle them accordingly
> (at least indicate to the user what happened with a diagnostic
> message).
> 3) Before doing the next read, make sure you do a select on the
> socket to make sure that data is available.
> 4) Since the data in the buffer is in a stream, you will need to
> find where each record begins and ends and possibly concatenate
> data from two reads to complete a single record when the record
> spans the read boundary.

It seems that increasing the buffer more thant 1024 didn't help in all
cases. It seems that this stream of data comes in several packets and the
terminator string is !END!
I've tried reading one byte at a time, using an $i as offset, and checking
whenever the END pattern showed up

my $buffer;
$i=1;
while($buffercomplete=~/END/){
$bytes = sysread ($handle, $buffer, 1,$i);
$buffercomplete=$buffercomplete.$buffer;
$i++;
}
return $buffer;

but it doesn't read a line,
whereas:
> > my $buffer;
> > my $bytes = sysread ($handle, $buffer, 1024);
> > if (not defined $bytes or $bytes < 0) {
> > #err
> > print "error!!!";
> > } elsif ($bytes == 0) {
> > # EOF
> > }
> > return $buffer;
does read seven lines, and increasing the 1024 to 8192 the reading is the
same, and it does not include the !END! pattern



___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-11-01 Thread $Bill Luebkert
Alejandro Santillan wrote:

> Bill, I was using your solution successfully to read several messages sent
> by the server, but when trying to work some request,
> which had a longer answer, your buffer only gets:
...
> How could I modify your routine, which is the following:
> 
> my $buffer;
> my $bytes = sysread ($handle, $buffer, 1024);
> if (not defined $bytes or $bytes < 0) {
> #err
> print "error!!!";
> } elsif ($bytes == 0) {
> # EOF
> }
> return $buffer;
> 
> In order to get the full answer and avoid the program to hang.

1) Increase the size of the read to more than 1024.
2) Optionally add a print on the error and EOF lines and return
-1 and 0 to the caller to indicate err and EOF.  Test for those
two conditions in the calling code and handle them accordingly
(at least indicate to the user what happened with a diagnostic
message).
3) Before doing the next read, make sure you do a select on the
socket to make sure that data is available.
4) Since the data in the buffer is in a stream, you will need to
find where each record begins and ends and possibly concatenate
data from two reads to complete a single record when the record
spans the read boundary.

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-11-01 Thread Alejandro Santillan


>> The output was
>> 8=FIX.4.0.C35=A52=10/26/2006 8:16:10 AM100=PFG10=999
>> What is simply correct, but if I put 58 instead of 57, the program hangs
>> indefinitely.
>> Obviously I don't know the lengh fo the message beforehand.
>>
>> Anyone has any idea why this happens and how to avoid it?
>
>Try just reading a max size buffer and taking what you get:
>
>my $buffer;
>my $bytes = sysread ($handle, $buffer, 1024);
>if (not defined $bytes or $bytes < 0) {
># err
>} elsif ($bytes == 0) {
># EOF
>}
># we now have $bytes worth of data in $buffer.
># process the data and do a select before the next read
># unless blocking is OK

Bill, I was using your solution successfully to read several messages sent
by the server, but when trying to work some request,
which had a longer answer, your buffer only gets:

!APID3765PFGPFG!START!^C^,D3765,10/30/2006,1,0,ESZ6,1383.25,D3765-904495,|
^C^,D3765,10/31/2006,1,0,ESZ6,1381,D3765-907899,|

which is the first part of the answer. The complete answer should be:

!APID3765PFGPFG!START!^C^,D3765,10/30/2006,1,0,ESZ6,1383.25,D3765-904495,|
^C^,D3765,10/31/2006,1,0,ESZ6,1381,D3765-907899,|
!APID3765PFGPFG^R^,911655,0,0,ESZ6 ,138650,LIMIT,DAY,0 ,0,0,01-Nov-06 N@
7:39:48 AM ,F ,1386.5 ,GLOBE,0,0,1 ,BUY ,1 |
^R^,911690,0,0,ESZ6 ,138725,LIMIT,DAY,0 ,0,0,01-Nov-06 N@ 7:50:36 AM ,F
,1387.25 ,GLOBE,0,0,2 ,BUY ,2 |
^R^,911703,0,0,ESZ6 ,138600,LIMIT,60,0 ,0,0,01-Nov-06 N@ 7:57:10 AM ,F
,1386. ,GLOBE,0,0,2 ,BUY ,2 |
!APID3765PFGPFG^P^,904495,D3765,11-01-2006,1,0,ESZ6,1383.25,1381.7500,-75|
^P^,907899,D3765,11-01-2006,1,0,ESZ6,1381.,1381.7500,37.5|
^P^,911655,D3765,11-01-2006,1,0,ESZ6,1386.5,1381.7500,-237.5|
^P^,911690,D3765,11-01-2006,2,0,ESZ6,1387.25,1381.7500,-550|
^P^,911703,D3765,11-01-2006,2,0,ESZ6,1386.,1381.7500,-425|
!APID3765PFGPFG^F^,911655,1 ,1386.50 ,FILL720651851,0, ,|
^F^,911655,1 ,1386.50 ,FILL720651851 ,|
^F^,911690,2 ,1387.25 ,FILL720686890,0, ,|
^F^,911690,2 ,1387.25 ,FILL720686890 ,|
^F^,911703,2 ,1386.00 ,FILL7206991213,0, ,|
^F^,911703,2 ,1386.00 ,FILL7206991213 ,|

How could I modify your routine, which is the following:

my $buffer;
my $bytes = sysread ($handle, $buffer, 1024);
if (not defined $bytes or $bytes < 0) {
#err
print "error!!!";
} elsif ($bytes == 0) {
# EOF
}
return $buffer;

In order to get the full answer and avoid the program to hang.

Thank you again,

Alejandro


PS: I don't know what it means,what do you mean by "unless blocking is OK?".


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-11-01 Thread Alejandro Santillan
>> The output was
>> 8=FIX.4.0.C35=A52=10/26/2006 8:16:10 AM100=PFG10=999
>> What is simply correct, but if I put 58 instead of 57, the program hangs
>> indefinitely.
>> Obviously I don't know the lengh fo the message beforehand.
>>
>> Anyone has any idea why this happens and how to avoid it?
>
>Try just reading a max size buffer and taking what you get:
>
>my $buffer;
>my $bytes = sysread ($handle, $buffer, 1024);
>if (not defined $bytes or $bytes < 0) {
># err
>} elsif ($bytes == 0) {
># EOF
>}
># we now have $bytes worth of data in $buffer.
># process the data and do a select before the next read
># unless blocking is OK

Bill, I was using your solution successfully to read several messages sent
by the server, but when trying to work some request,
which had a longer answer, your buffer only gets:

!APID3765PFGPFG!START!^C^,D3765,10/30/2006,1,0,ESZ6,1383.25,D3765-904495,|
^C^,D3765,10/31/2006,1,0,ESZ6,1381,D3765-907899,|

which is the first part of the answer. The complete answer should be:

!APID3765PFGPFG!START!^C^,D3765,10/30/2006,1,0,ESZ6,1383.25,D3765-904495,|
^C^,D3765,10/31/2006,1,0,ESZ6,1381,D3765-907899,|
!APID3765PFGPFG^R^,911655,0,0,ESZ6 ,138650,LIMIT,DAY,0 ,0,0,01-Nov-06 N@
7:39:48 AM ,F ,1386.5 ,GLOBE,0,0,1 ,BUY ,1 |
^R^,911690,0,0,ESZ6 ,138725,LIMIT,DAY,0 ,0,0,01-Nov-06 N@ 7:50:36 AM ,F
,1387.25 ,GLOBE,0,0,2 ,BUY ,2 |
^R^,911703,0,0,ESZ6 ,138600,LIMIT,60,0 ,0,0,01-Nov-06 N@ 7:57:10 AM ,F
,1386. ,GLOBE,0,0,2 ,BUY ,2 |
!APID3765PFGPFG^P^,904495,D3765,11-01-2006,1,0,ESZ6,1383.25,1381.7500,-75|
^P^,907899,D3765,11-01-2006,1,0,ESZ6,1381.,1381.7500,37.5|
^P^,911655,D3765,11-01-2006,1,0,ESZ6,1386.5,1381.7500,-237.5|
^P^,911690,D3765,11-01-2006,2,0,ESZ6,1387.25,1381.7500,-550|
^P^,911703,D3765,11-01-2006,2,0,ESZ6,1386.,1381.7500,-425|
!APID3765PFGPFG^F^,911655,1 ,1386.50 ,FILL720651851,0, ,|
^F^,911655,1 ,1386.50 ,FILL720651851 ,|
^F^,911690,2 ,1387.25 ,FILL720686890,0, ,|
^F^,911690,2 ,1387.25 ,FILL720686890 ,|
^F^,911703,2 ,1386.00 ,FILL7206991213,0, ,|
^F^,911703,2 ,1386.00 ,FILL7206991213 ,|

How could I modify your routine, which is the following:

my $buffer;
my $bytes = sysread ($handle, $buffer, 1024);
if (not defined $bytes or $bytes < 0) {
#err
print "error!!!";
} elsif ($bytes == 0) {
# EOF
}
return $buffer;

In order to get the full answer and avoid the program to hang.

Thank you again,

Alejandro

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-10-27 Thread Chris Wagner
At 12:53 PM 10/26/2006 -0300, Alejandro Santillan wrote:
>Thanks Rob. I think that my problem is just the terminator character, but on
>the string sent by the server, and not mine.

U should be able to manually telnet to the port and do the transaction by
just typing/pasting the string.  Does that work?  Does the proper response
show up on the screen?


>#1. This failed miserably. The program hanged indefinitely
># @answerfromserver=<$handle>;
>#$answerfromserver=join "",@answerfromserver;
>#print $anserfromserver;

If there was no new line sequence transmitted by the other end then this
would fail.  Remember that <> is the line input operator.  It won't fire
until it sees that new line on the input.  undef'ing $/ might make it work.


>#2. This failed also miserably. The program hanged indefinitely
>#my $byte;
>#while (sysread($handle, $byte, 1) == 1) {
>#print $byte;
>#}

That should have worked, since it's essentially the same as ur try #3.
Maybe declaring binmode on $handle would make a difference.  Perhaps some
unicode strangeness is going on.


>#3. This worked, after several tries when I gess correctly the right length
>of the answer to be 55 bytes!!!
>for($i=0;$i<57;$i++){
>sysread($handle,$byte,1);
>print $byte;
>}

I noticed in ur original code that ur reading and writing to the same
handle.  According to perldoc mixing print's and sysread's on the same
handle is a no-no.  That's because they're using different I/O schemes which
can collide.  Have u tried it with a normal read?  "Conventional wisdom" has
it that sysread's should only be used with syswrite's.  $return = read
$handle, $buffer, 99; should give u what u want.





--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede malis"

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-10-26 Thread $Bill Luebkert
Alejandro Santillan wrote:

> The output was:
> 8=FIX.4.0.C35=A52=10/26/2006 8:16:10 AM100=PFG10=999
> What is simply correct, but if I put 58 instead of 57, the program hangs
> indefinitely.
> Obviously I don't know the lengh fo the message beforehand.
> 
> Anyone has any idea why this happens and how to avoid it?

Try just reading a max size buffer and taking what you get:

my $buffer;
my $bytes = sysread ($handle, $buffer, 1024);
if (not defined $bytes or $bytes < 0) {
# err
} elsif ($bytes == 0) {
# EOF
}
# we now have $bytes worth of data in $buffer.
# process the data and do a select before the next read
# unless blocking is OK
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-10-26 Thread Alejandro Santillan
Thanks Rob. I think that my problem is just the terminator character, but on
the string sent by the server, and not mine.

I am using this script now:
use IO::Socket;

# create a tcp connection to the specified host and port
$handle = IO::Socket::INET->new(Proto => "tcp",
PeerAddr  => $host,
PeerPort  => $port,
Type => SOCK_STREAM)
   or die "can't connect to port $port on $host: $!";


$handle->autoflush(1);  # so output gets there right away
print STDERR "[Connected to $host:$port]\n";

#Now I begin to talk to the server:
#this is the
$message="8=FIX.4.0.C9=3935=A9998=D37659997=44437349996=Bara10=180";
#and I send it in the face of the server:
print $handle "$message\n";

#Now I read the response, and here is the problem:

#1. This failed miserably. The program hanged indefinitely
# @answerfromserver=<$handle>;
#$answerfromserver=join "",@answerfromserver;
#print $anserfromserver;

#2. This failed also miserably. The program hanged indefinitely
#my $byte;
#while (sysread($handle, $byte, 1) == 1) {
#print $byte;
#}

#3. This worked, after several tries when I gess correctly the right length
of the answer to be 55 bytes!!!
for($i=0;$i<57;$i++){
sysread($handle,$byte,1);
print $byte;
}


The output was:
8=FIX.4.0.C35=A52=10/26/2006 8:16:10 AM100=PFG10=999
What is simply correct, but if I put 58 instead of 57, the program hangs
indefinitely.
Obviously I don't know the lengh fo the message beforehand.

Anyone has any idea why this happens and how to avoid it?

Thanks

Alejandro


- Original Message - 
From: "Sisyphus" <[EMAIL PROTECTED]>
To: "Alejandro Santillan" <[EMAIL PROTECTED]>;

Cc: "perl-win32-user" 
Sent: Thursday, October 26, 2006 1:24 AM
Subject: Re: help with sockets



- Original Message - 
From: "Alejandro Santillan" <[EMAIL PROTECTED]>
.
.
>
> use IO::Socket;
>
> $message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺";

#Try instead:
$message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺\n";

I don't know if that will fix the problem, but it's worth a try.

Cheers,
Rob


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

___
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-10-26 Thread Alejandro Santillan
Thanks Rob. I think that my problem is just the terminator character, but on
the string sent by the server, and not mine.

I am using this script now:
use IO::Socket;

# create a tcp connection to the specified host and port
$handle = IO::Socket::INET->new(Proto => "tcp",
PeerAddr  => $host,
PeerPort  => $port,
Type => SOCK_STREAM)
   or die "can't connect to port $port on $host: $!";


$handle->autoflush(1);  # so output gets there right away
print STDERR "[Connected to $host:$port]\n";

#Now I begin to talk to the server:
#this is the
$message="8=FIX.4.0.C9=3935=A9998=D37659997=44437349996=Bara10=180";
#and I send it in the face of the server:
print $handle "$message\n";

#Now I read the response, and here is the problem:

#1. This failed miserably. The program hanged indefinitely
# @answerfromserver=<$handle>;
#$answerfromserver=join "",@answerfromserver;
#print $anserfromserver;

#2. This failed also miserably. The program hanged indefinitely
#my $byte;
#while (sysread($handle, $byte, 1) == 1) {
#print $byte;
#}

#3. This worked, after several tries when I gess correctly the right length
of the answer to be 55 bytes!!!
for($i=0;$i<57;$i++){
sysread($handle,$byte,1);
print $byte;
}


The output was:
8=FIX.4.0.C35=A52=10/26/2006 8:16:10 AM100=PFG10=999
What is simply correct, but if I put 58 instead of 57, the program hangs
indefinitely.
Obviously I don't know the lengh fo the message beforehand.

Anyone has any idea why this happens and how to avoid it?

Thanks

Alejandro


- Original Message - 
From: "Sisyphus" <[EMAIL PROTECTED]>
To: "Alejandro Santillan" <[EMAIL PROTECTED]>;

Cc: "perl-win32-user" 
Sent: Thursday, October 26, 2006 1:24 AM
Subject: Re: help with sockets



- Original Message - 
From: "Alejandro Santillan" <[EMAIL PROTECTED]>
.
.
>
> use IO::Socket;
>
> $message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺";

#Try instead:
$message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺\n";

I don't know if that will fix the problem, but it's worth a try.

Cheers,
Rob


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: help with sockets

2006-10-25 Thread Sisyphus

- Original Message - 
From: "Alejandro Santillan" <[EMAIL PROTECTED]>
.
.
>
> use IO::Socket;
>
> $message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺";

#Try instead:
$message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺\n";

I don't know if that will fix the problem, but it's worth a try.

Cheers,
Rob


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


help with sockets

2006-10-25 Thread Alejandro Santillan
I need to connect to a TCP server that uses a protocol called fix (financial
information exchange)
Basically the server receives a text string and answers with a text string
also.
The protocol is pretty simple, and it consists in a series of fields
separated by the char alt+1:☺

$message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺";
It is supposed that I will receive an answer from the server that should be:
$message2="8=FIX.4.0.c☺35=A☺52=10/24/2006 10:30:34 AM☺100=PFG☺";
The server talks TCP, as stated by its owner, and it can be accessed at some
ip and some port.

Well, trying to keep it simple, I wrote this script:


use IO::Socket;

$message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺";
$host="11.35.72.194";
$port="1000";
$handle = IO::Socket::INET->new(Proto => "tcp",
PeerAddr  => $host,
PeerPort  => $port,
#  Reuse=> "1",
#  Listen => "1",
Type => SOCK_STREAM)
or die "can't connect to port $port on $host: $!";

$handle->autoflush(1);  # so output gets there right away
print STDERR "[Connected to $host:$port]\n";
#up to here, the connection worked fine
if($handle){print "ok, it worked!\n";}

#now I write to the socket:
print $handle $message1;

#now I hope to get the answer
$response=<$handle>;
print $response;
#I should have received the same as $message2. Instead, the run hangs and
nothing happen.

Am I doing the right thing to enter in conversation with an TCP server?

Thank you to anyone that can shed some litght. Maybe I should ask something
more to the server owner? What?


Alejandro





___
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


help with sockets

2006-10-25 Thread Alejandro Santillan
I need to connect to a TCP server that uses a protocol called fix (financial
information exchange)
Basically the server receives a text string and answers with a text string
also.
The protocol is pretty simple, and it consists in a series of fields
separated by the char alt+1:☺

$message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺";
It is supposed that I will receive an answer from the server that should be:
$message2="8=FIX.4.0.c☺35=A☺52=10/24/2006 10:30:34 AM☺100=PFG☺";
The server talks TCP, as stated by its owner, and it can be accessed at some
ip and some port.

Well, trying to keep it simple, I wrote this script:


use IO::Socket;

$message1="8=FIX.4.0.c☺35=A☺9998=D3765☺9997=4443734☺9996=Bara☺";
$host="11.35.72.194";
$port="1000";
$handle = IO::Socket::INET->new(Proto => "tcp",
PeerAddr  => $host,
PeerPort  => $port,
#  Reuse=> "1",
#  Listen => "1",
Type => SOCK_STREAM)
or die "can't connect to port $port on $host: $!";

$handle->autoflush(1);  # so output gets there right away
print STDERR "[Connected to $host:$port]\n";
#up to here, the connection worked fine
if($handle){print "ok, it worked!\n";}

#now I write to the socket:
print $handle $message1;

#now I hope to get the answer
$response=<$handle>;
print $response;
#I should have received the same as $message2. Instead, the run hangs and
nothing happen.

Am I doing the right thing to enter in conversation with an TCP server?

Thank you to anyone that can shed some litght. Maybe I should ask something
more to the server owner? What?


Alejandro





___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs