Share via


Socket Constructors

Definition

Overloads

Socket()

Creates an unconnected socket, with the system-default type of SocketImpl.

Socket(Proxy)

Creates an unconnected socket, specifying the type of proxy, if any, that should be used regardless of any other settings.

Socket(SocketImpl)

Creates an unconnected Socket with a user-specified SocketImpl.

Socket(InetAddress, Int32)

Creates a stream socket and connects it to the specified port number at the specified IP address.

Socket(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Socket(String, Int32)

Creates a stream socket and connects it to the specified port number on the named host.

Socket(InetAddress, Int32, Boolean)
Obsolete.

Creates a socket and connects it to the specified port number at the specified IP address.

Socket(String, Int32, Boolean)
Obsolete.

Creates a stream socket and connects it to the specified port number on the named host.

Socket(InetAddress, Int32, InetAddress, Int32)

Creates a socket and connects it to the specified remote address on the specified remote port.

Socket(String, Int32, InetAddress, Int32)

Creates a socket and connects it to the specified remote host on the specified remote port.

Socket()

Creates an unconnected socket, with the system-default type of SocketImpl.

[Android.Runtime.Register(".ctor", "()V", "")]
public Socket ();
Attributes

Remarks

Creates an unconnected socket, with the system-default type of SocketImpl.

Added in 1.1.

Java documentation for java.net.Socket.Socket().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to

Socket(Proxy)

Creates an unconnected socket, specifying the type of proxy, if any, that should be used regardless of any other settings.

[Android.Runtime.Register(".ctor", "(Ljava/net/Proxy;)V", "")]
public Socket (Java.Net.Proxy? proxy);
[<Android.Runtime.Register(".ctor", "(Ljava/net/Proxy;)V", "")>]
new Java.Net.Socket : Java.Net.Proxy -> Java.Net.Socket

Parameters

proxy
Proxy

a java.net.Proxy Proxy object specifying what kind of proxying should be used.

Attributes

Exceptions

if the argument proxy is null or of an invalid type.

Remarks

Creates an unconnected socket, specifying the type of proxy, if any, that should be used regardless of any other settings.

If there is a security manager, its checkConnect method is called with the proxy host address and port number as its arguments. This could result in a SecurityException.

Examples: <UL> <LI>Socket s = new Socket(Proxy.NO_PROXY); will create a plain socket ignoring any other proxy configuration.</LI> <LI>Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080))); will create a socket connecting through the specified SOCKS proxy server.</LI> </UL>

Added in 1.5.

Java documentation for java.net.Socket.Socket(java.net.Proxy).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to

Socket(SocketImpl)

Creates an unconnected Socket with a user-specified SocketImpl.

[Android.Runtime.Register(".ctor", "(Ljava/net/SocketImpl;)V", "")]
protected Socket (Java.Net.SocketImpl? impl);
[<Android.Runtime.Register(".ctor", "(Ljava/net/SocketImpl;)V", "")>]
new Java.Net.Socket : Java.Net.SocketImpl -> Java.Net.Socket

Parameters

impl
SocketImpl

an instance of a <B>SocketImpl</B> the subclass wishes to use on the Socket.

Attributes

Exceptions

if an error occurs while creating the socket.

Remarks

Creates an unconnected Socket with a user-specified SocketImpl.

Added in 1.1.

Java documentation for java.net.Socket.Socket(java.net.SocketImpl).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Socket(InetAddress, Int32)

Creates a stream socket and connects it to the specified port number at the specified IP address.

[Android.Runtime.Register(".ctor", "(Ljava/net/InetAddress;I)V", "")]
public Socket (Java.Net.InetAddress? address, int port);
[<Android.Runtime.Register(".ctor", "(Ljava/net/InetAddress;I)V", "")>]
new Java.Net.Socket : Java.Net.InetAddress * int -> Java.Net.Socket

Parameters

address
InetAddress

the IP address.

port
Int32

the port number.

Attributes

Exceptions

if an error occurs while creating the socket.

Remarks

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 is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Java documentation for java.net.Socket.Socket(java.net.InetAddress, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Socket(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

protected Socket (IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer);
new Java.Net.Socket : nativeint * Android.Runtime.JniHandleOwnership -> Java.Net.Socket

Parameters

javaReference
IntPtr

nativeint

A IntPtrcontaining a Java Native Interface (JNI) object reference.

transfer
JniHandleOwnership

A JniHandleOwnershipindicating how to handle javaReference

Remarks

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Socket(String, Int32)

Creates a stream socket and connects it to the specified port number on the named host.

[Android.Runtime.Register(".ctor", "(Ljava/lang/String;I)V", "")]
public Socket (string? host, int port);
[<Android.Runtime.Register(".ctor", "(Ljava/lang/String;I)V", "")>]
new Java.Net.Socket : string * int -> Java.Net.Socket

Parameters

host
String

the host name, or null for the loopback address.

port
Int32

the port number.

Attributes

Exceptions

if the host name could not be resolved into an IP address.

if an error occurs while creating the socket.

Remarks

Creates a stream socket and connects it to the specified port number on the named host.

If the specified host is null it is the equivalent of specifying the address as java.net.InetAddress#getByName InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

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 is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Java documentation for java.net.Socket.Socket(java.lang.String, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Socket(InetAddress, Int32, Boolean)

Caution

deprecated

Creates a socket and connects it to the specified port number at the specified IP address.

[Android.Runtime.Register(".ctor", "(Ljava/net/InetAddress;IZ)V", "")]
[System.Obsolete("deprecated")]
public Socket (Java.Net.InetAddress? host, int port, bool stream);
[<Android.Runtime.Register(".ctor", "(Ljava/net/InetAddress;IZ)V", "")>]
[<System.Obsolete("deprecated")>]
new Java.Net.Socket : Java.Net.InetAddress * int * bool -> Java.Net.Socket

Parameters

host
InetAddress

the IP address.

port
Int32

the port number.

stream
Boolean

if true, create a stream socket; otherwise, create a datagram socket.

Attributes

Exceptions

if an error occurs while creating the socket.

Remarks

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 is created.

If there is a security manager, its checkConnect method is called with host.getHostAddress() and port as its arguments. This could result in a SecurityException.

If UDP socket is used, TCP/IP related socket options will not apply.

This member is deprecated. Use DatagramSocket instead for UDP transport.

Java documentation for java.net.Socket.Socket(java.net.InetAddress, int, boolean).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Socket(String, Int32, Boolean)

Caution

deprecated

Creates a stream socket and connects it to the specified port number on the named host.

[Android.Runtime.Register(".ctor", "(Ljava/lang/String;IZ)V", "")]
[System.Obsolete("deprecated")]
public Socket (string? host, int port, bool stream);
[<Android.Runtime.Register(".ctor", "(Ljava/lang/String;IZ)V", "")>]
[<System.Obsolete("deprecated")>]
new Java.Net.Socket : string * int * bool -> Java.Net.Socket

Parameters

host
String

the host name, or null for the loopback address.

port
Int32

the port number.

stream
Boolean

a boolean indicating whether this is a stream socket or a datagram socket.

Attributes

Exceptions

if the host name could not be resolved into an IP address.

if an error occurs while creating the socket.

Remarks

Creates a stream socket and connects it to the specified port number on the named host.

If the specified host is null it is the equivalent of specifying the address as java.net.InetAddress#getByName InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

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 is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

If a UDP socket is used, TCP/IP related socket options will not apply.

This member is deprecated. Use DatagramSocket instead for UDP transport.

Java documentation for java.net.Socket.Socket(java.lang.String, int, boolean).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Socket(InetAddress, Int32, InetAddress, Int32)

Creates a socket and connects it to the specified remote address on the specified remote port.

[Android.Runtime.Register(".ctor", "(Ljava/net/InetAddress;ILjava/net/InetAddress;I)V", "")]
public Socket (Java.Net.InetAddress? address, int port, Java.Net.InetAddress? localAddr, int localPort);
[<Android.Runtime.Register(".ctor", "(Ljava/net/InetAddress;ILjava/net/InetAddress;I)V", "")>]
new Java.Net.Socket : Java.Net.InetAddress * int * Java.Net.InetAddress * int -> Java.Net.Socket

Parameters

address
InetAddress

the remote address

port
Int32

the remote port

localAddr
InetAddress

the local address the socket is bound to, or null for the anyLocal address.

localPort
Int32

the local port the socket is bound to or zero for a system selected free port.

Attributes

Exceptions

if an error occurs while creating the socket.

Remarks

Creates a socket and connects it to the specified remote address on the specified remote port. The Socket will also bind() to the local address and port supplied.

If the specified local address is null it is the equivalent of specifying the address as the AnyLocal address (see java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress()).

A local port number of zero will let the system pick up a free port in the bind operation.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Added in 1.1.

Java documentation for java.net.Socket.Socket(java.net.InetAddress, int, java.net.InetAddress, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Socket(String, Int32, InetAddress, Int32)

Creates a socket and connects it to the specified remote host on the specified remote port.

[Android.Runtime.Register(".ctor", "(Ljava/lang/String;ILjava/net/InetAddress;I)V", "")]
public Socket (string? host, int port, Java.Net.InetAddress? localAddr, int localPort);
[<Android.Runtime.Register(".ctor", "(Ljava/lang/String;ILjava/net/InetAddress;I)V", "")>]
new Java.Net.Socket : string * int * Java.Net.InetAddress * int -> Java.Net.Socket

Parameters

host
String

the name of the remote host, or null for the loopback address.

port
Int32

the remote port

localAddr
InetAddress

the local address the socket is bound to, or null for the anyLocal address.

localPort
Int32

the local port the socket is bound to, or zero for a system selected free port.

Attributes

Exceptions

if the host name could not be resolved into an IP address.

if an error occurs while creating the socket.

Remarks

Creates a socket and connects it to the specified remote host on the specified remote port. The Socket will also bind() to the local address and port supplied.

If the specified host is null it is the equivalent of specifying the address as java.net.InetAddress#getByName InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

A local port number of zero will let the system pick up a free port in the bind operation.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Added in 1.1.

Java documentation for java.net.Socket.Socket(java.lang.String, int, java.net.InetAddress, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to