1 socket network programming 02/11/2008. admin. r programming assignment 1 update 2
TRANSCRIPT
1
Socket Network Programming
02/11/2008
Admin.
Programming assignment 1 update
2
Recap: P2P Scalability
BitTorrent as an example a swarming protocol each peer is given a set (~50) of peers upon
joining piece selection
• random at startup and (local) rarest after that peer selection
• periodically unchoke a few (4) peers with the highest rates sending to me
• optimistic unchoking
3
4
UDP: Connectionless Demux
DatagramSocket serverSocket = new DatagramSocket(9876);
ClientIP:B
P2
client IP: A
P1P1P3
serverIP: S
SP: 9876
DP: 9157
SP: 9157
DP: 9876
SP: 9876
DP: 5775
SP: 5775
DP: 9876
Source Port (SP) provides “return address”
5
Recap: UDP
closeclientSocket
Server (running on hostid)
read reply fromclientSocket
create socket,clientSocket = DatagramSocket()
Client
Create datagram using (hostid, x) as (dest addr. port),send request using clientSocket
create socket,port=x, forincoming request:serverSocket = DatagramSocket( x )
read request fromserverSocket
write reply toserverSocket
Create socket with port number:DatagramSocket sSock = new DatagramSocket(9876);
If no port number is specified, the OS will pick one
generate reply, create datagram using clienthost address, port number
6
UDP Provides Multiplexing/Demultiplexing
server clientUDP socket space
address: {*:9876}snd/recv buf:
128.36.232.5128.36.230.2
address: {128.36.232.5:53}snd/recv buf:
UDP socket space
address: {198.69.10.10:1500}snd/recv buf:
address: {198.69.10.10:4343}snd/recv buf:
198.69.10.10
Packet demutiplexing is based on (dst address, dst port) at dst%netstat –u –n -a
local addresslocal port
7
Example: Java server (UDP)
import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Createdatagram socket
at port 9876
Create space forreceived datagram
Receivedatagra
m
8
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } }
}
Get IP addrport #, of
sender
Write out datagramto socket
End of while loop,loop back and wait foranother datagram
Create datagramto send to client
9
Example: Java client (UDP)
import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
Createinput stream
Create client socket
Translate hostname to IP
address using DNS
10
Example: Java client (UDP), cont.
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); }
}
Create datagram with data-to-send,
length, IP addr, port
Send datagramto server
Read datagramfrom server
11
UDP Connectionless Demux
DatagramSocket serverSocket = new DatagramSocket(9876);
ClientIP:B
P2
client IP: A
P1P1P3
serverIP: S
SP: 9876
DP: 9157
SP: 9157
DP: 9876
SP: 9876
DP: 5775
SP: 5775
DP: 9876
Source Port (SP) provides “return address”
Recap: TCP Provides Connection-Oriented Demux
Webserver S
Web clienthost B
Source IP: BDest IP: S
source port: x
dest. port: 80
Source IP: BDest IP: S
source port: y
dest. port: 80
Web server
Web clienthost A
Source IP: ADest IP: S
source port: x
dest. port: 80
TCP separates data packetsfrom different peers intodifferent sockets
Big Picture: Connection-Oriented TCP
Socket Programming with TCP
Client must contact server server process must first
be running server must have created
socket (door) that welcomes client’s contact
Client contacts server by: creating client-local TCP
socket specifying IP address, port
number of server process When client creates
socket: client TCP establishes connection to server TCP
When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk
with multiple clients source port numbers
used to distinguish clients
TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server
application viewpoint
TCP Connection-Oriented Demux
TCP socket identified by 4-tuple: source IP address source port number dest IP address dest port number
recv host uses all four values to direct segment to appropriate socket server can easily support many simultaneous TCP
sockets: different connections/sessions are automatically separated into different sockets
16
Connection-Oriented Demux
ClientIP:B
P1
client IP: A
P1P2P4
serverIP: S
SP: x
DP: 80
SP: y
DP: 80
P5 P6 P3
D-IP: SS-IP: A
D-IP: S
S-IP: B
SP: x
DP: 80
D-IP: SS-IP: B
Under the Hood: TCP Multiplexingserver client
TCP socket space
state: listeningaddress: {*:6789, *:*}completed connection queue:sendbuf:recvbuf:
128.36.232.5128.36.230.2
TCP socket space
state: listeningaddress: {*:25, *:*}completed connection queue: sendbuf:recvbuf:
198.69.10.10
state: listeningaddress: {*:25, *:*}completed connection queue: sendbuf:recvbuf:
state: startingaddress: {198.69.10.10:1500, *:*}sendbuf:recvbuf:
local addrlocal port
remote addr
remote port
%netstat –-tcp –all –l -n
Example: Client Initiates Connection
server clientTCP socket space
state: listeningaddress: {*:6789, *.*}completed connection queue:sendbuf:recvbuf:
128.36.232.5128.36.230.2
TCP socket space
state: listeningaddress: {*.25, *.*}completed connection queue:sendbuf:recvbuf:
198.69.10.10
state: listeningaddress: {*.25, *.*}completed connection queue:sendbuf:recvbuf:
state: connectingaddress: {198.69.10.10:1500, 128.36.232.5:6789}sendbuf:recvbuf:
Example: TCP Handshake Doneserver client
TCP socket space
state: listeningaddress: {*:6789, *:*}completed connection queue: {128.36.232.5.6789, 198.69.10.10.1500}sendbuf:recvbuf:
128.36.232.5128.36.230.2
TCP socket space
state: listeningaddress: {*:25, *:*}completed connection queue:sendbuf:recvbuf:
198.69.10.10
state: listeningaddress: {*:25, *:*}completed connection queue:sendbuf:recvbuf:
state: connectedaddress: {198.69.10.10:1500, 128.36.232.5:6789}sendbuf:recvbuf:
Example: Server accept()server client
TCP socket space
state: listeningaddress: {*.6789, *:*}completed connection queue: sendbuf:recvbuf:
128.36.232.5128.36.230.2
TCP socket space
state: listeningaddress: {*.25, *:*}completed connection queue:sendbuf:recvbuf:
198.69.10.10
state: listeningaddress: {*.25, *:*}completed connection queue:sendbuf:recvbuf:
state: connectedaddress: {198.69.10.10.1500, 128.36.232.5:6789}sendbuf:recvbuf:
state: establishedaddress: {128.36.232.5:6789, 198.69.10.10.1500}sendbuf:recvbuf:
Packet sent to the socket with the best match!Packet demutiplexing is based on (dst addr, dst port, src addr, src port)
Client/server socket interaction: TCP
wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()
create socket,port=x, forincoming request:welcomeSocket =
ServerSocket(x)
create socket,connect to hostid, port=xclientSocket =
Socket()
closeconnectionSocket
read reply fromclientSocket
closeclientSocket
Server (running on hostid) Client
send request usingclientSocketread request from
connectionSocket
write reply toconnectionSocket
TCP connection setup
Example
Example client-server app:
1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream)
2) server reads line from socket3) server converts line to
uppercase, sends back to client
4) client reads, prints modified line from socket (inFromServer stream)
outT
oSer
ver
to network from network
inFr
omS
erve
r
inFr
omU
ser
keyboard monitor
Process
clientSocket
inputstream
inputstream
outputstream
TCPsocket
Clientprocess
client TCP socket
Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient {
public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(“server.name", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
Createinput stream
Create client socket,
connect to server
Createoutput stream
attached to socket
Example: Java client (TCP), cont.
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close(); } }
Createinput stream
attached to socket
Send lineto server
Read linefrom server
Example: Java server (TCP)import java.io.*; import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
Createwelcoming socket
at port 6789
Wait, on welcomingsocket for contact
by client
Create inputstream, attached
to socket
Example: Java server (TCP), cont
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence); } } }
Read in linefrom socket
Create outputstream,
attached to socket
Write out lineto socket
End of while loop,loop back and wait foranother client connection
Outline
Recap Socket programming API
UDP TCP implementing a web server
Example: WebServer
A simple web server which supports only simple HTTP/1.0
http://zoo.cs.yale.edu/classes/cs433/programming/examples-java-socket/WebServer/
WebServer Flow
TCP socket space
state: listeningaddress: {*.6789, *.*}completed connection queue: sendbuf:recvbuf:
128.36.232.5128.36.230.2
state: listeningaddress: {*.25, *.*}completed connection queue:sendbuf:recvbuf:
state: establishedaddress: {128.36.232.5:6789, 198.69.10.10.1500}sendbuf:recvbuf:
connSocket = accept()
Create ServerSocket(6789)
read request from connSocket
read local file
write file to connSocket
close connSocketDiscussion: what does each step do and how long does it take?
Writing High Performance Servers: Major Issues
Many socket/IO operations can cause a process to block, e.g., accept: waiting for new connection; read a socket waiting for data or close; write a socket waiting for buffer space; I/O read/write for disk to finish
Thus a crucial perspective of network server design is the concurrency design (non-blocking) for high performance to avoid denial of service
Concurrency is also important for clients!
Writing High Performance Servers: Using Multi-Threads
Using multiple threads so that only the flow
processing a particular request is blocked
Java: extends Thread or implements Runnable interface
Example: a Multi-threaded WebServer, which creates a thread for each request
Problems of Multi-Thread Server
High resource usage, context switch overhead, contended locks
Too many threads throughput meltdown, response time explosion
In practice: bound total number of threads
Event-Driven Programming
Event-driven programming, also called asynchronous i/o Using Finite State Machines (FSM) to monitor the progress of requests Yields efficient and scalable concurrency Many examples: Click router, Flash web server, TP Monitors, etc.
Java: asynchronous i/o for an example see: http://www.cafeaulait.org/books/jnp3/examples/12/
Async I/O in Java
An important class is the class Selector, which is a multiplexer of selectable channel objects example channels: DatagramChannel, Pipe, ServerSocketChannel, SocketChannel
use configureBlocking(false) to make a channel non-blocking
A selector may be created by invoking the open method of this class
Async I/O in Java
A selectable channel registers events (called a SelectionKey) with a selector with the register method
A SelectionKey object contains two operation sets interest Set ready Set
A SelectionKey object has an attachment which can store data often the attachment is a
buffer
Selector
Selection Key
Selectable Channel
register
Async I/O in Java
Call select (or selectNow(), or select(int timeout)) to check for ready events, called the selected key set
Iterate over the set to process all ready events
Problems of Event-Driven Server
Difficult to engineer, modularize, and tune
Little OS and tool support: ‘‘roll your own’’
No performance/failure isolation between FSMs
FSM code can never block (but page faults, garbage collection may still force a block) thus still need multiple threads
38
Backup
39
Example 1: A Relay TCP Client
TCP client
TCP server
writen
readn
fgets
fputs
Check the code at http://zoo.cs.yale.edu/classes/cs433/programming/examples/tcpclient
40
Backup: C/C++ Version
41
Connectionless UDP: Big Picture
sd=Socket(): create socket
bind(sd, …): specify socket local IP address and port number
server client
read()/recv(): receive packets
close(): done
socket(): create socket
close(): done
write()/sendto(): send packets to server, by specifying receiver
address and port number
42
Connection-oriented: Big Picture (C version)
sd=socket(): create socket
bind(sd, …): specify socket address
server client
TCP connection setup
listen(sd, …): specify that socket sd is a listening socket
sd2=accept(sd, …): get a connected connection from the queue for socket sd;
create a new socket identified by sd2
read()/write(): do IO on socket sd2
close(sd2): done
socket(): create socket
bind(): specify socket address
connect(): initialize TCP handshake;return until TCP handshake is done
read()/write(): do IO on the socket
close(): done
optional
43
Unix Programming: Mechanism UNIX system calls and library routines
(functions called from C/C++ programs)
%man 2 <function name>
A word on style: check all return codes
if ((code = syscall()) < 0) {perror("syscall");
}
44
Creating Sockets
#include <sys/types.h>#include <sys/socket.h>int socket(int domain, int type, int protocol);
- creates an endpoint for communication- return value: -1 if an error occurs;
otherwise the return value is a descriptor referencing the socket
- what are the possible outcomes of this system call?
45
Creating Sockets: Parameters
domain : address family (protocol family) determine address structure e.g. AF_UNIX, AF_INET, AF_INET6 we will use AF_INET only
type : service of a socket e.g. SOCK_DGRAM provides unreliable,
connectionless service e.g. SOCK_STREAM provides connection-
oriented reliable byte-stream service
46
Creating Sockets: Parameters (cont.)
protocol : specifies particular protocol Usually already defined by domain and type
(e.g. TCP for AF_INET and SOCK_STREAM; UDP for AF_INET and SOCK_DGRAM)
we will use 0 (default protocol) Example
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) {
perror(“socket”);exit(1);
}
47
Binding the Address for a Socket
#include <sys/types.h>#include <sys/socket.h>int bind(int sd, struct sockaddr *my_addr,
socklen_t addrlen);
- assigns the local address of a socket- return value: -1 if an error occurs;
otherwise 0- what are the possible outcomes of this
system call?
48
Socket Address
Several types of addresses We will use sockaddr_in (<netinet/in.h>)
struct sockaddr_in {sa_family_t sin_family; /*AF_INET*/uint16_t sin_port; /* network order*/
struct in_addr sin_addr;};struct in_addr {
uint32_t s_addr; /* network order*/};
Two types of byte ordering: little endian, and big endian
49
Internet Addresses and Ports
sin_port 16 bits 0-1024 reserved for system well-known ports are important If you specify 0, the OS picks a port
s_addr 32 bits INADDR_ANY for any local interface address
50
Internet Addresses and Ports: Example
struct sockaddr_in myaddr;
bzero( (char*)myaddr, sizeof(myaddr) );myaddr.sin_family = AF_INET;myaddr.sin_port = htons(80); /* bind to HTTP port*/myaddr.sin_addr.s_addr = htos(INADDR_ANY); /* any address*/
if ( (bind(sockfd, (struct sockaddr*)&myaddr, sizeof(myaddr)) < 0 ) {perror(“bind”);exit(1);
}
51
Set a Socket in the Listening State (Server)
#include <sys/types.h>#include <sys/socket.h>int listen(int sd, int backlog);
- Specify the willingness to accept new connection
- backlog : specify the number of pending connections
- return value: -1 if an error occurs; otherwise 0- what are the possible outcomes of this system
call?
52
Initialize Connection Setup (Client)
#include <sys/types.h>#include <sys/socket.h>int connect(int sd, const struct sockaddr *serv_addr,
socklen_t addrlen);
- For SOCK_STREAM, initialize connection to the server; for SOCK_DGRAM, just set the destination address and set the socket in connected state
- return value: -1 if an error occurs; otherwise 0- what are the possible outcomes of this system
call?
53
Accept a Connection (Server)
#include <sys/types.h>#include <sys/socket.h>int accept(int sd, struct sockaddr *peer_addr,
socklen_t addrlen);
- remove the first connection from the pending connection queue, create a new socket in connected state, the original sd is not changed and still in listening state
- return value: -1 if an error occurs; otherwise the descriptor of the newly connected socket
- what are the possible outcomes of this system call?
54
Read/Write to a Socket
read()/write() of the file interface for connected-oriented
Socket specific system call send()/sendto()/sendmsg() recv()/recvfrom()/recvmsg()
55
Read from a socket by using read()
#include <unistd.h>ssize_t read(int sockfd, void *buf, size_t
count);
- read up to count from the socket- return value: -1 if an error occurs; 0 if
end of file; otherwise number of bytes read
- what are the possible outcomes of this system call?
56
Write to a socket by using write()
#include <unistd.h>ssize_t write(int sockfd, const void *buf,
size_t count);
- write up to count to the socket- return value: -1 if an error occurs;
otherwise number of bytes write- what are the possible outcomes of this
system call?
57
Send to a Socket
#include <sys/types.h>#include <sys/socket.h>int send(int sd, const void *msg, size_t len, int
flags);int sendto(int sd, const void *msg, size_t len, int
flags, const struct sockaddr *to, socklen_t tolen);int sendmsg(int sd, const struct msghdr *msg, int
flags)
- return value: -1 if an error occurs; otherwise the number of bytes sent
58
sendmsg(): scatter and collectstruct msghdr {
void *msg_name; // peer addresssocklen_t msg_namelen; // address length struct iovec *msg_iov; // io vectorsize_t msg_iovlen; // io vector lengthvoid *msg_control; socklen_t msg_controllen;int msg_flags;
};struct iovec {
void *iov_base;size_t iov_len;
};
59
Receive from a Socket
#include <sys/types.h>#include <sys/socket.h>int recv(int sd, void *buf, size_t len, int flags);int recvfrom(int sd, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t fromlen);int recvmsg(int sd, struct msghdr *msg, int flags);
- return value: -1 if an error occurs; otherwise the number of bytes received
60
Close a Socket
#include <unistd.h>int close(int sd);
- return value: -1 if an error occurs; otherwise 0
#include <sys/socket.h>int shutdown(int sd, int how);- how : if 0, no further receives; if 1, no further
sends; if 2, no further sends or receives
- return value: -1 if an error occurs; otherwise 0
61
Support Routines: Address from and to
String Formats#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);return value: positive if successful
const char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
return value: NULL is error
Note: inet_addr() deprecated!
62
Support Routines: Network/Host Order
#include <netinet/in.h>
unsigned long int htonl(unsigned long int hostlong);unsigned short int htons(unsigned short int
hostshort);
unsigned long int ntohl(unsigned long int networklong);
unsigned short int ntohs(unsigned short int networkshort);
63
DNS Service
#include <netdb.h>extern int h_errno;
struct hostent *gethostbyname(const char *name);
Struct hostent {char *h_name; // official namechar **h_aliases; // a list of aliasesint h_addrtype;int h_length;char **h_addr_list;
}#define h_addr h_addr_list[0]- return value: NULL if fails
64
Summary: Socket Programming %man 2 <name> System calls (functions)
int socket(int domain, int type, int protocol); int bind(int sd, struct sockaddr *my_addr, socklen_t
addrlen); int listen(int sd, int backlog); int connect(int sd, const struct sockaddr *serv_addr,
socklen_t addrlen); int accept(int sd, struct sockaddr *peer_addr, socklen_t
addrlen);
read(int sockfd, void *buf, size_t count); write(int sockfd, const void *buf, size_t count)
65
Asynchronous Network Programming
(C/C++)
66
A Relay TCP Client: telnet-like Program
TCP client
TCP server
writen
readn
fgets
fputs
http://zoo.cs.yale.edu/classes/cs433/programming/examples-c-socket/tcpclient
67
Method 1: Process and Thread process
fork() waitpid()
Thread: light weight process pthread_create() pthread_exit()
68
pthread
Void main() { char recvline[MAXLINE + 1]; ss = new socketstream(sockfd);
pthread_t tid; if (pthread_create(&tid, NULL, copy_to, NULL)) { err_quit("pthread_creat()"); }
while (ss->read_line(recvline, MAXLINE) > 0) { fprintf(stdout, "%s\n", recvline); }}
void *copy_to(void *arg) { char sendline[MAXLINE];
if (debug) cout << "Thread create()!" << endl; while (fgets(sendline, sizeof(sendline), stdin)) ss->writen_socket(sendline, strlen(sendline));
shutdown(sockfd, SHUT_WR); if (debug) cout << "Thread done!" << endl;
pthread_exit(0);}
69
Method 2: Asynchronous I/O (Select)
select: deal with blocking system callint select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
FD_CLR(int fd, fd_set *set);FD_ZERO(fd_set *set);FD_ISSET(int fd, fd_set *set);FD_SET(int fd, fd_set *set);
70
Method 3: Signal and Select
signal: events such as timeout
71
Examples of Network Programming
Library to make life easier Four design examples
TCP Client TCP server using select TCP server using process and thread Reliable UDP
Warning: It will be hard to listen to me reading through the code. Read the code.
72
Example 2: A Concurrent TCP Server Using Process or Thread
Get a line, and echo it back Use select() For how to use process or thread, see
later Check the code at:
http://zoo.cs.yale.edu/classes/cs433/programming/examples-c-socket/tcpserver
Are there potential denial of service problems with the code?
73
Example 3: A Concurrent HTTP TCP Server Using Process/Thread
Use process-per-request or thread-per- request
Check the code at:http://zoo.cs.yale.edu/classes/cs433/programming/examples-c-socket/simple_httpd
74
Example 4: Reliable UDP
How to implement timeout? Use SIGALARM Use setjmp() Check the code at:http://zoo.cs.yale.edu/classes/cs433/programming/examples-c-socket/udptimeout