Building Java Client/Server Applications with TCP. Part 2

Building Java Client/Server Applications with TCP. Part 2

The second article in our series on Building Java Client/Server Applications with TCP.
The second article in our series on Building Java Client/Server Applications with TCP.


2. Sockets programming in Java

A network socket is an internal endpoint for sending or receiving data within a node on a computer network.

When a Java program would like to establish a TCP connection between two computers with sockets, the following steps need to be covered:

  • The server instantiates the ServerSocket class with the port number.
  • The server invokes the accept() method of the ServerSocket class and waits until a client connects to the server on the given port.
  • The client instantiates the Socket class, attempting to connect to the specified server and the port number.
  • On the server side, the accept() method returns a reference to a new socket, to be connected to the client socket.
  • On the server side, the accept() method returns a reference to a new socket, to be connected to the client socket.

The ServerSocket class can be instantiated through the constructors presented in the table below:

 Constructor  Meaning
public ServerSocket (int port) throws IOException
Creates a server socket bound to the specified port. An exception occurs if the port is already bound by another  application.

public ServerSocket (int port, int backlog) throws IOException Similar to the previous constructor, the backlog parameter specifies how many incoming clients to store in a wait    queue.
public ServerSocket (int port, int backlog, InetAddress address) throws IOException Similar to the previous constructor, the InetAddress parameter specifies the local IP address to bind to. 
public ServerSocket() throws IOException Creates an unbound server socket. Use the bind() method when you are ready to bind the server socket.

The most important methods of the ServerSocket class are presented in the table below:

 Method      Meaning 
public int getLocalPort()  Returns the port that the server socket is listening on. Useful if you passed in 0 as the port number in a constructor and let the server find a port for you.
public Socket accept() throws IOException Waits for an incoming client. Blocks until either a client connects to the server on the specified port or the socket times out, assuming that the time-out value has been set using the setSoTimeout() method. Otherwise, this method blocks indefinitely.
public void setSoTimeout(int timeout) Sets the time-out value for how long the server socket waits for a client during the accept().
public void bind(SocketAddress host, int backlog) Binds the socket to the specified server and port in the SocketAddress object. Use if you have instantiated the ServerSocket using the no-argument constructor.

Building Java ClientServer Applications with TCP.jpg

The Socket class can be instantiated through the constructors presented in the table below:

 

Constructor

    

Meaning

public Socket(String host, int port) throws UnknownHostException, IOException Creates a socket to the specified server at the specified port.
public Socket(InetAddress host, int port) throws IOException Identical to the previous constructor, the host is denoted by an InetAddress object.
public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException Connects to the specified host and port, creating a socket on the local host at the specified address and port.
public Socket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException
Identical to the previous constructor, the host is denoted by an InetAddress object instead of a String.
public Socket() Creates an unconnected socket. Use the connect() method to connect this socket to a server.

The most important methods of the Socket class are presented in the table below:

 

Method

    

Meaning

public InputStream getInputStream() throws IOException Returns the input stream of the socket. The input stream is connected to the output stream of the remote socket.
public OutputStream getOutputStream() throws IOException Returns the output stream of the socket. The output stream is connected to the input stream of the remote socket.
public void close() throws IOException Closes the socket, which makes this Socket object no longer capable of connecting again to any server.

Interested in Java? Check out our trainings.


Catalin Tudose
Java and Web Technologies Expert
Mai ai întrebări?
Conectați-văcu noi