ok, for the sake of frustration, I'll just post the whole damn thing.
The client can _NOT_ be changed.
The Perl server must somehow be altered so that the sockets will work.
C Client
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n,len,len1;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)
+) < 0)
error("ERROR connecting");
printf("Welcome to Fortune Teller Client\n");
printf("Please enter a command GET or BYE: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
while ((buffer[0]=='G') &&(buffer[1]=='E') && (buffer[2]=='T'))
{
n = write(sockfd,"GET",strlen("GET"));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
len = read(sockfd,buffer,2);exit(0);
if (len < 0)
error("ERROR reading from socket");
printf("Welcome to Fortune Teller Client\n");
printf("Please enter a command GET or BYE: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
while ((buffer[0]=='G') &&(buffer[1]=='E') && (buffer[2]=='T'))
{
n = write(sockfd,"GET",strlen("GET"));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
len = read(sockfd,buffer,2);exit(0);
if (len < 0)
error("ERROR reading from socket");
sscanf(buffer,"%d",&len1);
bzero(buffer,256);
n = read(sockfd,buffer,len1);
printf("Your Fortune from the Fortune Server is \n");
printf("%s\n",buffer);
bzero(buffer,256);
printf("Next Command Please: (GET or BYE)\n");
fgets(buffer,255,stdin);
}
write(sockfd,"BYE",3);
return 0;
}
Perl Server
#!/usr/bin/perl
use IO::Socket;
@ARGV == 1 || die "usage: $0 port\n";
if ($ARGV[0] < 1 || $ARGV[0] > 65535) { die "$ARGV[0] is not a valid p
+ort\n"; }
my ($port) = @ARGV;
open(FILE,"fortunes");
while ($fortune = <FILE>)
{
chomp($fortune);
push @fortunes,$fortune;
$db_size++;
}
close(FILE);
if (!$db_size) { die "fortunes file is empty\n"; }
my $server = IO::Socket::INET->new(
Listen => SOMAXCONN,
LocalPort => $port,
Reuse => 1,
Proto => 'tcp' )
|| die "can't open server socket connection: $!";
while (defined($remote = $server->accept)) { # accept multiple clients
$remote->autoflush(1);
$hostinfo = gethostbyaddr($remote->peeraddr, AF_INET);
print STDERR "[Received connection from ", $hostinfo || $remote->p
+eerhost," on $port]\n";
while (<$remote>) # listen on command channel
{
if (/GET/i)
{
srand; # seed the random numb
+er function
$num = int (rand $db_size); # get a random fortune
$length = length($fortunes[$num]);
if ($length < 10 || $length > 99) { $fortune = "Your f
+ortune was not of valid length."; }
print $remote "$length\n";
print $remote "$fortunes[$num]\n";
}
elsif (/BYE/i)
{
close($remote);
}
}
}