Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

IO over socket

by neptuna (Acolyte)
on Jun 22, 2004 at 12:28 UTC ( #368693=perlquestion: print w/replies, xml ) Need Help??

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

Hi
I have 2 simple scripts to send short files from a win2k box to HP unix box ( for learning purposes only). They work fine, but I am looking for ideas on why the client (win2k) needs a sleep statement after each line write. If I leave it out, I get a "Connection reset by peer" on the receiving end(unix) Thanks and the relative code is below:
# Server (Receiver on Unix end) if ($pid = fork) { # parent close(C); waitpid( -1, &WNOHANG ); next; # ready for another client } else { # child die "can't fork: $!" unless defined $pid; close(S); print C "Connected to $ip:$port "; # get file name sysread(C, $fname, 128) or die "couldn't read: $!\n& +quot;; $fname =~ s/\n//; print "recieving file: $fname\n"; while ( sysread(C, $buf, 128) ) { $msg .= $buf; } open (F, ">$fname") or die "couldn't ope +n it: $!"; # open a new file with the file name and write to it print F $msg; close F; print "Recieved file\n"; exit; } ------ # client (win2k sender) socket(SOCK,AF_INET,SOCK_STREAM,$proto) || die "$0:Cannot open so +cket\n"; connect(SOCK, $remote) or die "can't connect: $!\n"; # send filename to server print "sending file $fname\n"; $bytes = syswrite(SOCK, $fname, length($fname)); sleep 1; open(F, "<$fname") or die "coundn't open: $!"; @buf = <F>; foreach $line (@buf) { # why do we need to sleep ? syswrite(SOCK, $line, length($line)) or die "can't write: $ +!"; sleep 1; } close F;
 editted by jeffa - s/pre/code/g 

Replies are listed 'Best First'.
Re: IO over socket
by Abigail-II (Bishop) on Jun 22, 2004 at 13:02 UTC
    Your problem is that you send the filename and the content of the file without any marker in between. Without the sleep both the name and the content might be send in a single packet, especially if the file is small. Assume for instance you have a 50 byte file, and a 20 byte filename. You send 70 bytes, and those 70 bytes will be read at the receiving end at your first sysread. By the time you get to the second sysread, in the loop, the sending end has already finished sending the data and closed the socket.

    Your problem is that you are mixing protocols. It seems you like to do a line based protocol, first sending the filename, then the content, line-by-line, but you are using a bytestream mechanism for it. If you want to do it line-by-line, just use print (on the sending end) and <$socket> (on the receiving end).

    Abigail

      Thanks. Funny that you mentioned the "Marker" in between the file name and the data. 

      I was also looking for the best way to do that. I prefer using a byte stream

      for the actual transfer but again not sure how to separate the file name from the file data. Thanjs again for your help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://368693]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-09-18 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?