CS241 System Programming: Discussion Section 11 April 24 - April 27
CS241 System Programming: Discussion Section 11 April 24 - April 27
System Programming
Discussion Section 11
April 24 – April 27
Outline
z Connectionless Socket Programming
z Connectionless Library Functions
z UICI UDP Implementation
z MP5 Clarifications
Recall:
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;
z No SIGPIPE problem
z A write to a pipe (e.g. TCP socket) generates a SIGPIPE signal when
there are no active readers.
z UDP provides no information about active readers.
z UDP datagram is “sent correctly” when copied into network buffer (no
error detection).
POSIX sendto
ssize_t sendto(int socket, const void *message, size_t
length, int flags, const struct sockaddr *dest_addr,
socklet_t dest_len);
z Returns the number of bytes actually read if successful, -1 with errno set if
unsuccessful
Implementation of u_sendtohost
ssize_t u_sendtohost(int fd, void *buf, size_t nbytes, char
*hostn,
u_port_t port) {
struct sockaddr_in remote;
z Restarting recvfrom
timedone = add2currenttime(seconds);
if (waitfdtimed(fd, timedone) == -1)
return (ssize_t)(-1);
len = sizeof (struct sockaddr_in);
remote = (struct sockaddr *)ubufp;
while (((retval = recvfrom(fd, buf, nbytes, 0, remote, &len)) == -1)
&& (errno == EINTR)) ;
return retval;
}
z If successful, returns the number of bytes written into buf
z waitfdtimed: Implemented using select – times out after specified number of seconds
u_recvfromtimed
z Suppose you call u_recvfromtimed with a
timeout of 2 seconds and 10 signals come in
1 second apart. When does
u_recvfromtimed time out if no data
arrives?
u_recvfromtimed
z Suppose you call u_recvfromtimed with a
timeout of 2 seconds and 10 signals come in
1 second apart. When does
u_recvfromtimed time out if no data
arrives?
z Answer:
2 seconds after it’s called (timeout is
independent of number of times it needs to
restart – uses end time computed from
add2currenttime)
UICI UDP –
Receiver host information
z Functions for examining the receiver host information (as
filled in the u_buf_t structure)
z Non-reentrant version
Implementation of name2addr
int name2addr(char *name, in_addr_t *addrp) {
struct hostent *hp;
if (isdigit((int)(*name)))
*addrp = inet_addr(name);
else {
hp = gethostbyname(name);
if (hp == NULL)
return -1;
memcpy((char *)addrp, hp->h_addr_list[0], hp->h_length);
}
return 0;
}
z Non-reentrant version
Implementation of u_gethostname
void u_gethostname(u_buf_t *ubufp, char *hostn,
int hostnsize) {
struct sockaddr_in *remotep;
portnumber = ntohs(ubufp->sin_port);
len = snprintf(info, infosize, "port number is %d on host ",
portnumber);
info[infosize-1] = 0; /* in case name did not fit */
if (len >= infosize) return;
u_gethostname(ubufp, info+len, infosize-len);
}
z Returns 1 if the given host name and port number match the info in *ubufp and 0
otherwise
z Does comparison on address obtained from name2addr – not on host name itself
UDP vs. TCP
TCP UDP
Connection-oriented Each message has destination
Reliable Unreliable
z Examples
z http://csil-linux52.cs.uiuc.edu:8008/r?s=asdfasdf
z resource = “/r?s=asdfasdf”
z http://csil-linux52.cs.uiuc.edu:8008/index.html
z resource = “/index.html”
MP5 Datagram Example
name2
RESULT
HELLO name25888
KEEPALIVE
name2 14name2
file_path2 offset2
name1
RESULT
GOODBYE
name1
METASEARCH
HELLO name1
KEEPALIVE14name1
name1 file_path
name16708 offset
keyword
SEARCH 14 keyword
MetaServer
SEARCH 14 keyword
METARESULT
METARESULTkeyword
keywordfile_path3
file_path2
file_path offset3
offset2
offset name1
name2
name3
SEARCH 14 keyword
RESULT name38765
KEEPALIVE
HELLO name3 14name3
file_path3 offset3
name3
Summary
z Connectionless Socket Programming
z Connectionless Library Functions
z UICI UDP Implementation
z MP5 Clarifications