Presentation is loading. Please wait.

Presentation is loading. Please wait.

Client/Server In Java An Introduction to TCP/IP and Sockets.

Similar presentations


Presentation on theme: "Client/Server In Java An Introduction to TCP/IP and Sockets."— Presentation transcript:

1 Client/Server In Java An Introduction to TCP/IP and Sockets

2 IP: Internet Protocol IP is a protocol for connecting networks IP is a packet-based protocol Packets can be dropped, garbled or re- ordered and duplicated: –IP promises nothing but best-effort delivery IP usually runs over ethernet, but not always IP can run over PPP

3 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

4 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.

5 TCP over IP IP Header SrcDst IP Data TCP Header Type: TCP TCP Data Src Port Dst Port Seq Num Application Data

6 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 –{, }

7 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.

8 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)

9 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()); }

10 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)

11 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()); }

12 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

13 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

14 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.


Download ppt "Client/Server In Java An Introduction to TCP/IP and Sockets."

Similar presentations


Ads by Google