Network Programing
Network Programing
I YEAR II SEM
Hi TCP connection
request
Hi
Client TCP connection
Got the reply Server
time? GET http://www.cs.huji.ac.il/~dbi
2:00
<file>
time
2
Networking as Layers
3
TCP (Transmission-Control Protocol)
• Enables symmetric byte-stream transmission between two
endpoints (applications)
• Reliable communication channel
• TCP perform these tasks:
– connection establishment by handshake (relatively slow)
– division to numbered packets (transferred by IP)
– error correction of packets (checksum)
– acknowledgement and retransmission of packets
– connection termination by handshake
4
UDP (User Datagram Protocol)
• Enables direct datagram (packet) transmission from one
endpoint to another
• No reliability (except for data correction)
– sender does not wait for acknowledgements
– arrival order is not guaranteed
– arrival is not guaranteed
5
Ports
• A computer may have several applications that communicate
with applications on remote computers through the same
physical connection to the network
• When receiving a packet, how can the computer tell which
application is the destination?
• Solution: each channel endpoint is assigned a unique port that
is known to both the computer and the other endpoint
6
Ports (cont)
• Thus, an endpoint application on the Internet is
identified by
– A host name → 32 bits IP-address
– A 16 bits port
7
Known Ports
• Some known ports are Client
– 20, 21: FTP Application
– 22: SSH
– 23: TELNET mail client
web browser
– 25: SMTP
– 110: POP3
21 23 25 110 80 119
– 80: HTTP
– 119: NNTP
8
Sockets
• A socket is a construct that represents one end-point of a two-
way communication channel between two programs running on
the network
• Using sockets, the OS provides processes a file-like access to
the channel
– i.e., sockets are allocated a file descriptor, and processes can access
(read/write) the socket by specifying that descriptor
9
Sockets (cont)
• A socket stores the IP and port number of the other end-point
computer of the channel
• When writing to a socket, the written bytes are sent to the other
computer and port (e.g., over TCP/IP)
– That is, remote IP and port are attached to the packets
10
Java Sockets
Low-Level Networking
Java Sockets
• Java wraps OS sockets (over TCP) by the objects of class
java.net.Socket
12
A Socket Example
import java.net.*;
import java.io.*;
}
}
13
Socket socket = new Socket("www.cs.huji.ac.il", 80);
InputStream istream = socket.getInputStream();
OutputStream ostream = socket.getOutputStream();
String request =
"GET /~dbi/admin.html HTTP/1.1\r\n" +
"Host: www.cs.huji.ac.il\r\n" + Needed for forwarding
"Connection: close\r\n\r\n"; for example
ostream.write(request.getBytes());
14
Timeout
• You can set timeout values to blocking method read() of
Socket
• Use the method socket.setSoTimeout(milliseconds)
• If timeout is reached before the method returns,
java.net.SocketTimeoutException is thrown
Request/Status-Line \r\n
Header1: value1 \r\n
Header2: value2 \r\n
...
HeaderN: valueN \r\n
\r\n
Message-Body
17
Reading HTTP Messages
18
An Example
19
Parsing the Headers
• So how are the headers themselves represented?
• Headers of a HTTP message must be in
US-ASCII format (1 byte per character)
20
Example: Extracting the Headers
Socket socket = new Socket(argv[0], 80);
InputStream istream = socket.getInputStream();
OutputStream ostream = socket.getOutputStream();
21
Example: Extracting the Headers (cont)
public static boolean endOfHeaders(StringBuffer headers) {
23
Parsing URLs
Working with URLs
• URL (Uniform/Universal Resource Locator): a reference
(address) to a resource on the Internet
http://www.cs.huji.ac.il:80/~dbi/main.html#notes
Protocol
Query
http://www.google.com/search?hl=en&q=dbi+huji&btnG=Search
25
The Class URL
26
Parsing URLs
• The following methods of URL can be used for parsing
URLs
getProtocol(), getHost(), getPort(), getPath(), getFile(),
getQuery(), getRef()
27
URLEncoder
• Contains a utility method encode for converting a string into an
encoded format (used in URLs, e.g. for searches)
• To convert a string, each char is examined in turn:
– Space is converted into a plus sign +
– a-z, A-Z, 0-9, ., -, * and _ remain the same.
– The bytes of all special characters are replaced by hexadecimal
numbers, preceded with %
High-Level Networking
The class URLConnection
• To establish the actual resource, we can use the object
URLConnection obtained by url.openConnection()
• If the protocol of the URL is HTTP, the returned object is of
class HttpURLConnection
• This class encapsulates all socket management and HTTP
directions required to obtain the resource
30
public class ContentExtractor {
32
About HttpURLConnection
• The HttpURLConnection class encapsulates all HTTP transaction
over sockets, e.g.,
– Content decoding
– Redirection
– Proxy indirection
33