
manishm
New User
Aug 11, 2010, 5:16 AM
Post #1 of 2
(14915 views)
|
Client Server Connection through sockets issue.
|
Can't Post
|
|
I am new into sockets programming through perl: I have the following server.pl
#!C:\Perl\bin\perl use strict; use Socket; print "Content-type:text/html\n\n"; print "<html>\n"; print "<ul>"; # use port 7890 as default my $port = shift || 7890; my $proto = getprotobyname('tcp'); my $paddr = sockaddr_in($port, INADDR_ANY); # create a socket, make it reusable socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "Can't open socket $!\n"; setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "Can't set socket option to SO_REUSEADDR $!\n"; # bind to a port, then listen bind( SOCKET, $paddr) or die "Can't bind to port $port! \n"; listen(SOCKET, 5) or die "listen: $!"; print "<li>SERVER started on port $port\n"; # accepting a connection my $client_addr; while ($client_addr = accept(NET_SOCKET, SOCKET)) { # send them a message, close connection print NEW_SOCKET "Smile from the server"; close NEW_SOCKET; } print "</ul>"; print "</html>\n"; and the following client.pl
#!C:\Perl\bin\perl use strict; use Socket; print "Content-type:text/html\n\n"; print "<html>\n"; print "<ul>"; # initialize host and port my $host = shift || 'localhost'; my $port = shift || 7890; my $server = "localhost"; my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); # create the socket, connect to the port socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))) or die "Can't create a socket $!\n"; connect( SOCKET, $paddr) or die "Can't connect to port $port! \n"; my $line; while ($line = <SOCKET>) { print "$line\n"; } close SOCKET or die "close: $!"; print "</ul>"; print "</html>\n"; But I am unable to connect the client to the server. Do please let me know the areas I need to look into. Thanks & Regards, Manish.
|