• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

server/client pair using a unix socket

I need assistance with the following task and have included some code.  the server receives only 1 client a writes a "." once a second to the socket for the client to read.  The server should then close the connection, remove the socket and exit when given the TERM signal.

#!/usr/bin/perl -w
use Socket;
$socket_name = sockaddr_un("/tmp/mysock");
socket(SERVER, PF_UNIX, SOCK_STREAM, 0);
unlink "/tmp/mysock";
bind (SERVER, sockaddr_un("/tmp/mysock"))
or die "Can't create server: $!";

socket(CLIENT, PF_UNIX, SOCK_STREAM, 0);
unlink "/tmp/mysock";
connect (CLIENT, sockaddr_un("/tmp/mysock"))
or die "Can't connect to /tmp/mysock: $!";
0
targetddc
Asked:
targetddc
1 Solution
 
ozoCommented:
Server:
#!/usr/bin/perl -w
use strict;
use Socket;

socket(SERVER, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!";
unlink "/tmp/mysock";
bind (SERVER, sockaddr_un("/tmp/mysock"))
    or die "Can't create server: $!";

listen(SERVER,SOMAXCONN) || die "listen: $!";

accept(CLIENT,SERVER);
$SIG{TERM}=$SIG{PIPE}=\&cleanup;
    select((select(CLIENT),$|=1)[0]);

    while( 1 ){  print CLIENT "."; sleep 1; }

sub cleanup{
    close CLIENT;
    close SERVER;
    unlink "/tmp/mysock";
    exit;
}



Client:
#!/usr/bin/perl -w
use Socket;
socket(SOCK, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!";
connect (SOCK, sockaddr_un("/tmp/mysock"))
    or die "Can't connect to /tmp/mysock: $!";
$/=\1;
while( <SOCK> ){
    print "$_\n";
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now