Unformatted text preview:

COP 4610L: Java Networking Part 3 Page 1 Mark Llewellyn ©COP 4610L: Applications in the EnterpriseSpring 2006Java Networking and the Internet – Part 3COP 4610L: Applications in the EnterpriseSpring 2006Java Networking and the Internet – Part 3School of Electrical Engineering and Computer ScienceUniversity of Central FloridaInstructor : Mark [email protected] 242, 823-2790http://www.cs.ucf.edu/courses/cop4610L/spr2006COP 4610L: Java Networking Part 3 Page 2 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets•Step 1is to create a ServerSocket object.• Invoking a ServerSocket constructor such as,ServerSocket server = new ServerSocket (portNumber, queueLength);registers an available TCP port number and specifies the number of clients that can wait to connect to the server (i.e., the queue length).COP 4610L: Java Networking Part 3 Page 3 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)• The port number is used by the clients to locate the server application on the server computer. This is often called the handshake point.• If the queue is full, the server refuses client connections.• The constructor establishes the port where the server waits for connections from clients – a process known as binding the server to the port.• Each client will ask to connect to the server on this port. Only one application at a time can be bound to a specific port on the server.COP 4610L: Java Networking Part 3 Page 4 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)• Port numbers can be between 0 and 65,535. Most OS reserve port numbers below 1024 for system services such as email, and Internet servers. Generally, these ports should not be specified as connection ports in user programs. In fact, some OS require special access privileges to bind to port numbers below 1024. • Programs manage each client connection with a Socket object.COP 4610L: Java Networking Part 3 Page 5 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)•In Step 2, the server listens indefinitely (is said to block) for an attempt by a client to connect. To listen for a client connection, the program calls ServerSocket method accept, as in,Socket connection = server.accept();which returns a Socket when a connection with a client is established. • The Socket allows the server to interact with the client.• The interactions with the client actually occur at a different server port from the handshake port. This allows the port specified in Step 1 to be used again in a multi-threaded server to accept another client connection. We’ll see an example of this later in this set of notes.COP 4610L: Java Networking Part 3 Page 6 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)•In Step 3, the OutputStream and InputStream objects that enable the server to communicate with the client by sending and receiving bytes are established. • The server sends information to the client via an OutputStream and received information from the client via an InputStream. • The server invokes the method getOutputStream on the Socket to get a reference to the Socket’s OutputStreamand invokes method getInputStream on the Socket to get a reference to the Socket’s InputStream.COP 4610L: Java Networking Part 3 Page 7 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)• If primitive types or serializable types (like String) need to be sent rather than bytes, wrapper classes are used to wrap other stream types around the OutputStream and InputStream objects associated with the Socket.ObjectInputStream input = new(ObjectInputStream(connection.getInputStream());ObjectOutputStream output = new(ObjectOutputStream(connection.getOutputStream());COP 4610L: Java Networking Part 3 Page 8 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)• The beauty of establishing these relationships is that whatever the server writes to the ObjectOutputStream is set via the OutputStream and is available at the client’s InputStream, and whatever the client writes to its OutputStream (with a corresponding ObjectOutputStream) is available via the server’s InputStream. • The transmission of the data over the network is seamless and is handled completely by Java.COP 4610L: Java Networking Part 3 Page 9 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)• With Java’s multithreading, you can create multithreaded servers that can manage many simultaneous connections with many clients.• A multithreaded server can take the Socket returned by each call to accept and create a new thread that manages network I/O across that Socket. – Alternatively, a multithreaded sever can maintain a pool of threads (a set of already existing threads) ready to manage network I/O across the new Sockets as they are created. In this fashion, when the server receives a connection, it need not incur the overhead of thread creation. When the connection is closed, the thread is returned to the pool for reuse.COP 4610L: Java Networking Part 3 Page 10 Mark Llewellyn ©More Details on Establishing a Server Using Stream Sockets (cont.)•Step 4is the processing phase, in which the server and client communicate via the OutputStreamand InputStream objects.•In Step 5, when the transmission is complete, the server closes the connection by invoking the closemethod on the streams and on the Socket.COP 4610L: Java Networking Part 3 Page 11 Mark Llewellyn ©More Details on Establishing a Client Using Stream Sockets•Step 1is to create a Socket object to connect to the server. The Socket constructor established the connection with the server. • For example, the statementSocket connection = new Socket(serverAddress, port);uses the Socket constructor with two arguments –the server’s address and the port number. • If the connection attempt is successful, this statement returns a Socket.COP 4610L: Java Networking Part 3 Page 12 Mark Llewellyn ©More Details on Establishing a Client Using Stream Sockets (cont.)• If the connection attempt fails, an instance of a subclass of IOException, since so many program simply catch IOException.• An


View Full Document

UCF COP 4610L - Java Networking and the Internet

Download Java Networking and the Internet
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Networking and the Internet and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Networking and the Internet 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?