LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2005, 11:03 PM   #1
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
PERL sockets


i have this code on the server side

Code:
#!/bin/perl -w
use strict;
use IO::Socket;

		print "Hello welcome to the Fs server\n";
		
		my $sock = new IO::Socket::INET (
		   LocalHost => 'charlie',
		   LocalPort => '7073',
		   Proto => 'tcp',
		   Listen => 1,
		   Reuse => 1,
						);
		die "Could not create socket: $!\n" unless $sock;		
		print " Great just created a socket\n";
		
		my $new_sock = $sock->accept();
				while(defined(<$new_sock>)) {
						print $_;
						}
          
	        close($sock);

and this code on the client side

Code:
#!/bin/perl -w
use strict;
use IO::Socket;


		print " Hello welcome to the fsclient\n";
		
		my $sock = new IO::Socket::INET(
                   PeerAddr => 'charlie',
                   PeerPort => '7073',
                   Proto => 'tcp',
                                  );
		die "Could not create socket: $!\n" unless $sock;
		print $sock "Hello there!\n";
		close($sock);
when i run this server followed by the client this is the error i get

[jnsahibz@charlie assignment2]$ fsserver.pl
Hello welcome to the Fs server
Great just created a socket
Use of uninitialized value at ./fsserver.pl line 19, <GEN1> chunk 1.
 
Old 05-08-2005, 04:05 AM   #2
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
just remove defined construction

Code:
#!/bin/perl -w
use strict;
use IO::Socket;

		print "Hello welcome to the Fs server\n";
		
		my $sock = new IO::Socket::INET (
		   LocalHost => 'charlie',
		   LocalPort => '7073',
		   Proto => 'tcp',
		   Listen => 1,
		   Reuse => 1,
						);
		die "Could not create socket: $!\n" unless $sock;		
		print " Great just created a socket\n";
		
		my $new_sock = $sock->accept();
				while(<$new_sock>) {
						print $_;
						}
          
	        close($sock);
 
Old 05-08-2005, 04:12 AM   #3
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
thanks a lot it worked.

i was scratching my head on it for the whole day.

i ll be posting more questions on this thread. cause so plz visit this thread when ever i bump it back to the top.

thanks.
 
Old 05-08-2005, 04:45 AM   #4
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
and by the way can u explain why this code is working in the first case.

i havent binded it to a address.

i havent used the listen functions either?

every single article i read instructed me to use bind and listen before accepting connections.

so why is this connection working?

as u can see in the code u pasted their is no bind and listen

Code:
#!/bin/perl -w
use strict;
use IO::Socket;

		print "Hello welcome to the Fs server\n";
		
		my $sock = new IO::Socket::INET (
		   LocalHost => 'charlie',
		   LocalPort => '7073',
		   Proto => 'tcp',
		   Listen => 1,
		   Reuse => 1,
						);
		die "Could not create socket: $!\n" unless $sock;		
		print " Great just created a socket\n";
		
		my $new_sock = $sock->accept();
				while(<$new_sock> ) {
						print $_;
						}
          
	        close($sock);
 
Old 05-08-2005, 05:01 AM   #5
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
There are two basic ways how you can use sockets.

1) (use Socket;)
Bind and listen are used in low level access.

2) (use IO::Socket;)
Higher level programming with object oriented approach. All bind and listen operation are hidden in the object, so you haven't to call them manually.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Non blocking sockets in Perl ivanatora Programming 3 08-19-2005 02:14 AM
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
Yum error: .conflict between perl and perl-NDBM_File zepplin611 Red Hat 3 10-20-2004 10:22 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration