When i try to implement a server program giving UDP as protocol , it works fine , but if the same c

T

Tompyna

Hi All,

I am a newbie to Socket Programming . I had written a socket
program in perl , implementing client and server.

Now coming to the problem i am stuck:

1.When i implement the program with "UDP" as protocol option, the code
works
fine,but When i try to implement the same program , giving TCP as
the
protocol option, it gives me an error:
"Can't call method "recv" on an undefined value at servermodulenew.pl
line 18"

I believe the method "recv" should be called irrespective of the
protocol option i give.

Hope someone could possibly give me an answer to this problem.

(Below is the code for server implementation)
NOTE: The program is executing on solaris and run as a background
process.

The server Program i implemented is:

/************************************************/
# Server Program

use IO::Socket;

my $port = 1024;

#use ngd_simulator::thomasp::perl::Socket;

my $receive_status;

print ">> Server Program <<\n";

# Create a new socket
$MySocket=new IO::Socket::INET->new(LocalPort=>($port),Proto=>(getprotobyname('udp')));

while(1)
{
#Receiving a message from the client
$receive_status = $MySocket->recv($text,128);

if($text ne '')
{
print "\nReceived message ===>", $text,"\n";

}
# If client message is empty exit
else
{
print "Cilent has exited!";
exit 1;

}
}
#Close the Socket after sending the message
close($MySocket);

/******************************************************/



Thanx.... in advance,
Thomas Poly.
 
G

gnari

1.When i implement the program with "UDP" as protocol option, the code
works
fine,but When i try to implement the same program , giving TCP as
the
protocol option, it gives me an error:
"Can't call method "recv" on an undefined value at servermodulenew.pl
line 18"

I believe the method "recv" should be called irrespective of the
protocol option i give.

UDP is connectionless, TCP is not.

with TCP you have to
make socket
listen
accept connections when they arrive
and then read data

gnari
 
T

thumb_42

In comp.lang.perl.misc Tompyna said:
1.When i implement the program with "UDP" as protocol option, the code
works
fine,but When i try to implement the same program , giving TCP as
the
protocol option, it gives me an error:
"Can't call method "recv" on an undefined value at servermodulenew.pl
line 18"

Thats because TCP doesn't have a recv. :)

UDP is connection-less, you're responsible for any network errors and
dropped packets, it has a lot in common with the US postal service, in that
it will loose your mail and not care.

TCP is more like a phone connection, so the methods are different.

(TCP and UNIX sockets are a lot like working with file handles)

Jamie
 
A

Anno Siegel

[...]
1.When i implement the program with "UDP" as protocol option, the code
works
fine,but When i try to implement the same program , giving TCP as
the
protocol option, it gives me an error:
"Can't call method "recv" on an undefined value at servermodulenew.pl
line 18"
[...]

The server Program i implemented is:

/************************************************/
# Server Program

No strict, no warnings. Switch them on.
use IO::Socket;

my $port = 1024;

#use ngd_simulator::thomasp::perl::Socket;

my $receive_status;

print ">> Server Program <<\n";

# Create a new socket
$MySocket=new

You have declared other variables lexicals. Why is $MySocket a package
variable?
IO::Socket::INET->new(LocalPort=>($port),Proto=>(getprotobyname('udp')));

This is the statement that returns an undefined value for $MySocket. Why
don't you ask why?

my $MySocket = new IO::Socket::INET->new(
LocalPort=>($port),Proto=>(getprotobyname('udp'))) or
die "Socket error: $!";

I notice that you call the new() method twice in that statement, once as
as class method and once as an object method. I don't think you want to
do that.

[rest snipped]

Anno
 
D

Dr. Ugo Gagliardelli

Tompyna said:
Hi All,

I am a newbie to Socket Programming . I had written a socket
program in perl , implementing client and server.

Now coming to the problem i am stuck:

1.When i implement the program with "UDP" as protocol option, the code
works
fine,but When i try to implement the same program , giving TCP as
the
protocol option, it gives me an error:
"Can't call method "recv" on an undefined value at servermodulenew.pl
line 18" [cut]
# Create a new socket
$MySocket=new IO::Socket::INET->new(LocalPort=>($port),Proto=>(getprotobyname('udp')));

while(1)
{
#Receiving a message from the client
$receive_status = $MySocket->recv($text,128);

if($text ne '')

As far as I know to use a connection oriented socket in general (with recv as
well), as tcp is, you must connect first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top