#!/usr/local/bin/php
|
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 ! ).
ServerSocket server = new ServerSocket(9999);
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);
Here's a summary of the features of the sockets class
Constructor Summary Creates a server socket on a specified port. ServerSocket(int port, int backlog)
ServerSocket(int port, int backlog, InetAddress bindAddr)
Listens for a connection to be made to this socket and accepts it. void close()
InetAddress getInetAddress()
int getLocalPort()
int getSoTimeout()
protected void implAccept(Socket s)
static void setSocketFactory(SocketImplFactory fac)
void setSoTimeout(int timeout)
String toString()
The preceding Constructor and Method summaries are the work of Sun Microsystems
|
|
|
|