RE: :Socket need help
Morgan, Just after the "print $buf" line in the server code, add: $cmd=`$buf`; print $new_sock "$cmd"; You actually don't need to use a client a this time. Certainly you can, but, to make this simpler, just telnet to the server in that port (telnet 192.168.1.120 9500) and issue commands. Their output will return to you. Pretty unsafe... I guess that from this point you can improve your scripts. A socket connection is just an interface between two scripts/applications. You'll have to build your protocols above this "layer" so that one side is making resquets and and other is providing the answers. That's a client/server application. Regards, Bruno Figueira -Original Message- From: Morgan Norell [mailto:[EMAIL PROTECTED]] Sent: Quarta-feira, 3 de Abril de 2002 10:22 To: [EMAIL PROTECTED] Subject: IO::Socket need help Hi I'm about to lern the basics of perls IO::Socket. I have managet to set up a small server and connected to it with a client (I had some help of a perl book). But I want to be able to send a command to it and get a response. For exapmle request the servers hostname. Can anyone please help me with a few hints. I use RH 7.2 and perl 5.6.1 thanks Morgan --- SERVER --- #!/usr/bin/perl -w use IO::Socket; $server_host = "localhost"; $server_port = 9500; $sock = new IO::Socket::INET (LocalHost => $server_host, LocalPort => $server_port, Proto => 'tcp', Reuse => 1, Listen=> 10 ) or die "Couldn't connect to tcp server on port $server_port : $@\n"; while ($new_sock = $sock->accept()) { while (defined ($buf = <$new_sock>)) { print $buf; } } close ($sock); --- client --- #!/usr/bin/perl -w use IO::Sock; $remote_host = "192.168.1.120"; $remote_port = 9500; $sock = new IO::Socket::INET (PeerAddr => $remote_host, PeerPort => $remote_port, Proto=> 'tcp', ); die "Socket could not be created. Reason: $!\n" unless $sock; print "$sock\n"; close ($sock); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Better "tail -f"
Thank you drieux. The suggestion from your link handles it (I've made minor modifications)! You're right. Checking sizes is a good idea. Ciao, BrunoF -Original Message- From: drieux [mailto:[EMAIL PROTECTED]] Sent: Terça-feira, 2 de Abril de 2002 13:11 To: [EMAIL PROTECTED] Subject: Re: Better "tail -f" On Tuesday, April 2, 2002, at 04:30 , Bruno Figueira wrote: > However, if another program removes (all) lines from the file referenced > by > FH, perl looses the trailing (for a undetermined time). How do I improve > this code to understand the file was changed? I misread your problem - then ran into it... used the p2 code from yesterday in p2>filename and it made: http://www.wetware.com/drieux/CS/lang/Perl/Beginners/file_change_while_taili ng.txt act stoopid - till I put the spare subroutine into reset to begining ciao drieux -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Better "tail -f"
Well, I should not lock it. Actually, my code should be "robust" enough to workaround it and "tail -f" a file that is "reseted" sometimes. Cheers, Bruno -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Terça-feira, 2 de Abril de 2002 12:24 To: Figueira, Bruno [CMPS:003R:EXCH] Cc: [EMAIL PROTECTED] Subject: Re: Better "tail -f" How about putting a file lock on it? That way if someone tries to access it and write to it, it will not work. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Better "tail -f"
Hi there, I have got the example of trailing a file from "Perl Cookbook": --- for (;;) { while () { print $_ } sleep 1; seek(FH, 0, 1); } --- However, if another program removes (all) lines from the file referenced by FH, perl looses the trailing (for a undetermined time). How do I improve this code to understand the file was changed? Cheers, Bruno -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Socket closed connection
Guys, How can I check if the remote peer closed my TCP connection? The following code is OK (taken from the book) because it expects something to come from the peer: use IO::Socket; $sock = new IO::Socket::INET (LocalHost => 'goldengate', LocalPort => 1200, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $sock; while ($new_sock = $sock->accept()) { while (defined ($buf = <$new_sock>)) { # Will return undef if peer closes connection! print $buf; } } However, how can I check if peer closed when I am not expecting anything from it? There is not <$new_sock> in my code. I don't want to block on peer's input. BrunoF -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Trailing nasty files and sockets closing
People, I am working in a script I would call file2port. The idea is to trail (like tail -f) a file and send it to a TCP port. For every TCP connection incoming to my server, I would trail a file and print its new lines to the client peer. However, I am having two problems I would like to present: 1. I can't tell exactly when the client disconnected. I expected to check for any variable that could tell me the status of that connection but I could not find it. So I am using a "Broken Pipe" signal to discover this, but it is not quite elegant. Question: how can I tell the client has disconnected? 2. I am trailing a file (e.g. myTime). The trailing would be ok if the file is not periodically overwritten by another application. I mean, from time to time, another program cleans the file (the size goes to zero bytes) and starts writing on it again. It seems that perl looses the tracking of it and it takes to much time (and to many lines lost) to re-synch with the file again. Question: How do I improve the trailing so that I wont miss lines when the file is rewritten? I have copied the code in this e-mail, since it's quite small. Regards, Bruno Figueira --- CodeFollows --- #!/opt/MagellanContrib/bin/perl -w use Socket; use IO::Socket; $SIG{CHLD} = 'IGNORE'; $sock = new IO::Socket::INET (LocalHost => 'localhost', LocalPort => 1200, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $sock; while ($new_sock = $sock->accept()) { $new_sock->autoflush(1); $pid = fork(); die "Cannot fork: $!" unless defined($pid); if ($pid == 0) { $thisHost = $new_sock->peerhost(); $thisPort = $new_sock->peerport(); print "New connection from ", $thisHost, ", port ", $thisPort, "\n\n"; $SIG{PIPE} = 'IGNORE'; open(HORAS, "< myTime") or die "Couldn't open myTime for reading: $!\n"; while () { } for (;;) { seek(HORAS, 0, 1); $status = print $new_sock ; if ( !$status ) { last; } sleep 1; } print "Connection Finished! ", "Host: ", $thisHost, ", port ", $thisPort, "\n\n"; close (HORAS); exit(0); # Child process exits when it is done. } # else this is the parent process, which goes back to accept() } close ($sock); An easy way to create the myTime file - #!/bin/csh while ( 1 == 1 ) echo `date` >> myTime sleep 5 end reset it mannualy! - -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]