Java Sockets
Java Sockets
Java supports the socket interface with a group of classes in the package java.net. It includes Socket classes and classes related to IP addressing.the InetAddress class
An InetAddress represents an IP address. There are subclasses for IPv4 and IPv6 addresses. InetAddress objects are created by calling one of the static methods of the class:static InetAddress getByName(String hostname);
static InetAddress getByAddress(byte[] address);
static InetAddress getLocatHost();
Stream sockets
The implementation of stream sockets is based on two classes: Socket and ServerSocket.Socket(); // Creates an unconnected socket
Socket(InetAddress hostAddress, int port); // Creates a socket and connects to the server at the specified host address and port number
Socket(String hostName, int port); // Creates a socket and connects to the server with the specified host name and port number
InputStream getInputStream(); // Returns the input stream of a connected socket
OutputStream getOutputStream(); // Returns the output stream of a connected socket
ServerSocket(); // Creates an unbound server socket
ServerSocket(int port, int backlog); // Creates a server socket bound to the given port number, with the given queue length
bind(SocketAddress localAddress, int backlog); // Bind the server socket to an IP address and port number, and specify its queue length
Socket accept(); // Accept a connection from a client
Client side
A client wishing to make a connection to a server creates a Socket object with the constructor:Socket(hostname, portnum);
The constructor attempts to make the connection to the server. Alternatively, a program may create an unconnected socket and make a connection explicitly with the connect method.
Once the connection is made (i.e., the constructor or connect call returns), the socket possesses an InputStream and an OutputStream, which the client program can access through the methods getInputStream and getOutputStream. The program uses these streams for communication with the remote server.
Server side
A server starts by creating a ServerSocket object. The ServerSocket constructor specifies a port number and a queue length as parameters. The constructor binds the socket to the given port number and listens for connections. The queue length has the same meaning as the queue length in the Unix listen call. Alternatively, the server may create an unbound ServerSocket with the no-argument constructor and then use the bind method to bind it to a specific port and specify the incoming queue length. There is no listen method.When the server is ready to accept a connection from a client, it calls the accept method:
Socket accept();
This method returns a connected data socket, like the Unix accept call. The server program can then get access to the socket's InputStream and OutputStream and begin communicating with the client.
example
TCPEchoClient.java
TCPEchoServer.java
Datagram sockets
The implementation of datagram sockets is based on two classes: DatagramSocket and DatagramPacket.DatagramSocket(); // Creates a datagram socket and binds it to any available port on the local machine
DatagramSocket(int port); // Creates a datagram socket and binds it to the given port on the local machine
void send(DatagramPacket packet); // Sends the given packet from the datagram socket
void receive(DatagramPacket packet); // Receives a message into the given packet object
DatagramPacket(byte[] buffer, int length); // Creates a datagram packet used to receive a datagram
DatagramPacket(byte[] buffer, int length, InetAddress address, int port); // Creates a datagram packet used to send a datagram to the given address and port
byte[] getData(); // get the data buffer of this packet
int getLength(); // get the length of the message in this packet
InetAddress getAddress(); // get the inet address of this packet
int getPort(); // get the port number of this packet
SocketAddress getSocketAddress(); // get the socket address (ip addres + port) of this packet
Client side
When a client program wants to send a message to a server, it first creates a DatagramSocket. Then it creates a DatagramPacket object to hold the message it wants to send.The message is sent to the server using the send method of DatagramSocket.
void send(DatagramPacket outPacket);
If the client wants to wait for a reply message from the server, it creates a DatagramPacket for the incoming message and calls the receive method:
void receive(DatagramPacket inPacket);
When the method returns, the inPacket will have been filled in with the message. The contents and length of the message can be obtained using the the getData and getLength methods of the packet.
Server side
A server running the UDP protocol starts by creating a DatagramSocket bound
to a particular port.It then creates a DatagramPacket object which will be used to hold an incoming datagram.
To receive an incoming datagram, the server calls the receive method of DatagramSocket, passing the packet as its argument. This call will block until a datagram is received.
When the datagram has been received, the server program can obtain the address of the sending client using the getAddress and getPort methods of the packet. It needs these so that it knows where to send a reply message, if there is one.
The reply is sent by creating a DatagramPacket, using the address and port of the client's socket, and sending it using the send method.
example
UDPEchoClient.java
UDPEchoServer.java