1 / 10

CIS225 – Advanced Java

CIS225 – Advanced Java. Networking. Overview. Introduction Client / Server Computing. Introduction. Networking is tightly integrated in Java Java provides socket-based communication A socket is an abstraction that facilitates client/server communication

Download Presentation

CIS225 – Advanced 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. CIS225 – Advanced Java Networking Y. Daniel Liang Introduction to Java Programming

  2. Overview • Introduction • Client / Server Computing Y. Daniel Liang Introduction to Java Programming

  3. Introduction • Networking is tightly integrated in Java • Java provides socket-based communication • A socket is an abstraction that facilitates client/server communication • Java treats socket communication using the same semantics as file I/O Y. Daniel Liang Introduction to Java Programming

  4. Introduction • Networking is tightly integrated in Java • Java provides socket-based communication • A socket is an abstraction that facilitates client/server communication • Java treats socket communication using the same semantics as file I/O Y. Daniel Liang Introduction to Java Programming

  5. Introduction (cont.) • Java supports stream and datagram sockets • Stream sockets use TCP (Transmission Control Protocol) for data transmission • Datagrams sockets use UDP (User Datagram Protocol) for data transmission Y. Daniel Liang Introduction to Java Programming

  6. Client / Server Computing • Network programming involves a server and one or more clients • The server is passive, it waits for connection requests from clients • Once a connection is made, they communicate via sockets • The server must be running when the client starts Y. Daniel Liang Introduction to Java Programming

  7. Client / Server Computing • To establish a server, you need to create a server socket and attach it to a port • The port is where the server listens for connections • The port identifies the TCP service on the socket • Port numbers between 0 and 1023 are reserved Socket s = new ServerSocket(port); Y. Daniel Liang Introduction to Java Programming

  8. Client / Server Computing • After a socket is created, the server listens for connections Socket clientCon = s.accept(); • The client then can issue a connection request • The client request can use either an IP address or a DNS name Socket serverCon = new Socket(“123.14.12.10”,8000); Y. Daniel Liang Introduction to Java Programming

  9. Client / Server Computing • After the server accepts the client connection request, I/O streams are used for communication • The getInputStream() and getOutputStream() methods are used to get streams on a socket InputStream fromServer = serverCon.getInputStream(); OutputStream toServer = serverCon.getInputStream(); Y. Daniel Liang Introduction to Java Programming

  10. Server Client Client int port = 8000; DataInputStream in; DataOutputStream out; ServerSocket server; Socket socket; server = new ServerSocket (port); socket = server.accept(); in = new DataInputStream (socket.getInputStream()); out = new DataOutputStream (socket.getOutputStream()); System.out.println(in.readDouble()); out.writeDouble(aNumber); int port = 8000; String host = “localhost”; DataInputStream in; DataOutputStream out; Socket socket; socket = new socket(host,8000) in = new DataInputStream (socket.getInputStream()); out = new DataOutputStream (socket.getOutputStream()); out.writeDouble(aNumber); System.out.println(in.readDouble()); Connection Request I/O Streams

More Related