1 / 14

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 re-ordered and duplicated: IP promises nothing but best-effort delivery

baker-diaz
Download Presentation

Client/Server In Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 IP Data Src Dst Type:TCP TCP Header TCP Data SrcPort DstPort SeqNum 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 • {<src-host,src-port>,<dst-host,dst-port>}

  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.

More Related