Networking with Java CSc 335 ObjectOriented Programming and
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 with Java N-2
Networking Type Resolution Type Checking Compile-Time Run-Time Java API Java Language I/O Abstract Classes Packages Serialization Networks Concurrency Frameworks Collection Anonymous Classes Exceptions Java Swing JDK/JRE Listeners Events Inversion of Control Debugging JUnit Testing & Maintaing Large Programs Eclipse Javadoc Teams PITL Layout Manager Decorator Refactoring Command Composite Design Patterns Coupling/ Cohesion Inheritance Hierarchy Iterator Observer Observable OO Design Reading others’ code Template MVC UML Class Diagrams Sequence Diagrams Package Diagrams N-3 Y-3
Outline • Introduction to Networking Concepts n n n Client-Server and Peer-to-Peer Sockets Streams • Networking in Java • Summary Networking with Java N-4
What is “Networking” • What is “Networking”? n n • 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 n Client - Server u u n Many clients connect with one server. Clients communicate only with server. Peer-to-Peer u u Networking with Java Clients connect to a group of other clients, with no server. Clients communicating directly with each-other. N-5
Client - Server Networking • Advantages: n n n Easier to implement Less coordination involved Easier to maintain control of users • Disadvantage: n Relies on one main server for entire operation Networking with Java N-6
Peer-to-Peer Networking • Advantages: n n n No main server Easier for clients to enter and leave Easier for spreading updates • Disadvantages: n n n Less control over users Harder to coordinate More difficult to implement code Networking with Java N-7
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 N-8
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. n n 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 N-9
Outline • Introduction to Networking Concepts • Networking in Java n n n Sockets Streams Decorating Streams • Summary Networking with Java N-10
Sockets in Java • Found in java. net package • java. net. Server. Socket n n Accepts new incoming connections Creates new Server. Socket for each connection • java. net. Socket n Connects to an existing Server. Socket, through the network Networking with Java N-11
Sockets in Java Host Machine Process Server Socket Output Socket Input Socket Host Machine Process Socket Networking with Java N-12
java. net. Server. Socket • public Server. Socket( int port ) n n Throws IOException Creates a Server. Socket to accept new connections at the specified port • public Socket accept( ) n n n 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 Server. Socket • public void close( ) n n n Throws IOException Closes the server socket. Does not close open sockets. Networking with Java N-13
java. net. Socket • public Socket( String host, int port ) n n Throws IOException, Unknown. Host. Exception Connects to a server socket at the provided address (host) on the provided port • public Input. Stream get. Input. Stream( ) n Throws IOException n Returns the input stream from the socket • public Output. Stream get. Output. Stream( ) n Throws IOException n Returns the output stream from the socket • public void close( ) n Throws IOException n Closes the connection Networking with Java N-14
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 N-15
- Slides: 15