0% found this document useful (0 votes)
70 views

CS241 System Programming: Discussion Section 11 April 17 - April 20

The document discusses socket programming and the Hypertext Transfer Protocol (HTTP). It covers key socket functions like socket, bind, connect, listen, accept, and close. It also provides examples of implementing a basic client and server using these functions. The document concludes with an overview of HTTP, including the basic request/response model and a sample message exchange between an HTTP client and server.

Uploaded by

Gaurav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

CS241 System Programming: Discussion Section 11 April 17 - April 20

The document discusses socket programming and the Hypertext Transfer Protocol (HTTP). It covers key socket functions like socket, bind, connect, listen, accept, and close. It also provides examples of implementing a basic client and server using these functions. The document concludes with an overview of HTTP, including the basic request/response model and a sample message exchange between an HTTP client and server.

Uploaded by

Gaurav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CS241

System Programming
Discussion Section 11
April 17 – April 20
Outline
z Socket Programming
z Library Functions
z UICI Implementation

z Hypertext Transfer Protocol


Socket
z Standard APIs for sending and receiving data
across computer networks

z Introduced by BSD operating systems in 1983

z POSIX incorporated 4.3BSD sockets and XTI in


2001

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 Creates a communication endpoint

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)

z Returns a nonnegative integer corresponding to a socket file


descriptor if successful, -1 with errno set if unsuccessful
bind
int bind(int socket, const struct sockaddr
*address, socklen_t address_len);

z Associates the socket with a port on your local machine

z struct sockaddr_in used for struct sockaddr


sa_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 unsuccessful


connect
int connect(int socket, const struct sockaddr
*address, socklen_t address_len);

z Establishes a link to the well-known port of the


remote server

z Initiates the TCP 3-way handshake


z Cannot be restarted even if interrupted

z Returns 0 if successful, -1 with errno set if


unsuccessful
listen
int listen(int socket, int backlog);

z Puts the socket into the passive state to accept incoming


requests

z Internally, it causes the network infrastructure to allocate


queues to hold pending requests
z backlog: number of connections allowed on the incoming
queue

z bind should be called beforehand

z Returns 0 if successful, -1 with errno set if unsuccessful


accept
int accept(int socket, struct sockaddr *restrict
address, socklen_t *restrict address_len);

z Accepts the pending requests in the incoming queue

z *address is used to return the information about the client


making the connection.
z sin_addr.s_addr holds the Internet address

z listen should be called beforehand

z Returns nonnegative file descriptor corresponding to the


accepted socket if successful, -1 with errno set if
unsuccessful
send and sendto
int 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 msg

z 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 unsuccessful
recv and recvfrom
int 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 buf

z 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 unsuccessful
close and shutdown
int close(int socket);

int shutdown(int socket, int how);

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

z Returns 0 if successful, -1 with errno set if unsuccessful


Implementation of u_open
int 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 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);

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, 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

z 404 Not Found

z 500 Server Error


Sample HTTP exchange
z Scenario
z Client wants to retrieve the file at the following URL
(http://www.somehost.com/path/file.html)

z What a client does


z Client opens a socket to the host www.somehost.com, port
80
z Client sends the following message through the socket
GET /path/file.html HTTP/1.0
From: [email protected]
User-Agent: HTTPTool/1.0
[blank line here]
Sample HTTP exchange
z What a server does
z Server responds through the same socket
HTTP/1.0 200 OK
Date: Mon, 17 Apr 2006 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354

<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

z Hypertext Transfer Protocol


z Request
z Response

You might also like