UNIX Domain sockets - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

UNIX Domain sockets

Description:

UNIX Domain sockets The Linux Programming Interface (ch 57) UNIX Network Programming Vol 1, 3ed (ch 15) Using Sockets in UNIX domain Unnamed sockets ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 19
Provided by: BobC118
Category:
Tags: unix | domain | socket | sockets

less

Transcript and Presenter's Notes

Title: UNIX Domain sockets


1
UNIX Domain sockets
  • The Linux Programming Interface
  • (ch 57)
  • UNIX Network Programming Vol 1, 3ed (ch 15)

2
Using Sockets in UNIX domain
  • Unnamed sockets socketpair( )
  • Does not bind to transport layer ports
  • Creates a pair of sockets that are connected
  • Typically used to communicate between related
    processes
  • Communications similar to pipe
  • Traditional sockets in UNIX
  • Binds to transport layer ports
  • Allows independent processes to communicate
  • Creates a file type object (socket)
  • Any process that can access the socket can use
    it.
  • Does not use network (IP) layer

3
socketpair( )
  • int socketpair (int family, int type, int
    protocol, int sockfd2)
  • Family must be AF_LOCAL
  • Type may be SOCK_STREAM or SOCK_DGRAM
  • protocol must be 0
  • On success, the pair of socket descriptors
    (sockfd) are connected together and available for
    communications
  • include ltsys/socketsgt
  • Return zero on success, -1 on fail
  • In Linux, sockets are full duplex (can both read
    and write), however, POSIX standard does not
    require full duplex.

4
sockpair.cpp
define LB_SIZE 128 int main ( ) time_t
now char child_bufLB_SIZE, parent_bufLB_SIZE
pid_t pid int sockfd2, outSize,
inSize bool keepLooping time
(now) keepLooping true cout ltlt "Socket
Pair test at " ltlt ctime(now) socketpair
(AF_LOCAL, SOCK_STREAM, 0, sockfd) pid fork()
if (pid 0) // child process while
(keepLooping) inSize recv(sockfd1,
child_buf, LB_SIZE, 0) child_bufinSize
'\0' if (strncmp(child_buf, "bye", 3)
0) keepLooping false cout ltlt "Child
received " ltlt child_buf ltlt endl cout ltlt
"Closing child process" ltlt endl return 0
//end of child process section
5
sockpair.cpp
else if (pid gt 0) //parent process while
(keepLooping) cout ltlt "Enter text to send to
child " ltlt endl cin.getline(parent_buf,
LB_SIZE) outSize strlen(parent_buf) if
(strncmp(parent_buf, "bye", 3)
0) keepLooping false send (sockfd0,
parent_buf, outSize, 0) cout ltlt "Closing
parent process..." ltlt endl return 0
6
sockpair example output
  • rcotter_at_kc-sce-450p2 sockpair ./sockpair
  • Socket Pair test at Wed Oct 12 141828 2011
  • Enter text to send to child
  • This is the first message
  • Enter text to send to child
  • Child received This is the first message
  • This is the second message
  • Enter text to send to child
  • Child received This is the second message
  • bye
  • Closing parent process...
  • Child received bye
  • Closing child process
  • rcotter_at_kc-sce-450p2 sockpair

7
Traditional UNIX Domain Sockets
  • struct sockaddr_un sa_family_t sun_family
    //AF_LOCAL char sunpath108 //NULL terminated
    path
  • sunpath length is 108 in Linux, but can be 104,
    92, ? in other UNIX based systems.
  • File (path name) must not exist (be linked) prior
    to bind( )

8
UNIX Domain Sockets example
9
Traditional UNIX Domain Sockets
Server Socket
Client Socket
mytemp.txt
unnamed
10
UNIX Domain socket created by bind
11
unix_udp_echos.cpp
include ltiostreamgt, ltfstreamgt, ltsys/socket.hgt,
ltsys/types.hgt include ltsys/un.hgt, ltstring.hgt,
ltunistd.hgt using namespace std define MAXLINE
108 void UDP_echo(int sockfd, sockaddr pcliaddr,
socklen_t clilen) int main(int argc, char
argv) int sockfd struct
sockaddr_un servaddr, cliaddr char
udp_pathMAXLINE if (argc ! 2) cout ltlt
"Usage " ltlt argv0 ltlt " socket_address " ltlt
endl return(1) if (strlen(argv1) lt
MAXLINE) strcpy(udp_path, argv1) else
cout ltlt "Socket Path name too long. Try
again." ltlt endl return(1)
12
unix_udp_echos.cpp
sockfd socket(AF_LOCAL, SOCK_DGRAM,
0) unlink(udp_path) bzero(servaddr,
sizeof(servaddr)) servaddr.sun_family
AF_LOCAL strcpy(servaddr.sun_path,
udp_path) bind(sockfd, (sockaddr ) servaddr,
sizeof(servaddr)) UDP_echo(sockfd, (sockaddr
) cliaddr, sizeof(cliaddr))
13
unix_udp_echos.cpp
void UDP_echo(int sockfd, sockaddr pcliaddr,
socklen_t clilen) int n socklen_t len cha
r mesgMAXLINE for ( ) memset(mesg,
0, MAXLINE) len clilen n
recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr,
len) cout ltlt "We just got " ltlt n ltlt " char "
ltlt mesg ltlt endl sendto(sockfd, mesg, n, 0,
pcliaddr, len)
14
unix_udp_echo.cpp
include ltiostreamgt, ltfstreamgt, ltstdio.hgt,
ltstdlib.hgt, ltsys/socket.hgt include
ltsys/types.hgt, ltsys/un.hgt, ltstring.hgt,
ltunistd.hgt using namespace std define MAXLINE
128 void UDPecho(int sockfd, const sockaddr
pservaddr, socklen_t servlen) int main(int
argc, char argv) int sockfd struct
sockaddr_un cliaddr, servaddr char
udp_pathMAXLINE if (argc ! 2) cout ltlt
"Usage " ltlt argv0 ltlt " socket_address " ltlt
endl return(1) if (strlen(argv1) lt
MAXLINE) strcpy(udp_path, argv1) else
cout ltlt "Socket Path name too long. Try
again." ltlt endl return(1)
15
unix_udp_echo.cpp
sockfd socket(AF_LOCAL, SOCK_DGRAM,
0) bzero(cliaddr, sizeof(cliaddr)) / bind
an address for us / cliaddr.sun_family
AF_LOCAL // Bind the client to a local (unique
but unnamed) file strcpy(cliaddr.sun_path,
tmpnam(NULL)) bind(sockfd, (sockaddr )
cliaddr, sizeof(cliaddr)) //Identify the
servers address (file) bzero(servaddr,
sizeof(servaddr)) / fill in server's address
/ servaddr.sun_family AF_LOCAL strcpy(servad
dr.sun_path, udp_path) UDPecho(sockfd,
(sockaddr ) servaddr, sizeof(servaddr)) close(
sockfd) exit(0)
16
unix_udp_echo.cpp
void UDPecho(int sockfd, const sockaddr
pservaddr, socklen_t servlen) int n char sen
dlineMAXLINE, recvlineMAXLINE 1 while
(cin.getline(sendline, MAXLINE) ! NULL) if
(strncmp ("_bye", sendline, 4)
0) break sendto(sockfd, sendline,
strlen(sendline), 0, pservaddr, servlen) n
recvfrom(sockfd, recvline, MAXLINE, 0, NULL,
NULL) recvlinen 0 / null terminate
/ cout ltlt recvline ltlt endl cout ltlt "We
jumped out of the loop" ltlt endl
17
UNIX Domain Sockets example
18
Summary
  • socketpair( )
  • Used to communicate between related processes.
  • Works like a full-duplex pipe (called a stream
    pipe)
  • Does not rely on transport layer functionality
  • UNIX Domain Sockets
  • Used to communicate between UNIX / Linux
    processes that have shared access to a socket
    file.
  • Can use file permissions to control access.
  • Uses transport layer (TCP or UDP), but not
    network layer (IP)
  • Removes IP / Ethernet packet size limitations
    (but still have transport layer limitations).
Write a Comment
User Comments (0)
About PowerShow.com