Abstract Class SocketImpl in Java

0
257
digitization, transformation, binary
Spread the love

Abstract Class SocketImpl in Java

The abstract class SocketImpl is a common superclass of classes that actually implements sockets. It is used to create both client socket and server sockets. A plain socket implements these methods exactly as described without attempting to go through a firewall or proxy.
SocketImpl provides the environment-specific implementations of stream-based socket communication for Socket and ServerSocket.

The structure of the class SocketImpl is given as:

public abstract class java.net.SocketImpl extends java.lang.Object{
//constructor
public SocketImpl();
//the default constructor for a socket implementation.
//member elements
protected InetAddress address;// the IP address of the remote end of the socket.
protected FileDescriptor descriptor;//the file descriptor object for this socket.
protected int localPort;// the local port to which this socket is connected.
protected int port;// the port number on which the remote machine (hos)to which this socket is connected.
//method
protected abstract void accept(SocketImpl sock)throws IOException;
protected abstract int available()throws IOException;
protected abstract void bind(InetAddress host,int port)throws IOException;
protected abstract void close()throws IOException;
protected abstract void conect(InetAddress address,int port)throws IOException;
protected abstract void conect(String host,int port)throws IOException;
protected abstract void create(boolean stream)throws IOException;
protected FileDescriptor getFileDescriptor();
protected InetAddress getInetAddress();
protected int getLocalPort();
protected int getPort();
protected abstract InputStream getInputStream()throws IOException;
protected abstract OutputStream getOutputStream()throws IOException;
protected abstract void listen(int count)throws IOException;
public String toString();//Overrides Object
} 

The details of the class structure are given as follows:

protected InetAddress address;

protected InetAddress address represents the remote IP address to which this socket is connected.

protected FileDescriptor fd;

protected FileDescriptor fd represents the file descriptor that represents this socket.

protected int localport;

protected int localport represents the local port number of this socket.

protected int port;

protected int port represents the remote port number of this socket.

public String toString();

public String toString() method returns a String that contains a representation of this object.

READ  Class InetAddress in Java

This method returns the string representation of this SocketImpl.

protected abstract void accept(SocketImpl s);

protected abstract void accept(SocketImpl s) method accepts a connection. The method connects the given socket s to a remote host in response to the remote host’s connection request on this SocketImpl.

Parameter
s – A SocketImpl to connect.

protected abstract int available();

protected abstract int available() method returns the number of bytes that can be read from the socket without waiting for more data to arrive.

This method returns the number of bytes that can be read without blocking.

protected abstract void bind(InetAddress host, int port);

protected abstract void bind(InetAddress host, int port) method binds the socket to the given address and port. If the address or the port is unavailable, an exception is thrown.

Parameter
host – An IP address.
port – A port number.

protected abstract void close();

protected abstract void close() method closes the socket, releasing any system resources it holds.

protected abstract void connect(String host, int port);

protected abstract void connect(String host, int port) method connects this socket to the specified port on the given host.

Parameter
host – A remote hostname.
port – A port number on the remote host.

protected abstract void connect(InetAddress address, int port);

protected abstract void connect(InetAddress address, int port) method connects this socket to the specified port on the host at the given address.

Parameter
address – A remote IP address.
port – A port number on the remote host.

protected abstract void create(boolean stream);

protected abstract void create(boolean stream) method creates a socket that is not bound and not connected. If the stream argument is true, a stream socket is created. Otherwise, a datagram socket is created.

READ  Class URL in Java

Parameter
stream – A boolean value that indicates if this socket is a stream socket.

protected FileDescriptor getFileDescriptor();

protected FileDescriptor getFileDescriptor() method returns the file descriptor associated with this SocketImpl.

This method returns the file descriptor for this socket.

protected InetAddress getInetAddress();

protected InetAddress getInetAddress() method returns the IP address of the remote host to which this SocketImpl is connected.

This method returns the remote IP address to which this SocketImpl is connected.

protected abstract InputStream getInputStream();

protected abstract InputStream getInputStream() method returns an InputStream that reads data from the socket.

This method returns an InputStream that wraps this socket.

protected int getLocalPort();

protected int getLocalPort() method returns the local port number that is the origin of the socket.

This method returns the local port number from which this SocketImpl originates.

protected abstract OutputStream getOutputStream();

protected abstract OutputStream getOutputStream() method returns an OutputStream that sends data through the socket.

This method returns an OutputStream that wraps this socket.

protected int getPort();

protected int getPort() method returns the port number of the remote host to which this socket is connected.

This method returns the remote port number to which this SocketImpl is connected.

protected abstract void listen(int backlog);

protected abstract void listen(int backlog) method requests that the local system listens for connections and accept them on behalf of this object. The accepted connections are placed in a queue of the specified length. When there are connections in the queue, a call to this object’s accept() method removes a connection from the queue and immediately returns. If the queue is full, additional connection requests are refused.

READ  Common Interfaces like Peer and abstract interfaces in Java

This object can directly accept a connection if its accept() method has been called and is waiting for a connection. Otherwise, the local system rejects connections to this socket unless listen() has been called.

Parameter
backlog – The maximum length of pending connections queue.

Apart from these methods, the SocketImpl class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)


Spread the love
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here