problem in writing data to a file using socket (perl)

  • akaash
  • Beginner
  • Beginner
  • akaash
  • Posts: 62
  • Loc: Gujurat,India

Post 3+ Months Ago

We are having two servers(linux) : server1 and server2
I want to transfer contents of a file located on server1 to server2.

i m using IO::Socket module for this.

On server1 I have a cgi program sam.cgi having -rwsr-xr-x and owner is root and group is also root.

In this sam.cgi script we are creating the client part of the socket which reads data from a particular file and then transfers that data to server part of the socket which is running and listening on server2.

Data is transfered successfully and it is also printed on console (STDOUT) of server2. But the problem is that we are not able to write that collected data to some file on server2.

I don't know what's the problem?

The script on server2 which is listening the requests is having following rights
-rw-r----- temp1 temp1

where temp1 is the username.

i want to write the transfered data in file on server2: /home/temp1/backup.txt

what i should do?????
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Managedlinks
  • Proficient
  • Proficient
  • Managedlinks
  • Posts: 294

Post 3+ Months Ago

are you opening the file correctly ? must be opened for output using a ">" symbol

you need to catch the error returned when you try to open the file for writing

in perl this is stored in the $! variable e.g.

if (open (OUT,"> $tempname"))
{
write data ...
close OUT;
}
else
{
print "Oops an error occured : $!";
}
  • akaash
  • Beginner
  • Beginner
  • akaash
  • Posts: 62
  • Loc: Gujurat,India

Post 3+ Months Ago

The file is opened but no error is returned when i try to write the file.

When I see the directory the file is created but it does not contain anything.
  • Managedlinks
  • Proficient
  • Proficient
  • Managedlinks
  • Posts: 294

Post 3+ Months Ago

whats the code ?
  • akaash
  • Beginner
  • Beginner
  • akaash
  • Posts: 62
  • Loc: Gujurat,India

Post 3+ Months Ago

Code: [ Select ]
code for client that send the data:

#!/usr/bin/perl

use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => '172.16.40.11',
               PeerPort => 50010,
               Proto  => 'tcp'
               );
die "Socket could not be created. Reason: $!\n" unless $sock;
print "enter the filename:";
my $name = <STDIN>;
open(IN,"$name") || die "can't open i/p file";
while(<IN>){
  print $sock $_;

    print $_;
}
close (IN);
close ($sock);



and the code for the server that receive the data:


#!/usr/bin/perl

use IO::Socket;

$sock = new IO::Socket::INET (LocalHost => '172.16.40.11',
                             LocalPort => 50010,
               Proto   => 'tcp',
               Listen  => 10,
               );
die "Socket could not be created. Reason: $!" unless $sock;
open(SAVAN,">savanback");


while ($new_sock = $sock->accept()) {
  while (defined ($buf = <$new_sock>)) {

        print SAVAN $buf || die("$!");
        print $buf || die();
        
        
  }
}
close ($sock);
close(SAVAN);
  1. code for client that send the data:
  2. #!/usr/bin/perl
  3. use IO::Socket;
  4. $sock = new IO::Socket::INET (PeerAddr => '172.16.40.11',
  5.                PeerPort => 50010,
  6.                Proto  => 'tcp'
  7.                );
  8. die "Socket could not be created. Reason: $!\n" unless $sock;
  9. print "enter the filename:";
  10. my $name = <STDIN>;
  11. open(IN,"$name") || die "can't open i/p file";
  12. while(<IN>){
  13.   print $sock $_;
  14.     print $_;
  15. }
  16. close (IN);
  17. close ($sock);
  18. and the code for the server that receive the data:
  19. #!/usr/bin/perl
  20. use IO::Socket;
  21. $sock = new IO::Socket::INET (LocalHost => '172.16.40.11',
  22.                              LocalPort => 50010,
  23.                Proto   => 'tcp',
  24.                Listen  => 10,
  25.                );
  26. die "Socket could not be created. Reason: $!" unless $sock;
  27. open(SAVAN,">savanback");
  28. while ($new_sock = $sock->accept()) {
  29.   while (defined ($buf = <$new_sock>)) {
  30.         print SAVAN $buf || die("$!");
  31.         print $buf || die();
  32.         
  33.         
  34.   }
  35. }
  36. close ($sock);
  37. close(SAVAN);

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 67 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.