Package java.net Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class Socket

Constructors , Methods

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.


Constructors


Socket

public Socket(InetAddress  address, int  port) 
throws IOException 

Creates 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.

ParameterDescription
address the IP address
port the port number

Throw:

IOException

If an I/O error occurs when creating the socket.


Socket

public Socket(InetAddress  address, int  port, boolean  stream) 
throws IOException 

Creates 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.

ParameterDescription
address the IP address
port the port number
stream if true, create a stream socket; if false, create a datagram socket

Throw:

IOException

If an I/O error occurs when creating the socket.


Socket

public Socket(String  host, int  port) 
throws UnknownHostException, IOException 

Creates 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.

ParameterDescription
host the host name
port the port number

Throw:

IOException

If an I/O error occurs when creating the socket.


Socket

public Socket(String  host, int  port, boolean  stream) 
throws IOException 

Creates 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.

ParameterDescription
host the host name
port the port number
stream a boolean indicating whether this is a stream or datagram socket

Throw:

IOException

If an I/O error occurs when creating the socket.


Methods


close

public void close() 
throws IOException 

Closes this socket.

Throw:

IOException

If an I/O error occurs when closing this socket.


getInetAddress

public InetAddress getInetAddress() 

Return Value:

Returns the remote IP address to which this socket is connected.


getInputStream

public InputStream getInputStream() 
throws IOException 

Return Value:

Returns an input stream for reading bytes from this socket.

Throw:

IOException

Iff an I/O error occurs when creating the input stream.


getLocalPort

public int getLocalPort() 

Return Value:

Returns the local port number to which this socket is connected.


getOutputStream

public OutputStream getOutputStream() 
throws IOException 

Return Value:

Returns an output stream for writing bytes to this socket.

Throw:

IOException

If an I/O error occurs when creating the output stream.


getPort

public int getPort() 

Return Value:

Returns the remote port number to which this socket is connected.


setSocketImplFactory

public static void
setSocketImplFactory(SocketImplFactory  fac) 
throws IOException 

Sets 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.

ParameterDescription
fac the desired factory

Throw:

SocketException

If the factory is already defined.

Throw:

IOException

If an I/O error occurs when setting the socket factory.


toString

public String toString() 

Return Value:

Returns a string representation of this socket.

Overrides:

toString in class Object .



Top© 1996 Sun Microsystems, Inc. All rights reserved.