Jump to content

B-direction Unix Domain Sockets?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Hi, new to socket programming have been working through some exercises. I have little Perl knowledge and am piecing stuff together from tutorials etc I find online. Basically, I want to have a server and a client. The server opens and waits for connections, one client connects and sends a message to the server, the server sends a response to the client and then terminates the connection and waits for another. This is the code I've pieced together so far:

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?

#2
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
I remember trying to figure this out myself. This site gave me all the answers I needed and i think it shows exactly what you are trying to do: Perl, Sockets and TCP/IP Networking.
twas brillig

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
Just a hunch...try using a scalar instead of an autovivified handle:
# instead of
open(HANDLE,"asdfghjkl") or die "Error: $!";

# try
my $HANDLE = undef;
open($HANDLE, "asdfghjkl") or die "Error: $!\n";

sudo rm -rf /

#4
bythos

bythos

    Newbie

  • Members
  • PipPip
  • 14 posts
when coding a server you need to fork or thread off each connection from a client and handle them so main server loop can be free to listen.

In your code try replacing.
<SERVER> with <CLIENT>