Go Back   openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc

 
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 27-May-2008, 16:57
carlos
Guest
 
Posts: n/a
Default

I was writing a perl program that acts as a server opening a port and receiving connections from clients written in C.
The C clients gets connected successfully but when they send data the server do nothing.
Well, due my problem I wrote a perl client and I sent the same data, but this time, the server shows the previous data from the C client and from the perl client. Only after sent a data from a perl program the server shows the data sent by the C program.

Any ideas?

muy malo mi ingles :S
  #2 (permalink)  
Old 27-May-2008, 17:21
ken_yap
Guest
 
Posts: n/a
Default

You probably want to set $| to non-zero. See $OUTPUT_AUTOFLUSH in man perlvar.
  #3 (permalink)  
Old 28-May-2008, 09:37
carlos
Guest
 
Posts: n/a
Default

Thank you for the fast reply.
I did what you said, but nothing good happens. :S
Finally I wrote a perl server, again, but using the oop interface provided by IO::Socket::INET package. This ways works fine but I can't use it because I need a low level control, then, trying more I found...

This way does not work, even if I autoflush() the STDOUT. The data just stay buffered until the client gets disconnected.

Code:
sub load_socket()
{
****socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname($config{protocol}));
****my $my_address = sockaddr_in($config{port},INADDR_ANY); #(INADDR_ANY)any address because i am the server
****setsockopt(SERVER,SOL_SOCKET, SO_REUSEADDR,1);
****bind(SERVER,$my_address) || die "Can't bind ($!)"; #register the socket ********
****listen(SERVER,10) || die "Can't listen ($!)";****
****while (my $client_addr = accept(CLIENT,SERVER))
****{
********my($port, $packed_ip) = sockaddr_in($client_addr);
********my $ip = inet_ntoa($packed_ip);
********print "Client Connected->'$ip'\n";
********#my $out = <CLIENT>;
********print <CLIENT>;
****}****
****close (SERVER);
}
But doing this works.
Rare:S
Code:
sub load_socket()
{
****socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname($config{protocol}));
****my $my_address = sockaddr_in($config{port},INADDR_ANY); #(INADDR_ANY)any address because i am the server
****setsockopt(SERVER,SOL_SOCKET, SO_REUSEADDR,1);
****bind(SERVER,$my_address) || die "Can't bind ($!)"; #register the socket ********
****listen(SERVER,10) || die "Can't listen ($!)";****
****while (my $client_addr = accept(CLIENT,SERVER))
****{
********my($port, $packed_ip) = sockaddr_in($client_addr);
********my $ip = inet_ntoa($packed_ip);
********print "Client Connected->'$ip'\n";
********my $out = <CLIENT>; #set to a var first
********print $out;
****}****
****close (SERVER);
}
  #4 (permalink)  
Old 28-May-2008, 17:43
ken_yap
Guest
 
Posts: n/a
Default

You should also check the value of $/ aka $INPUT_RECORD_SEPARATOR for the CLIENT FD. Also it's the CLIENT FD you have to autoflush, not stdout.
  #5 (permalink)  
Old 29-May-2008, 08:54
carlos
Guest
 
Posts: n/a
Default

yes, I understand, I already autoflush the client, before calling the sub, however, the output stills buffered.
Another rare thing: only if I concat a /r/n or /n to the data (to send) I receive it.

The program is full of problems. My client (C++) runs over an AS400 and I need to convert the EDBDIC string to ASCII string. :S
  #6 (permalink)  
Old 29-May-2008, 08:59
ken_yap
Guest
 
Posts: n/a
Default

You should use wireshark to look at the packets arriving to see whether the client is sending everything. Maybe there is something data still unsent by the client and it's not the server's fault, especially as you say appending an end-of-line fixes the problem. You did not say before that you were not sending from a different OS. You have to look at the conventions of that OS. If you are programming in stdio did you flush the stream after writing to the network?
  #7 (permalink)  
Old 29-May-2008, 23:08
carlos
Guest
 
Posts: n/a
Default

Quote:
You did not say before that you were not sending from a different OS.
[/b]
yes, sorry about that. The server runs over a Linux OS but the client over an AS400 (I can't remember the OS name right now) but, at least, the code that I write and compile in my opensuse works fine in the AS.

Quote:
If you are programming in stdio did you flush the stream after writing to the network?
[/b]
I use iostream but I did not flush () explicitly after sending data.
When the "buffer thing" happens the program never returns me the control after calling the send() function of the socket API.

Tomorrow (because now is midnight here in Paraguay ) I'll sniffer the communication between the client and the server.

Thank you!
  #8 (permalink)  
Old 30-May-2008, 04:10
FuryWS
Guest
 
Posts: n/a
Default


I don't know about Perl, but when reading from the sockets in C, you have to read multiple times, until there is nothing left in the incoming buffer. I don't know if in Perl <CLIENT> buffers the input first, or returns what is immediately in it.

Also how big is this data? If the packets are very, very small and Nagle algorithm is engaged on the client side (TCP_DELAY), you won't see anything on the server until it accumulates a bit.

  #9 (permalink)  
Old 30-May-2008, 04:19
FuryWS
Guest
 
Posts: n/a
Default


Just realized this:

Quote:
I use iostream but I did not flush () explicitly after sending data.
When the "buffer thing" happens the program never returns me the control after calling the send() function of the socket API.
[/b]
How exactly do you use iostream with sockets? And send() should not block unless the data you're sending is too big for the socket buffer. You can try, just to see the effect, put the client socket in O_NONBLOCK mode and read the return value from the send(), which MUST return immediately regardless of error.
 

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2