16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

…<br />

connect(sockfd,(struct sockaddr*) & serv_addr, sizeof(serv_addr));<br />

Socket System Calls<br />

#include <br />

#include <br />

int socket(int family, int type, int protocol);<br />

Interprocess Communication<br />

family AF_UNIX<br />

AF_INET<br />

unix internal protocols<br />

internet protocols<br />

type SOCK_STREAM stream socket }<br />

SOCK_DGRAM datagram socket} VALID<br />

SOCK_RAW raw socket }<br />

SOCK_SEQPACKET sequenced packet socket<br />

SOCK_RDM<br />

reliably delivered message<br />

protocol IPPROTO_UDP }<br />

IPPROTO_TCP } AF _INET family<br />

IPPROTO_ICMP}<br />

IPPROTO_RAW }<br />

For an association (5-tuple):<br />

{protocol, local-addr, local-process, remote-addr, remote-process}<br />

socketpair System Call - only for <strong>Unix</strong> domain<br />

#include <br />

#include <br />

int socketpair(int family, int type, int protocol, int sockvec[2]);<br />

similar to <strong>the</strong> "pipe" system call, but bidirectional<br />

int rc, sockfd[2];<br />

rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd);<br />

bind System Call - assigns a name to an unnamed socket<br />

#include <br />

#include <br />

int bind(int sockfd, struct sockaddr *myaddr, int addrlen);<br />

connect System Call - establish connection <strong>with</strong> a server<br />

#include <br />

#include <br />

int connect(int sockfd, struct sockaddr *servaddr, int addrlen);<br />

listen System Call - server is willing to receive connections<br />

int listen(int sockfd, int backlog);<br />

accept System Call<br />

#include <br />

#include <br />

int accept(int sockfd, struct sockaddr *peer, int *addrlen);<br />

accept takes <strong>the</strong> first connection request on <strong>the</strong> queue and creates ano<strong>the</strong>r socket <strong>with</strong> <strong>the</strong> same properties as<br />

sockfd. If <strong>the</strong>re are no connection requests pending, this call blocks <strong>the</strong> caller until one arrives.<br />

int sockfd, newsocketfd;<br />

166

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!