CS241 System Programming: Discussion Section 11 April 17 - April 20
CS241 System Programming: Discussion Section 11 April 17 - April 20
System Programming
Discussion Section 11
April 17 – April 20
Outline
z Socket Programming
z Library Functions
z UICI Implementation
z #include <sys/socket.h>
Socket Functions
z socket
z bind
z connect
z listen
z accept
z send, sendto
z recv, recvfrom
z close, shutdown
socket
int socket(int domain, int type, int protocol);
z Parameters
z domain: AF_INET (IPv4)
z type: SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)
z protocol: 0 (socket chooses the correct protocol based on
type)
int sendto(int socket, const void *msg, int len, int flags,
const struct sockaddr *to, socklet_t tolen);
z close
z Prevents any more reads and writes
z same function covered in file systems
z shutdown
z provides a little more control
z how
z 0 – Further receives are disallowed
z 1 – Further sends are disallowed
z 2 – same as close
if ((u_ignore_sigpipe() == -1) ||
((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1))
return -1;
z setsockopt
z Sets the options on sockets
z SO_REUSEADDR permits the server to be restarted immediately
z u_ignore_sigpipe
z Sets SIGPIPE to be ignored. It terminates the process by default
Implementation of u_open
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons((short)port);
z htoln, htons
z Converts the address and port number fields to network byte order
Implementation of u_accept
int 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 addr2name
z Converts the address to an ASCII host name
Implementation of u_connect
int 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 name2addr
z Converts the host name into a binary address and stores it to its second parameter
Implementation of u_connect
if (((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;
}
Reference
z Beej's Guide to Network Programming
z http://beej.us/guide/bgnet/
HTTP
z Hypertext Transfer Protocol
z Delivers virtually all files and resources on the
World Wide Web
z Uses Client-Server Model
z HTTP transaction
z HTTP client opens a connection and sends a
request to HTTP server
z HTTP server returns a response message
HTTP (continued)
z Request
z GET /path/to/file/index.html HTTP/1.0
z Other methods (POST, HEAD) possible for request
z Response
z HTTP/1.0 200 OK
z Common Status Codes
z 200 OK
<html>
<body>
(more file contents)
.
.
.
</body>
</html>
HTTP exchange in MP5
z Server-side exchange has been implemented in
helper.{h,c}.
z All you need to do is call them.
z getHTTPRequest
z Converts the request message to a struct Request
z GET, HEAD methods are supported
Pass this value to sendHTTPReply
z struct HeaderList includes the list of headers
z sendHTTPReply
z Generates the response message from the Request
Communications in MP5
Summary
z Socket Programming
z Library Functions
z UICI can be implemented through sockets