socks
Class SocksSocket

java.lang.Object
  |
  +--java.net.Socket
        |
        +--socks.SocksSocket

public class SocksSocket
extends java.net.Socket

SocksSocket tryies to look very similar to normal Socket, while allowing connections through the SOCKS4 or 5 proxy. To use this class you will have to identify proxy you need to use, Proxy class allows you to set default proxy, which will be used by all Socks aware sockets. You can also create either Socks4Proxy or Socks5Proxy, and use them by passing to the appropriate constructors.

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.


Constructor Summary
SocksSocket(java.net.InetAddress ip, int port)
          Tryies to connect to given ip and port using default proxy.
SocksSocket(Proxy p, java.net.InetAddress ip, int port)
          Connects to given ip and port using given Proxy server.
SocksSocket(Proxy p, java.lang.String host, int port)
          Connects to host port using given proxy server.
SocksSocket(java.lang.String host, int port)
          Tryies to connect to given host and port using default proxy.
 
Method Summary
 void close()
          Same as Socket
 java.lang.String getHost()
          Returns remote host name, it is usefull in cases when addresses are resolved by proxy, and we can't create InetAddress object.
 java.net.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
 java.io.InputStream getInputStream()
          Same as Socket
 java.net.InetAddress getLocalAddress()
          Get address assigned by proxy to make a remote connection, it might be different from the host specified for the proxy.
 java.lang.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.
 java.io.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.
 java.lang.String toString()
          Get string representation of the socket.
 
Methods inherited from class java.net.Socket
getReceiveBufferSize, getSendBufferSize, getSoLinger, getSoTimeout, setReceiveBufferSize, setSendBufferSize, setSocketImplFactory
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SocksSocket

public SocksSocket(java.lang.String host,
                   int port)
            throws SocksException,
                   java.net.UnknownHostException
Tryies to connect to given host and port using default proxy. If no default proxy speciefied it throws SocksException with error code SOCKS_NO_PROXY.
Parameters:
host - Machine to connect to.
port - Port to which to connect.
See Also:
SocksSocket(Proxy,String,int), Socks5Proxy.resolveAddrLocally(boolean)

SocksSocket

public SocksSocket(Proxy p,
                   java.lang.String host,
                   int port)
            throws SocksException,
                   java.net.UnknownHostException
Connects to host port using given proxy server.
Parameters:
p - Proxy to use.
host - Machine to connect to.
port - Port to which to connect.
Throws:
java.net.UnknownHostException - If one of the following happens:
  1. Proxy settings say that address should be resolved locally, but this fails.
  2. Proxy settings say that the host should be contacted directly but host name can't be resolved.
SocksException - If one of the following happens:
  • Proxy is is null.
  • Proxy settings say that the host should be contacted directly but this fails.
  • Socks Server can't be contacted.
  • Authentication fails.
  • Connection is not allowed by the SOCKS proxy.
  • SOCKS proxy can't establish the connection.
  • Any IO error occured.
  • Any protocol error occured.
IOexception - if anything is wrong with I/O.
See Also:
Socks5Proxy.resolveAddrLocally(boolean)

SocksSocket

public SocksSocket(java.net.InetAddress ip,
                   int port)
            throws SocksException
Tryies to connect to given ip and port using default proxy. If no default proxy speciefied it throws SocksException with error code SOCKS_NO_PROXY.
Parameters:
ip - Machine to connect to.
port - Port to which to connect.
See Also:
SocksSocket(Proxy,String,int)

SocksSocket

public SocksSocket(Proxy p,
                   java.net.InetAddress ip,
                   int port)
            throws SocksException
Connects to given ip and port using given Proxy server.
Parameters:
p - Proxy to use.
ip - Machine to connect to.
port - Port to which to connect.
Method Detail

close

public void close()
           throws java.io.IOException
Same as Socket
Overrides:
close in class java.net.Socket

getInputStream

public java.io.InputStream getInputStream()
Same as Socket
Overrides:
getInputStream in class java.net.Socket

getOutputStream

public java.io.OutputStream getOutputStream()
Same as Socket
Overrides:
getOutputStream in class java.net.Socket

getPort

public int getPort()
Same as Socket
Overrides:
getPort in class java.net.Socket

getHost

public java.lang.String getHost()
Returns remote host name, it is usefull in cases when addresses are resolved by proxy, and we can't create InetAddress object.
Returns:
The name of the host this socket is connected to.

getInetAddress

public java.net.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
Overrides:
getInetAddress in class java.net.Socket
Returns:
Ip address of the host this socket is connected to, or null if address was returned by the proxy as DOMAINNAME and can't be resolved locally.

getLocalPort

public int getLocalPort()
Get the port assigned by the proxy for the socket, not the port on locall machine as in Socket.
Overrides:
getLocalPort in class java.net.Socket
Returns:
Port of the socket used on the proxy server.

getLocalAddress

public java.net.InetAddress getLocalAddress()
Get address assigned by proxy to make a remote connection, it might be different from the host specified for the proxy. Can return null if socks server returned this address as hostname and it can't be resolved locally, use getLocalHost() then.
Overrides:
getLocalAddress in class java.net.Socket
Returns:
Address proxy is using to make a connection.

getLocalHost

public java.lang.String getLocalHost()
Get name of the host, proxy has assigned to make a remote connection for this socket. This method is usefull when proxy have returned address as hostname, and we can't resolve it on this machine.
Returns:
The name of the host proxy is using to make a connection.

setSoLinger

public void setSoLinger(boolean on,
                        int val)
                 throws java.net.SocketException
Same as socket.
Overrides:
setSoLinger in class java.net.Socket

getSoLinger

public int getSoLinger(int timeout)
                throws java.net.SocketException
Same as socket.

setSoTimeout

public void setSoTimeout(int timeout)
                  throws java.net.SocketException
Same as socket.
Overrides:
setSoTimeout in class java.net.Socket

getSoTimeout

public int getSoTimeout(int timeout)
                 throws java.net.SocketException
Same as socket.

setTcpNoDelay

public void setTcpNoDelay(boolean on)
                   throws java.net.SocketException
Same as socket.
Overrides:
setTcpNoDelay in class java.net.Socket

getTcpNoDelay

public boolean getTcpNoDelay()
                      throws java.net.SocketException
Same as socket.
Overrides:
getTcpNoDelay in class java.net.Socket

toString

public java.lang.String toString()
Get string representation of the socket.
Overrides:
toString in class java.net.Socket