Client/Server In Java

Download Report

Transcript Client/Server In Java

Client/Server In Java
An Introduction to TCP/IP and
Sockets
IP: Internet Protocol
• IP is a protocol for connecting networks
• IP is a packet-based protocol
• Packets can be dropped, garbled or reordered and duplicated:
– IP promises nothing but best-effort delivery
• IP usually runs over ethernet, but not always
• IP can run over PPP
Addressing
• Every packet has a source and a destination
• A Host is identified by an IP address.
• IP addresses are 4 bytes
– Example: 129.123.12.43
• An IP can belong to only one host in the
Internet
– Routers use the IP addresses to decide where to
direct packets
TCP
• TCP is a stream-based protocol over IP
• TCP promises a reliable transport
• TCP takes care of packet ordering, packet
loss and data corruption.
• A TCP stream is a connection.
TCP over IP
IP Header
IP Data
TCP Header
Src Dst
Type:
TCP Src Dst Seq
Port Port Num
TCP Data
Application Data
TCP Connections
• A TCP connection is between a Client Host
and a Server Host
• The Server is passive; It just waits
• The Client is active
• More than one connection is allowed
between to hosts
• TCP connections are 4-tuples
– {<src-host,src-port>,<dst-host,dst-port>}
Sockets
• The Java interface with a TCP connection is
a socket.
• A socket is a Java object which has methods
to connect, accept connections and transfer
data.
• Core Networking is in java.net.*
• TCP Sockets come in two flavours:
ServerSocket and Socket.
java.net.Socket
• Socket(InetAddress address, int port)
– Creates a stream socket and connects it to the
specified port number at the specified IP
address.
• InputStream getInputStream()
– Returns an input stream for this socket.
• OutputStream getOutputStream()
– Returns an output stream for this socket.
• void close()
– closes this socket (cuts the connection)
HelloClient
Socket sock;
try {
InetAddress host =
InetAddress.getByName(“antares.math.tau.ac.il”);
sock = new Socket(host, 6789);
InputStream instr = sock.getInputStream();
InputStreamReader inread = new InputStreamReader(instr);
BufferedReader inp = new BufferedReader(inread);
PrintStream out = new PrintStream(sock.getOutputStream());
String line;
line = inp.readLine();
out.println(line);
sock.close();
} catch (UnknownHostException e) {
System.err.println("Unknown host: " + e.getMessage());
} catch (IOException e) {
System.err.println("Error connecting: " + e.getMessage());
}
java.net.ServerSocket
• ServerSocket(int port)
– Creates a server socket on a specified port.
• Socket accept()
– Listens for a connection to be made to this
socket and accepts it.
• void close()
– closes this socket (stops listening)
HelloServer
ServerSocket sock;
try {
sock = new ServerSocket(9987);
Socket client;
client = sock.accept();
InputStream instr = client.getInputStream();
InputStreamReader inread = new InputStreamReader(instr);
BufferedReader inp = new BufferedReader(inread);
PrintStream out = new PrintStream(client.getOutputStream());
String line;
line = inp.readLine();
out.println(line);
client.close();
sock.close();
} catch (IOException e) {
System.err.println(”Network Error: " + e.getMessage());
}
Domain Name Service
• Translates Names to Addresses
• Hierarchical
• Hostname format:
hostname.subdomain.domain.tld.
• Reverse DNS: IP->Name
– Special DNS domain: 4.3.2.1.in-addr.arpa
java.net.InetAddress
• static InetAddress getByName(String name)
– returns the address for “name”. Throws
UnknownHostException if it’s unknown.
• This class is the interface from Java to DNS
Multiple clients
• One ServerSocket can accept multiple
clients simultaneously
• Use multithreading to handle them
• One thread waits for “accept()”
• Open a new thread for each connection.