|
Project3: Echo Client
Sockets are
an easy way for one process to talk to another.
The processes could be on the same computer or on different computers,
communicating over a network.
They first came into use with the "Berkeley Systems Distribution" of
UNIX (BSD). Since then they have been adopted by most of the major Operating
Systems and programming languages, eg: WinSock for windows. Before their
use, inter process communication (one program talking to another) was difficult,
especially if the programs were on fundamentally different systems
(like an Apple MAC talking to a Windows machine), or on opposite sides
of a large network (like the Internet).
Basically, a socket is a connection from one program to another, over
which data can be sent and received.
When you use a socket in a program, you initialize it with the internet
address of the computer you want talk to, and the port number on that computer
to connect to. The internet address can be (and often is) the local computer,
often called "localhost". The port number must be between 1 - 65535. The
first few thousand ports are reserved for certain services. For example,
webservers run on port 80, ftp servers run on port 21, and echo servers
run on port 7. It is possible to change their port numbers, but not recommended.
In the higher port numbers, you could find anything running!
When writing applications using sockets, it's common to use the client/server
model. The idea of this is that the server runs continuously, listening
for connections on a predefined port. The client program is used by the
user to connect to the server, exchange information, then disconnect. The
client's port number is usually a random high port number.
Sockets and JAVA
Java's implementation of sockets is very powerful and easy to use (especially
compared to languages like C ! ).
The Socket class is part of the java.net library.
The most basic use of a socket is as follows:
Socket connection = new Socket("localhost", 7));
This opens a socket with the computer named localhost on port
number 7 . You can send and receive information just like reading
and writing a file:
PrintWriter out = new PrintWriter(connection.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
out.println(data); //send data to server
String incoming = in.readLine(); //receive data from server
Here's a summary of the features of the sockets class
(courtesy of java.sun.com)
Constructor Summary
Socket(InetAddress
address, int port)
Creates a stream socket and connects it to the specified
port number at the specified IP address.
Socket(InetAddress
address, int port, InetAddress localAddr, int localPort)
Creates a socket and connects it to the specified
remote address on the specified remote port.
Socket(String host,
int port)
Creates a stream socket and connects it to the specified
port number on the named host.
Socket(String host,
int port, InetAddress localAddr, int localPort)
Creates a socket and connects it to the specified
remote host on the specified remote port.
Method Summary
void close()
Closes this socket.
InetAddress
getInetAddress()
Returns the address to which the socket is connected.
InputStream getInputStream()
Returns an input stream for this socket.
boolean getKeepAlive()
Tests if SO_KEEPALIVE is enabled.
InetAddress
getLocalAddress()
Gets the local address to which the socket is bound.
int getLocalPort()
Returns the local port to which this socket is bound.
OutputStream getOutputStream()
Returns an output stream for this socket.
int getPort()
Returns the remote port to which this socket is connected.
int getReceiveBufferSize()
Gets the value of the SO_RCVBUF option for this Socket,
that is the buffer size used by the platform for input on this Socket.
int getSendBufferSize()
Get value of the SO_SNDBUF option for this Socket,
that is the buffer size used by the platform for output on this Socket.
int getSoLinger()
Returns setting for SO_LINGER.
int getSoTimeout()
Returns setting for SO_TIMEOUT.
boolean getTcpNoDelay()
Tests if TCP_NODELAY is enabled.
void setKeepAlive(boolean
on)
Enable/disable SO_KEEPALIVE.
void setReceiveBufferSize(int
size)
Sets the SO_RCVBUF option to the specified value for this Socket.
void setSendBufferSize(int
size)
Sets the SO_SNDBUF option to the specified value for this Socket.
static void setSocketImplFactory(SocketImplFactory
fac)
Sets the client socket implementation factory for the application.
void setSoLinger(boolean
on, int linger)
Enable/disable SO_LINGER with the specified linger time in seconds.
void setSoTimeout(int
timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
void setTcpNoDelay(boolean
on)
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
void shutdownInput()
Places the input stream for this socket at "end of stream".
void shutdownOutput()
Disables the output stream for this socket.
String toString()
Converts this socket to a String.
The preceding Constructor and Method summaries
are the work of Sun Microsystems
|