Server
#! usr/local/bin/perl -w
2
3 use Socket;
4 use FileHandle;
5
6 #Create new socket filehandle SERVER which is
7 #UNIX-domain and stream-based.
8 socket (SERVER, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
9 #
10 autoflush SERVER 1;
11
12 #bind SERVER socket filehandle to filename stored in $filename
13 unlink "/tmp/mysock";
14 bind(SERVER, sockaddr_un("/tmp/mysock")) or die "bind: $!";
15
16 #Listen for connections. Integer in listen() specifies max number
17 # of waiting connections that can be supported.
18 listen (SERVER, 1) or die "listen: $!";
19
20 for (;;) {
21 accept(DATA, SERVER);
22 print SERVER "Because you are a rude client!";
23 # perform communication between client and server
24 # reading and writing information to the DATA filehandle.
25 print "Client says: ".<DATA>."\n";
26 close DATA; # finished with this client
27 # go back to accept the next communication
28 }
Client
1 #! usr/local/bin/perl -w
2
3 use Socket;
4 use FileHandle;
5
6 #Create socket called CLIENT
7 socket (CLIENT, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
8 #Connect to server
9 connect(CLIENT, sockaddr_un("/tmp/mysock")) or die "connect: $!";
10 autoflush CLIENT 1;
11
12 print CLIENT "Why don't you ever pick up the phone?\n";
13 print "Server says: ".<SERVER>."\n";
What happens now is, as far as I can see, the connections are all made, the client sends the server a message but then the server cannot respond. I get this error:
Name "main::SERVER" used only once: possible typo at client.pl line 13.
readline() on unopened filehandle SERVER at client.pl line 13.
Can anyone point out what it is I'm doing wrong here and give some suggestions?


Sign In
Create Account


Back to top









