Outline



Exercise 1



Connection-Oriented Service



Connectionless Service



Sockets



Unix Sockets API -- principles



Unix Sockets API -- Managing Connections


/* create a socket of a given type/protocol */
int socket(int domain, int type, int protocol);

/* bind a socket to a given local address (usually default) and port number */
/* mostly used by servers to specify the port on which to accept connections */
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);

/* specify this is a server and how many connections may be pending */
int listen(int s, int backlog);

/* wait for an incoming connection, and return a new socket descriptor */
/* after the connection is established */
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);

/* create a (client) connection to the give IP address and port number */
/* the local IP address and port number are selected automatically, */
/* unless bind was called first (very unusual) */
int connect(int sockfd, struct sockaddr *serv_addr, socklen_t addrlen);

/* close (or half-close, with shutdown) a connection */
int shutdown(int s, int how);
int close(int fd);

/* get the name of the local host */ int gethostname(char *name, int len); /* get one or more IP addresses for the given host name */ struct hostent *gethostbyname(const char *n); struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */ } /* get the protocol number for the given protocol */ /* returns 6 for TCP, 17 (0x11) for UDP */ struct protoent *getprotobyname(const char *n);



Unix Sockets API -- Sending and Receiving data


/* send the data in the buffer pointed to be msg with len bytes */
int send(int s, const void *msg, int len, int flags);

/* send the data on an un-connected socket */
int sendto(int s, const void *msg, int len, unsigned int flags,
           const struct sockaddr *to, socklen_t tolen);

/* send the data on any file descriptor, including connected sockets */
int write(int fd, const void *buf, int count);

/* receive up to len bytes of data from the connected socket */ int recv(int s, void *buf, int len, int flags); /* receive up to len bytes of data from the un-connected socket, */ /* recording the address of the sender. */ int recvfrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from, socklen_t *fromlen); /* read or receive the data on any file descriptor, including connected sockets */ int read(int fd, void *buf, int count);



Windows Sockets API


/* call before making any socket-related call */
int WSAStartup(int version,
               WSADATA *implementation);

/* call after the last socket-related call */
int WSACleanup();



Socket types



Reading a STREAM Socket


/* create a buffer and declare some variables */
char buffer [BUFFER_SIZE];
int num_bytes, new_bytes, socket;


/* we have not yet received anything */
num_bytes = 0;
do {

/* receive as much as the socket can give us now */
/* note the buffer argument begins AFTER all the already-received data */
/* and the buffer size is correspondingly smaller*/ 
  new_bytes = read (socket, buffer + num_bytes, BUFFER_SIZE - num_bytes);

/* the byte count reflects the new bytes we have received */ 
  num_bytes += new_bytes;

/* receive more if we have room, AND if what we received is not complete */
/* deciding if what we received is complete is application-dependent */ 
} while ((num_bytes < BUFFER_SIZE) && expecting_more (buffer, num_bytes));



HTTP



HTTP Client



HTTP server



HTTP timing diagram



HTML