1 / 15

Networking with Java

Networking with Java. CSc 335 Object-Oriented Programming and Design Spring 2009. Acknowledgements. These slides were written by Craig Barber. Some slides from Martin Stepp were used. The slides were modified slightly by Richard Snodgrass and reorganized by Ivan Vazquez. Networking.

ronna
Télécharger la présentation

Networking with 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. Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009

  2. Acknowledgements • These slides were written by Craig Barber. • Some slides from Martin Stepp were used. • The slides were modified slightly by Richard Snodgrass and reorganized by Ivan Vazquez. Networking with Java

  3. Networking Type Resolution Type Checking Compile-Time Run-Time Java API Java Language Abstract Classes I/O Serialization Networks Concurrency Frameworks Collection Packages Anonymous Classes Exceptions Java Swing Template JDK/JRE Listeners Events Inversion of Control Iterator Command MVC Debugging Layout Manager JUnit Testing & Maintaing Large Programs Observer Observable Eclipse Decorator Composite Design Patterns Coupling/ Cohesion Javadoc Teams PITL OO Design UML Reading others’ code Inheritance Hierarchy Class Diagrams Sequence Diagrams Package Diagrams N-3 Refactoring Refactoring Refactoring Y-3 Y-3

  4. Outline • Introduction to Networking Concepts • Client-Server and Peer-to-Peer • Sockets • Streams • Networking in Java • Summary Networking with Java

  5. What is “Networking” • What is “Networking”? • Basic: getting two or more computers to send data to each other • Practical: having programs on separate computers interact with one another • Types of Networking • Client - Server • Many clients connect with one server. • Clients communicate only with server. • Peer-to-Peer • Clients connect to a group of other clients, with no server. • Clients communicating directly with each-other. Networking with Java

  6. Client - Server Networking • Advantages: • Easier to implement • Less coordination involved • Easier to maintain control of users • Disadvantage: • Relies on one main server for entire operation Networking with Java

  7. Peer-to-Peer Networking • Advantages: • No main server • Easier for clients to enter and leave • Easier for spreading updates • Disadvantages: • Less control over users • Harder to coordinate • More difficult to implement code Networking with Java

  8. How Does Networking Work? • Computers connect to each other through links called sockets, each associated with a single computer. • A network stream is created by connecting a socket on one computer to a socket on another computer. • Applications communicate by sending data through streams to each other. • Note: streams are also used in Java for input and output. Networking with Java

  9. Sockets • A socket is a connection on one computer used to send data back and forth • The application consists of multiple processes, one running on each computer. • Sockets are created by the process on each computer. • The sockets then establish a connection to each other. • One process sets up a server socket to receive a connection. • The other process sets up a client socket to establish the connection with the server socket. Networking with Java

  10. Outline • Introduction to Networking Concepts • Networking in Java • Sockets • Streams • Decorating Streams • Summary Networking with Java

  11. Found in java.net package java.net.ServerSocket Accepts new incoming connections Creates new ServerSocket for each connection java.net.Socket Connects to an existing ServerSocket, through the network Sockets in Java Networking with Java

  12. Sockets in Java Host Machine Host Machine Process Process Socket Server Socket Output Socket Socket Input Socket Socket Input Socket Process Host Machine Socket Networking with Java

  13. public ServerSocket( int port ) Throws IOException Creates a ServerSocket to accept new connections at the specified port public Socket accept( ) Throws IOException Waits for an incoming connection, establishes the new connection, and returns a socket for that connection Multiple applications can connect to the same ServerSocket public void close( ) Throws IOException Closes the server socket. Does not close open sockets. java.net.ServerSocket Networking with Java

  14. public Socket( String host, int port ) Throws IOException, UnknownHostException Connects to a server socket at the provided address (host) on the provided port public InputStream getInputStream( ) Throws IOException Returns the input stream from the socket public OutputStream getOutputStream( ) Throws IOException Returns the output stream from the socket public void close( ) Throws IOException Closes the connection java.net.Socket Networking with Java

  15. Summary • Networking is actually doable, when using Java’s convenient API. • Networking is integrated with input and output: both use streams. • Decorators make your life much easier. • Advanced applications often use serialization and threads. Networking with Java

More Related