|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Java provides two kinds of sockets: client sockets, implemented in the Socket class, and server sockets, implemented in the ServerSocket class. Understanding the Client Socket ClassTo connect to a host, your client program should include a line such as Socket theConnection = new Socket(hostname, portNumber); (Remember the security restrictions that apply to applets.)
The Socket constructor throws an IOException if it has a problem. Otherwise, you can presume that the Socket is open and ready for communication: BufferedReader theReader = new BufferedReader( new InputStreamReader(theConnection.getInputStream())); BufferedWriter theWriter = new BufferedWriter( new OutputStreamWriter(theConnection.getOutputStream())); Now you can read and write theReader and theWriter in the usual fashion. When youre done with theConnection, call theConnection.close(); This step will also close all the streams, readers, and writers you have associated with this Socket. Understanding ServerSocketsIf you choose to write a server, youll need to write a ServerSocket. Such a socket binds a specified port. To bind port 8000, for example, you write ServerSocket theServerConnection = new ServerSocket(8000);
This code tells the underlying operating system that you intend to offer a service on port 8000. (You arent listening to that port quite yet.) If the runtime environment is able to bind to the specified port, it does so and sets the allowable backlog to a default of 50. (This means that once you have 50 pending requests to connect, all subsequent requests are refused. You can specify a different backlog value in the ServerSocket constructor.) If the runtime environment cannot bind to the port (which happens if the port is already allocated to another service), youll get an IOException. After youve bound the port, you can attach the port and start listening for connections by calling accept(): Socket aSocket = theServerConnection.accept(); When the connection is made, accept() unblocks and returns a Socket. You can open streams, readers, and writers on the Socket the same as you did from the client program. Using Client and Server SocketsListing 40.2 shows a server framework. Our simple server sets up the ServerSocket, then implements four steps:
Listing 40.2 TServer.javaUse This Framework as the Basis for Your Own Server import java.net.*; import java.io.*; public class TServer extends Thread { private static final int PORTNUMBER = 8013; private ServerSocket fServerSocket; public TServer() { super(TServer); try { fServerSocket = new ServerSocket(PORTNUMBER); System.out.println(TServer up and running...); } catch (IOException e) { System.err.println(Exception: couldnt make server socket.); System.exit(1); } } public void run() { Socket theClientSocket; while (true) { // wait for a client to connect if (fServerSocket == null) return; try { theClientSocket = fServerSocket.accept(); // accept the client connection // send a message to the client PrintWriter theWriter = new PrintWriter(new OutputStreamWriter(theClientSocket.getOutputStream())); theWriter.println(new java.util.Date().toString()); theWriter.flush(); // tear down the connection theWriter.close(); theClientSocket.close(); } catch (IOException e) { System.err.println(Exception: + e.getMessage()); System.exit(1); } } } public static void main(String[] args) { TServer theServer = new TServer(); theServer.start(); } }
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |