?
Solved

server/client pair using a unix socket

Posted on 2005-04-05
1
Medium Priority
?
272 Views
Last Modified: 2006-11-18
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
Comment
Question by:targetddc
1 Comment
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 13713797
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question