Package java.net |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public final class java.net.Socket extends java.lang.Object { // Constructors public Socket(InetAddress address, int port); public Socket(InetAddress address, int port, boolean stream); public Socket(String host, int port); public Socket(String host, int port, boolean stream); // Methods public void close(); public InetAddress getInetAddress(); public InputStream getInputStream(); public int getLocalPort(); public OutputStream getOutputStream(); public int getPort(); public static void setSocketImplFactory(SocketImplFactory fac); public String toString(); }
This class implements client sockets (also called just "sockets"). A socket is a end point for communication between two machines.
The actual work of the socket is performed by an instance of the SocketImpl class . An application, by changing the socket factory that creates the socket implementation , can configure itself to create sockets appropriate to the local firewall.
public Socket(InetAddress address, int port)throws IOExceptionCreates a stream socket and connects it to the specified port number at the specified IP address.
If the application has specified a socket factory , that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket (see Class SocketImpl) is created.
Parameter Description address the IP address port the port number Throw:
If an I/O error occurs when creating the socket.
public Socket(InetAddress address, int port, boolean stream)throws IOExceptionCreates a socket and connects it to the specified port number at the specified IP address.
If the stream argument is true, this creates a stream socket. If the stream argument is false, it creates a datagram socket.
If the application has specified a server socket factory , that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket (see Class SocketImpl) is created.
Parameter Description address the IP address port the port number stream if true, create a stream socket; if false, create a datagram socket Throw:
If an I/O error occurs when creating the socket.
public Socket(String host, int port)throws UnknownHostException, IOExceptionCreates a stream socket and connects it to the specified port number on the named host.
If the application has specified a server socket factory , that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket (see Class SocketImpl) is created.
Parameter Description host the host name port the port number Throw:
If an I/O error occurs when creating the socket.
public Socket(String host, int port, boolean stream)throws IOExceptionCreates a stream socket and connects it to the specified port number on the named host.
If the stream argument is true, this creates a stream socket. If the stream argument is false, it creates a datagram socket.
If the application has specified a server socket factory , that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket (see Class SocketImpl) is created.
Parameter Description host the host name port the port number stream a boolean indicating whether this is a stream or datagram socket Throw:
If an I/O error occurs when creating the socket.
public void close()throws IOExceptionCloses this socket.
Throw:
If an I/O error occurs when closing this socket.
public InetAddress getInetAddress()Return Value:
Returns the remote IP address to which this socket is connected.
public InputStream getInputStream()throws IOExceptionReturn Value:
Returns an input stream for reading bytes from this socket.
Throw:
Iff an I/O error occurs when creating the input stream.
public int getLocalPort()Return Value:
Returns the local port number to which this socket is connected.
public OutputStream getOutputStream()throws IOExceptionReturn Value:
Returns an output stream for writing bytes to this socket.
Throw:
If an I/O error occurs when creating the output stream.
public int getPort()Return Value:
Returns the remote port number to which this socket is connected.
public static void setSocketImplFactory(SocketImplFactory fac)throws IOExceptionSets the client socket implementation factory for the application. The factory can be specified only once.
When an application creates a new client socket, the socket implementation factory's createSocketImpl method is called to create the actual socket implementation.
Parameter Description fac the desired factory Throw:
If the factory is already defined.
Throw:
If an I/O error occurs when setting the socket factory.
public String toString()Return Value:
Returns a string representation of this socket.
Overrides:
toString in class Object .