public class SocksSocket extends Socket
Using Socks package can be as easy as that:
import Socks.*;
....
try{
//Specify SOCKS5 proxy
Proxy.setDefaultProxy("socks-proxy",1080);
//OR you still use SOCKS4
//Code below uses SOCKS4 proxy
//Proxy.setDefaultProxy("socks-proxy",1080,userName);
Socket s = SocksSocket("some.host.of.mine",13);
readTimeFromSock(s);
}catch(SocksException sock_ex){
//Usually it will turn in more or less meaningfull message
System.err.println("SocksException:"+sock_ex);
}
However if the need exist for more control, like resolving addresses remotely, or using some non-trivial authentication schemes, it can be done.
| Modifier and Type | Field and Description |
|---|---|
protected String |
localHost |
protected InetAddress |
localIP |
protected int |
localPort |
protected Proxy |
proxy |
protected String |
remoteHost |
protected InetAddress |
remoteIP |
protected int |
remotePort |
| Modifier | Constructor and Description |
|---|---|
|
SocksSocket(InetAddress ip,
int port)
Tryies to connect to given ip and port
using default proxy.
|
protected |
SocksSocket(InetAddress ip,
int port,
Proxy proxy) |
|
SocksSocket(Proxy p,
InetAddress ip,
int port)
Connects to given ip and port using given Proxy server.
|
|
SocksSocket(Proxy p,
String host,
int port)
Connects to host port using given proxy server.
|
|
SocksSocket(String host,
int port)
Tryies to connect to given host and port
using default proxy.
|
protected |
SocksSocket(String host,
int port,
Proxy proxy)
These 2 constructors are used by the SocksServerSocket.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Same as Socket
|
String |
getHost()
Returns remote host name, it is usefull in cases when addresses
are resolved by proxy, and we can't create InetAddress object.
|
InetAddress |
getInetAddress()
Get remote host as InetAddress object, might return null if
addresses are resolved by proxy, and it is not possible to resolve
it locally
|
InputStream |
getInputStream()
Same as Socket
|
InetAddress |
getLocalAddress()
Get address assigned by proxy to make a remote connection,
it might be different from the host specified for the proxy.
|
String |
getLocalHost()
Get name of the host, proxy has assigned to make a remote connection
for this socket.
|
int |
getLocalPort()
Get the port assigned by the proxy for the socket, not
the port on locall machine as in Socket.
|
OutputStream |
getOutputStream()
Same as Socket
|
int |
getPort()
Same as Socket
|
int |
getSoLinger(int timeout)
Same as socket.
|
int |
getSoTimeout(int timeout)
Same as socket.
|
boolean |
getTcpNoDelay()
Same as socket.
|
void |
setSoLinger(boolean on,
int val)
Same as socket.
|
void |
setSoTimeout(int timeout)
Same as socket.
|
void |
setTcpNoDelay(boolean on)
Same as socket.
|
String |
toString()
Get string representation of the socket.
|
bind, connect, connect, getChannel, getKeepAlive, getLocalSocketAddress, getOOBInline, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTrafficClass, isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setTrafficClass, shutdownInput, shutdownOutputprotected Proxy proxy
protected String localHost
protected String remoteHost
protected InetAddress localIP
protected InetAddress remoteIP
protected int localPort
protected int remotePort
public SocksSocket(String host, int port) throws SocksException, UnknownHostException
host - Machine to connect to.port - Port to which to connect.SocksExceptionUnknownHostExceptionSocksSocket(Proxy,String,int),
Socks5Proxy.resolveAddrLocallypublic SocksSocket(Proxy p, String host, int port) throws SocksException, UnknownHostException
p - Proxy to use.host - Machine to connect to.port - Port to which to connect.UnknownHostException - If one of the following happens:
SocksException - If one of the following happens:
IOexception - if anything is wrong with I/O.Socks5Proxy.resolveAddrLocallypublic SocksSocket(InetAddress ip, int port) throws SocksException
ip - Machine to connect to.port - Port to which to connect.SocksExceptionSocksSocket(Proxy,String,int)public SocksSocket(Proxy p, InetAddress ip, int port) throws SocksException
p - Proxy to use.ip - Machine to connect to.port - Port to which to connect.SocksExceptionprotected SocksSocket(String host, int port, Proxy proxy)
protected SocksSocket(InetAddress ip, int port, Proxy proxy)
public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class SocketIOExceptionpublic InputStream getInputStream()
getInputStream in class Socketpublic OutputStream getOutputStream()
getOutputStream in class Socketpublic String getHost()
public InetAddress getInetAddress()
getInetAddress in class Socketpublic int getLocalPort()
getLocalPort in class Socketpublic InetAddress getLocalAddress()
getLocalAddress in class Socketpublic String getLocalHost()
public void setSoLinger(boolean on,
int val)
throws SocketException
setSoLinger in class SocketSocketExceptionpublic int getSoLinger(int timeout)
throws SocketException
SocketExceptionpublic void setSoTimeout(int timeout)
throws SocketException
setSoTimeout in class SocketSocketExceptionpublic int getSoTimeout(int timeout)
throws SocketException
SocketExceptionpublic void setTcpNoDelay(boolean on)
throws SocketException
setTcpNoDelay in class SocketSocketExceptionpublic boolean getTcpNoDelay()
throws SocketException
getTcpNoDelay in class SocketSocketExceptionCopyright © 2025. All rights reserved.