#!/usr/local/bin/php ServerSockets  
 
Project4: Echo Server

      Sockets are an easy way for one process to talk to another. 

When writing applications using sockets, it's common to use the client/server model.The client program is used by the user to connect to the server, exchange information, then disconnect.  The server should run continuously, listening for connections on a predefined port. It should be reliable, and take multiple connections, either serially or in parrallel.

ServerSockets and JAVA

Java's implementation of sockets is very powerful and easy to use (especially compared to languages like C ! ).
The ServerSocket class is part of the java.net library. It is used to make server type network applications.
The most basic use of a ServerSocket is as follows:

  ServerSocket server = new ServerSocket(9999);
  Socket connection = server.accept();

This opens a ServerSocket that listens on port 9999 for a connection, when one appears, it is handled by the ordinary Socket connection. Once both parties are connected, the server exchanges information in a similar way to the echo client discussed previously, except in reverse. It reads data from the client, then sends it straight back out again to the client. The following is an example of how the server exchanges information with the echo client (or indeed telnet)

 PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
 BufferedReader  in = new BufferedReader(
                            new  InputStreamReader(connection.getInputStream())
                                       );
 String line = in.readLine(); //receive data from client
 out.println(line); //send data back to client

Here's a summary of the features of the sockets class 
(courtesy of java.sun.com)


Constructor Summary

 ServerSocket(int port) 
           Creates a server socket on a specified port.

 ServerSocket(int port, int backlog) 
           Creates a server socket and binds it to the specified 
           local port number, with the specified backlog.

 ServerSocket(int port, int backlog, InetAddress bindAddr) 
           Create a server with the specified port, 
           listen backlog, and local IP address to bind to.

  

 Method Summary
      Socket accept() 
                     Listens for a connection to be made to this socket and accepts it.

       void close() 
                     Closes this socket.

  InetAddress getInetAddress() 
                     Returns the local address of this server socket.

        int getLocalPort() 
                     Returns the port on which this socket is listening.

        int getSoTimeout() 
                     Retrive setting for SO_TIMEOUT.

   protected void implAccept(Socket s) 
                     Subclasses of ServerSocket use this method to override accept()
                     to return their own subclass of socket.

  static void setSocketFactory(SocketImplFactory fac) 
                     Sets the server socket implementation factory for the application.

       void setSoTimeout(int timeout) 
                     Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

      String toString() 
                     Returns the implementation address 
                     and implementation port of this socket as a String.

The preceding Constructor and Method summaries are the work of Sun Microsystems


 

Contact Me | Personal Projects |  About Me | College Projects |  Graphics | Links | ResuméHeyLog

Home