Hi,
I want to make a server-client application that as 5 steps
1) the client sends a request to the server
2) the server responds
3) the client does something with that response and sends a last transmission to the server
4) the server reads this last transmission and writes it to a file
5) the client disconnects
I'm new to this
, here's a piece of the code, i hope it's not too messie
#SERVER
#declarations, etc..
while (!$quit) {
next unless my $connection = $socket->accept;
#the server handles multiple connections
defined (my $child = fork()) or die "Can't fork: $!";
if ($child == 0) {
$socket->close;
do_something($connection);
}
$connection->close;
}
#here i wanted the server to read the first message from the #client, respond, and finally read the last message from the #client and print it..
sub do_something {
my $s = shift;
my $msg_in = <$s>; #reads the first message
$s->print("Hi!\n");#send the first response
#what about the second response from the client?
exit(0);
}
#CLIENT - forks, the father reads, the child writes, don't know if it is a good idea...
.
.
#declarations
.
if ($child) {
send_msg(); #sends the first request
$socket->shutdown(1);
sleep;
} else {
get_msg(); #reads the response
}
sub send_msg {
$socket->print("Hello!\n");
}
sub get_msg {
my $a;
print $a while sysread($socket, $a, 1024);
#this doesn't work
}
Should the server also fork to read and write?
how does the server know that there is an answer from the client and vice-versa?
thanks,
I want to make a server-client application that as 5 steps
1) the client sends a request to the server
2) the server responds
3) the client does something with that response and sends a last transmission to the server
4) the server reads this last transmission and writes it to a file
5) the client disconnects
I'm new to this
#SERVER
#declarations, etc..
while (!$quit) {
next unless my $connection = $socket->accept;
#the server handles multiple connections
defined (my $child = fork()) or die "Can't fork: $!";
if ($child == 0) {
$socket->close;
do_something($connection);
}
$connection->close;
}
#here i wanted the server to read the first message from the #client, respond, and finally read the last message from the #client and print it..
sub do_something {
my $s = shift;
my $msg_in = <$s>; #reads the first message
$s->print("Hi!\n");#send the first response
#what about the second response from the client?
exit(0);
}
#CLIENT - forks, the father reads, the child writes, don't know if it is a good idea...
.
.
#declarations
.
if ($child) {
send_msg(); #sends the first request
$socket->shutdown(1);
sleep;
} else {
get_msg(); #reads the response
}
sub send_msg {
$socket->print("Hello!\n");
}
sub get_msg {
my $a;
print $a while sysread($socket, $a, 1024);
#this doesn't work
}
Should the server also fork to read and write?
how does the server know that there is an answer from the client and vice-versa?
thanks,