ghostils has asked for the wisdom of the Perl Monks concerning the following question:

Kind of a strange problem. Here is the code:
use IO::Socket; use IO::Select; use Socket; use DBI; require ("cgiforms.pl"); require ("dbsearch.pl"); #-Get User Query: Convert to DBIServer format: @SQLStrings = ParseQueryString("\x22XP 1333\x22"); @DBIServerQueries = DBIServerEncapQuery(@SQLStrings); #-Socket stuff: $remote_host = "127.0.0.1"; $remote_port = "57660"; #-Create socket, connect to server: $DBIClient = IO::Socket::INET->new(PeerAddr=> $remote_host, PeerPort=> + $remote_port, Proto=> "tcp", Type=> SOCK_STREAM); #-Loop through queries, send to server, get database results: foreach $DBIServerQuery (@DBIServerQueries) { print $DBIClient "$DBIServerQuery"; while(defined(<$DBIClient>)) {print "$_";} } print $DBIClient "DBISERVER_CLIENT_DONE\r\n"; $DBIClient->close;
What it can't do: It can't receive any data, its like the socket is PermaBlocked or something. While if I use telnet to connect to my server I get all the data I could ever want when I submit my query text by hand. I"m completely confused. I've tried several variations on receiving data, and none work. The client connects to the DBIServer, then submits is query like a good little client, then it never gets the results of the query back. It just stays in a seemingly endless loop. Even if I take the while loop out of the code, and just use $Data = <$DBIClient>; it still stalls. Again the server works perfect with telnet, I just can't see why its not working here with a simple tcp perl client. Any help would be awesome. Thanks, ghostiLs

Replies are listed 'Best First'.
Re: IO::Socket not reading from server?
by dws (Chancellor) on Oct 02, 2003 at 22:31 UTC
    You don't show us what DBIServerEncapQuery() is doing. I'll bet it isn't adding a newline onto the end of the query strings. That would mean that
    print $DBIClient "$DBIServerQuery";
    is sending a string down the line, but if the receiving end is expecting a complete line (i.e., one with a trailing "\n"), it's going to block.

    Try

    print $DBIClient $DBIServerQuery, "\n";
Re: IO::Socket not reading from server?
by sgifford (Prior) on Oct 03, 2003 at 06:01 UTC
    Use tcpdump or strace to see whether the packets are not being sent by the host, are being sent by the host but not received by the client, or are being received by the client but not sent to your program. That should point you in the right direction for finding a solution.
Re: IO::Socket not reading from server?
by Anonymous Monk on Oct 02, 2003 at 22:42 UTC
    DBIServerEncpquery() does append a \r\n to the end of the query and yes the DBIServer is expecting this, and it gets the packet from the client, however the information that it tries to send back never gets to the client, but it does get to telnet. Thanks, -ghostiLs
Re: IO::Socket not reading from server?
by Melly (Chaplain) on Oct 03, 2003 at 11:19 UTC

    Are you sure that the other end is terminating data with a newline?

    If they're not, then you won't read anything from the socket.

    Try redefining $/ to a character that you know appears in the return string from the socket.

    Tom Melly, tom@tomandlu.co.uk