DOC PREVIEW
U of I CS 241 - System Programming

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS241 System ProgrammingOutlineSocketSocket Functionssocketbindconnectlistenacceptsend and sendtorecv and recvfromclose and shutdownImplementation of u_openImplementation of u_openImplementation of u_acceptImplementation of u_connectImplementation of u_connectReferenceHTTPHTTP (continued)Sample HTTP exchangeSample HTTP exchangeHTTP exchange in MP5Communications in MP5SummaryCS241 System ProgrammingDiscussion Section 11April 17 – April 20Outlinez Socket Programmingz Library Functionsz UICI Implementationz Hypertext Transfer ProtocolSocketz Standard APIs for sending and receiving data across computer networksz Introduced by BSD operating systems in 1983z POSIX incorporated 4.3BSD sockets and XTI in 2001z #include <sys/socket.h>Socket Functionsz socketz bindz connectz listenz acceptz send, sendtoz recv, recvfromz close, shutdownsocketint socket(int domain, int type, int protocol);z Creates a communication endpointz Parametersz domain: AF_INET (IPv4)z type: SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)z protocol: 0 (socket chooses the correct protocol based on type)z Returns a nonnegative integer corresponding to a socket file descriptor if successful, -1 with errno set if unsuccessfulbindint bind(int socket, const struct sockaddr*address, socklen_t address_len);z Associates the socket with a port on your local machinez struct sockaddr_in used for struct sockaddrsa_family_t sin_family; /* AF_INET */in_port_t sinport; /* port number */struct in_addr sin_addr; /* IP address */z Returns 0 if successful, -1 with errno set if unsuccessfulconnectint connect(int socket, const struct sockaddr*address, socklen_t address_len);z Establishes a link to the well-known port of the remote serverz Initiates the TCP 3-way handshakez Cannot be restarted even if interruptedz Returns 0 if successful, -1 with errno set if unsuccessfullistenint listen(int socket, int backlog);z Puts the socket into the passive state to accept incoming requestsz Internally, it causes the network infrastructure to allocate queues to hold pending requestsz backlog: number of connections allowed on the incoming queuez bind should be called beforehandz Returns 0 if successful, -1 with errno set if unsuccessfulacceptint accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);z Accepts the pending requests in the incoming queuez *address is used to return the information about the client making the connection. z sin_addr.s_addr holds the Internet addressz listen should be called beforehandz Returns nonnegative file descriptor corresponding to the accepted socket if successful, -1 with errno set if unsuccessfulsend and sendtoint send(int socket, const void *msg, int len, int flags);int sendto(int socket, const void *msg, int len, int flags, const struct sockaddr *to, socklet_t tolen);z sends data pointed by msgz sendto is used for unconnected datagram sockets. If used in connection-mode, last two parameters are ignored.z Returns the number of bytes actually sent out if successful, -1 with errno set if unsuccessfulrecv and recvfromint recv(int socket, void *buf, int len, int flags);int recvfrom(int socket, void *buf, int len, int flags, const struct sockaddr *from, socklet_t fromlen);z receives data into the buffer bufz recvfrom is used for unconnected datagram sockets. If used in connection-mode, last two parameters are ignored.z Returns the number of bytes actually read if successful, -1 with errno set if unsuccessfulclose and shutdownint close(int socket);int shutdown(int socket, int how);z closez Prevents any more reads and writesz same function covered in file systemsz shutdownz provides a little more controlz howz 0 – Further receives are disallowedz 1 – Further sends are disallowedz 2 – same as closez Returns 0 if successful, -1 with errno set if unsuccessfulImplementation of u_openint u_open(u_port_t port) {int error;struct sockaddr_in server;int sock;int true = 1;if ((u_ignore_sigpipe() == -1) ||((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1))return -1;if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&true,sizeof(true)) == -1) {error = errno;while ((close(sock) == -1) && (errno == EINTR));errno = error;return -1;}/* continued on the next page */z setsockoptz Sets the options on socketsz SO_REUSEADDR permits the server to be restarted immediatelyz u_ignore_sigpipez Sets SIGPIPE to be ignored. It terminates the process by defaultImplementation of u_openserver.sin_family = AF_INET;server.sin_addr.s_addr = htonl(INADDR_ANY);server.sin_port = htons((short)port);if ((bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1) ||(listen(sock, MAXBACKLOG) == -1)) {error = errno;while ((close(sock) == -1) && (errno == EINTR));errno = error;return -1;}return sock;}z htoln, htonsz Converts the address and port number fields to network byte orderImplementation of u_acceptint u_accept(int fd, char *hostn, int hostnsize) {int len = sizeof(struct sockaddr);struct sockaddr_in netclient;int retval;while (((retval =accept(fd, (struct sockaddr *)(&netclient), &len)) == -1) &&(errno == EINTR));if ((retval == -1) || (hostn == NULL) || (hostnsize <= 0))return retval;addr2name(netclient.sin_addr, hostn, hostnsize);return retval;}z addr2namez Converts the address to an ASCII host nameImplementation of u_connectint u_connect(u_port_t port, char *hostn) {int error;int retval;struct sockaddr_in server;int sock;fd_set sockset;if (name2addr(hostn,&(server.sin_addr.s_addr)) == -1) {errno = EINVAL;return -1;}server.sin_port = htons((short)port);server.sin_family = AF_INET;if ((u_ignore_sigpipe() == -1) ||((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1))return -1;z name2addrz Converts the host name into a binary address and stores it to its second parameterImplementation of u_connectif (((retval =connect(sock, (struct sockaddr *)&server, sizeof(server))) == -1) &&((errno == EINTR) || (errno == EALREADY))) { /* asynchronous */FD_ZERO(&sockset);FD_SET(sock, &sockset);while (((retval = select(sock+1, NULL, &sockset, NULL, NULL)) == -1)&& (errno == EINTR)) {FD_ZERO(&sockset);FD_SET(sock, &sockset);}}if (retval == -1) {error = errno;while ((close(sock) == -1) && (errno == EINTR));errno = error;return -1;}return sock;}Referencez Beej's Guide to Network Programmingz http://beej.us/guide/bgnet/HTTPz Hypertext Transfer Protocolz Delivers virtually all files and resources on the World Wide Webz Uses Client-Server Modelz HTTP transactionz HTTP client opens a connection and sends a request to HTTP serverz HTTP


View Full Document

U of I CS 241 - System Programming

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download System Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view System Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view System Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?