Packages This Package Prev Next Index
§4.6 Class Socket
public final class java.net.Socket
extends java.lang.Object (I-§1.12)
{
// Constructors
public Socket(InetAddress address, int port); §4.6.1
public Socket(InetAddress address, int port, §4.6.2
boolean stream);
public Socket(String host, int port); §4.6.3
public Socket(String host, int port, boolean stream); §4.6.4
// Methods
public void close(); §4.6.5
public InetAddress getInetAddress(); §4.6.6
public InputStream getInputStream(); §4.6.7
public int getLocalPort(); §4.6.8
public OutputStream getOutputStream(); §4.6.9
public int getPort(); §4.6.10
public static void §4.6.11
setSocketImplFactory(SocketImplFactory fac);
public String toString(); §4.6.12
}
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 (I-§4.7).
An application, by changing the socket factory that creates the socket
implementation (I-§4.6.11), can configure itself to create sockets appropriate to the local
firewall.
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 (I-§4.6.11), that factory's
createSocketImpl method (I-§4.13.1) is called to create the actual socket
implementation. Otherwise a "plain" socket (see I-§4.7) is created.
- Parameters:
address
-
the IP address
port
-
the port number
- Throws
- IOException (I-§2.29)
- 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 (I-§4.6.11), that factory's createSocketImpl method (I-§4.13.1) is called to create the actual
socket implementation. Otherwise a "plain" socket (see I-§4.7) is created.
- Parameters:
address
-
the IP address
port
-
the port number
stream
-
if true, create a stream socket; if false, create a datagram socket
- Throws
- IOException (I-§2.29)
- 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 (I-§4.6.11), that factory's createSocketImpl method (I-§4.13.1) is called to create the actual
socket implementation. Otherwise a "plain" socket (see I-§4.7) is created.
- Parameters:
host
-
the host name
port
-
the port number
- Throws
- IOException (I-§2.29)
- 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 (I-§4.6.11), that factory's createSocketImpl method (I-§4.13.1) is called to create the actual
socket implementation. Otherwise a "plain" socket (see I-§4.7) is created.
- Parameters:
host
-
the host name
port
-
the port number
stream
-
a boolean indicating whether this is a stream or datagram socket
- Throws
- IOException (I-§2.29)
- If an I/O error occurs when creating the socket.
close
public void close()
throws IOException
- Closes this socket.
- Throws
- IOException (I-§2.29)
- If an I/O error occurs when closing this socket.
getInetAddress
public InetAddress getInetAddress()
- Returns:
- the remote IP address to which this socket is connected.
getInputStream
public InputStream getInputStream()
throws IOException
- Returns:
- an input stream for reading bytes from this socket.
- Throws
- IOException (I-§2.29)
- Iff an I/O error occurs when creating the input stream.
getLocalPort
public int getLocalPort()
- Returns:
- the local port number to which this socket is connected.
getOutputStream
public OutputStream getOutputStream()
throws IOException
- Returns:
- an output stream for writing bytes to this socket.
- Throws
- IOException (I-§2.29)
- If an I/O error occurs when creating the output stream.
getPort
public int getPort()
- 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 (I-§4.13.1) is called to create the
actual socket implementation.
- Parameters:
fac
-
the desired factory
- Throws
- SocketException (I-§4.17)
- If the factory is already defined.
- Throws
- IOException (I-§2.29)
- If an I/O error occurs when setting the socket factory.
toString
public String toString()
- Returns:
- a string representation of this socket.
- Overrides:
- toString in class Object (I-§1.12.9).
Packages This Package Prev Next Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc.
All rights reserved
Please send any comments or corrections to doug.kramer@sun.com