B
bob Smith
I am trying to setup a client that sends info to a server and then the
server responsds back to the client with a response from the info it sent
across the open socket. I am new and I know I missing something. I have
looked at several examples and have taken from them all but can't get all to
work together.
1. I've gotten the client to send to the server and the server recieves
2. I've gotten the client to connect and the server to send to it.
I can't get those 2 things to happen within the same program. I know that I
need to probably say from the client "Hey I am done sending and I am ready
to recieve" but I haven't figured out how to do it. Here is my code below.
Any help would be appreciated even if you can point me to a simple example.
Client code:
#!/usr/bin/perl -w
use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => 'localhost',
PeerPort => 5150,
Proto => 'tcp'
);
die "Socket could not be created. Reason: $!\n" unless $sock;
foreach (1 .. 10) {
print "Hello $_: \n";
print $sock "Msg $_: How are you?\n";
#$sock->flush();
}
print "quit";
print $sock "quit";
$sock->flush();
my $line;
while (defined($line = <$sock>)) {
print $line;
}
close ($sock);
Server code:
#!/usr/bin/perl -w
use Socket;
my $host = 'localhost';
my $port = 5150;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER)) {
while (defined($buf = <CLIENT>)) {
print "$buf\n";
}
my ($client_port, $client_ip) = sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host = gethostbyaddr($client_ip, AF_INET);
print "got a connection from: $client_host","[$client_ipnum]\n";
print CLIENT "Smile from the server\n";
close CLIENT;
}
server responsds back to the client with a response from the info it sent
across the open socket. I am new and I know I missing something. I have
looked at several examples and have taken from them all but can't get all to
work together.
1. I've gotten the client to send to the server and the server recieves
2. I've gotten the client to connect and the server to send to it.
I can't get those 2 things to happen within the same program. I know that I
need to probably say from the client "Hey I am done sending and I am ready
to recieve" but I haven't figured out how to do it. Here is my code below.
Any help would be appreciated even if you can point me to a simple example.
Client code:
#!/usr/bin/perl -w
use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => 'localhost',
PeerPort => 5150,
Proto => 'tcp'
);
die "Socket could not be created. Reason: $!\n" unless $sock;
foreach (1 .. 10) {
print "Hello $_: \n";
print $sock "Msg $_: How are you?\n";
#$sock->flush();
}
print "quit";
print $sock "quit";
$sock->flush();
my $line;
while (defined($line = <$sock>)) {
print $line;
}
close ($sock);
Server code:
#!/usr/bin/perl -w
use Socket;
my $host = 'localhost';
my $port = 5150;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER)) {
while (defined($buf = <CLIENT>)) {
print "$buf\n";
}
my ($client_port, $client_ip) = sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host = gethostbyaddr($client_ip, AF_INET);
print "got a connection from: $client_host","[$client_ipnum]\n";
print CLIENT "Smile from the server\n";
close CLIENT;
}