Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: C Client / Perl Server incompatibility

by Magius_AR (Sexton)
on Dec 08, 2000 at 06:50 UTC ( [id://45681]=note: print w/replies, xml ) Need Help??


in reply to C Client / Perl Server incompatibility

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); } } }

Replies are listed 'Best First'.
Re: Re: C Client / Perl Server incompatibility
by myocom (Deacon) on Dec 08, 2000 at 07:12 UTC

    You might try running the server and telnetting to the appropriate port to see if you get the output you're expecting. I just did so (under Win32) and was able to get fortunes... Looks like a closer look at what the C code is sending/expecting might be in order.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://45681]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-30 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found