Socket programming


0  0 2016-07-25 07:57:49

Socket programming provides the communication mechanism between computers using network. A client socket program creates a socket at its end of the communication and using that client socket it will connect to server socket.

When the connection is successfully established, the server will creates a socket object at its end of the communication. The client and server will now communicate writing and reading messages.

The class contains a socket functionality, and the class gives a mechanism for the server program to listen for clients and establish connections.

Below are the steps for establishing connection between client and server using Socket :

  • The server instantiates a ServerSocket object, denoting which port number communication is to occur on.
  • The server executes accept() method of the ServerSocket class. The accept method wait for client connect to server socket port.
  • Then server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to server socket.
  • The constructor of Socket class is connect the client to the specified server and port number. If communication is established, the client now capable to communicating with the server.
  • On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket.

After the connections is successfully established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream.

TCP Network is a two way communication protocol, so data can be sent across both sides at the same time.

Below are the classes providing complete set of methods to implement socket programming.

ServerSocket Class

The class is used by server applications to listen for client.

ServerSocket class has 4 constructors that are :

  1. public ServerSocket(int port) throws IOException
  2. public ServerSocket(int port, int backlog) throws IOException
  3. public ServerSocket(int port, int backlog, InetAddress address) throws IOException
  4. public ServerSocket() throws IOException.

ServerSocket  Methods :-

Methods of ServerSocket class are as follows:

  1. public int getLocalPort()
  2. public Socket accept() throws IOException
  3. public void setSoTimeout(int timeout)
  4. public void bind (SocketAddress host, int backlog)

Socket Class :

 Socket class has five constructors that is client uses for connect to a server:

  1. public Socket(String host, int port) throws UnknownHostException, IOException.
  2. public Socket(InetAddress host, int port) throws IOException
  3. public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException.
  4. public Socket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException.
  5. public Socket()


Methods of Socket class are as follows:

  1. public void connect(SocketAddress host, int timeout) throws IOException
  2. public InetAddress getInetAddress()
  3. public int getPort()
  4. public int getLocalPort()
  5. public SocketAddress getRemoteSocketAddress()
  6. public InputStream getInputStream() throws IOException
  7. public OutputStream getOutputStream() throws IOException
  8. public void close() throws IOException


Comments (0)

Post Your Comments


Hong Kong Office

Find Us On

Website Development Company
Digital Marketing
website development company in Hong Kong
website designer in pune