16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Interprocess Communication<br />

Address Conversion Routines<br />

#include <br />

#include <br />

#include <br />

/* convert character string in dotted decimal notation to/from 32-bit Internet<br />

address */<br />

unsigned long inet_addr(char *ptr);<br />

char *inet_ntoa(struct in_addr inaddr);<br />

A Simple Example<br />

1. The client reads a line from its standard input and writes <strong>the</strong> line to <strong>the</strong> server.<br />

2. The server reads a line from its network input and echoes <strong>the</strong> line back to <strong>the</strong> client.<br />

3. The client reads <strong>the</strong> echoed line and prints it on its standard output.<br />

4. This is known as an echo server. The example shows a concurrent server using connection-oriented<br />

Internet.<br />

Utility Routines<br />

Read or writing n bytes to or from a stream socket.<br />

int readn(int sockfd, char *ptr, int nbytes);<br />

int writen(int sockfd, char *ptr, int nbytes);<br />

int readline(int sockfd, char *ptr, int maxlen);<br />

Note that readline function issues one read system call for every byte of data.<br />

Would like to buffer <strong>the</strong> data using a read system call to read as much data as it can, and <strong>the</strong>n examine <strong>the</strong><br />

buffer one byte at a time.<br />

Read a stream socket one line at a time, and write each line back to <strong>the</strong> sender.<br />

str_echo(int sockfd);<br />

Read contents of FILE, write each line to stream socket, <strong>the</strong>n read line back from socket and write to standard<br />

out.<br />

str_cli(FILE *fd, int sockfd);<br />

Stream Pipes<br />

int s-pipe(int fd[2]); /* unnamed stream pipe */<br />

int ns-pipe(int fd[2]); /* named stream pipe */<br />

168

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

Saved successfully!

Ooh no, something went wrong!