5

Why does my SOCKS proxy code throw SocketException: Malformed reply from SOCKS server? I've tried to set in URLConnection or other, but this doesn't work. Only thing that worked - chilkat lib, but it's commercial. So, how I, for example, make http request through a ASOCKS proxy? Maybe exist some free lib for that?

For example, that code:

    SocketAddress addr = new InetSocketAddress(proxy_ip, proxy_port);
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
    Socket socket = new Socket(proxy);
    InetSocketAddress dest = new InetSocketAddress("google.com", 80);
    try {
        socket.connect(dest);
    } catch (IOException ex) {
        Logger.getLogger(CheckProxy.class.getName()).log(Level.SEVERE, null, ex);
    }

Throws exception:

java.net.SocketException: Malformed reply from SOCKS server
at java.net.SocksSocketImpl.readSocksReply(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at proxychecker.CheckProxy.SocksCheck(CheckProxy.java:86)

Where line 86 is "socket.connect(dest);"

user207421
  • 305,947
  • 44
  • 307
  • 483
Pter
  • 163
  • 1
  • 2
  • 7
  • There are two ways and they are both documented. – user207421 Nov 30 '14 at 15:59
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Prophet Nov 30 '14 at 16:06
  • Okay. I've used many ways to solve that. In general, i receive exception:"java.net.SocketException: Malformed reply from SOCKS server". Exception isn't depends of implementation, for e.x. i've tryed code from the last comment - http://stackoverflow.com/questions/21742103/http-call-with-socks-4-proxy . – Pter Nov 30 '14 at 16:11
  • This is now basically a duplicate of [HTTP call with Socks 4 proxy](http://stackoverflow.com/questions/21742103/http-call-with-socks-4-proxy). See also [this Java bug](http://bugs.java.com/view_bug.do?bug_id=6964547). – user207421 Dec 02 '14 at 02:02
  • @prophet that is not the case here. – Thorbjørn Ravn Andersen Jun 18 '21 at 23:26
  • It took you 7 years to recognize that? – Prophet Jun 19 '21 at 18:20

2 Answers2

6

Explanation of this bug: bugs.java.com/view_bug.do?bug_id=6964547

You need to manually change mode in Socket object using this code:

Class clazzSocks  = socket.getClass();
Method setSockVersion  = null;
Field sockImplField = null; 
SocketImpl socksimpl = null; 
try {
  sockImplField = clazzSocks.getDeclaredField("impl");
  sockImplField.setAccessible(true);
  socksimpl  = (SocketImpl) sockImplField.get(socket);
  Class clazzSocksImpl  =  socksimpl.getClass();
  setSockVersion  = clazzSocksImpl.getDeclaredMethod("setV4");
  setSockVersion.setAccessible(true);
  if(null != setSockVersion){
      setSockVersion.invoke(socksimpl);
  }
  sockImplField.set(socket, socksimpl);
} catch (Exception e) {

} 
Pter
  • 163
  • 1
  • 2
  • 7
5
SocketException: Malformed reply from SOCKS server

That indicates that you don't have a SOCKS proxy at all. Possibly it is an HTTP proxy.

And note that you asked the wrong question. Your code was correct, it was your premiss that was wrong.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    Trying different server, I have determined that it is not working only servers with SOCKS4. SOCKS5 server to function properly. Actually, how to tell java that I'm using the fourth version of the protocol? `System.setProperty("socksProxyVersion", "4");` isn't working. Thanks for help. Example of socks servers may be founded here - http://incloak.com/proxy-list/ . – Pter Dec 01 '14 at 16:07
  • 2
    And i see description of this fenomen here - http://bugs.java.com/view_bug.do?bug_id=6964547 . How enable it? – Pter Dec 01 '14 at 19:48
  • @PterPterkovich You should post that information as an answer. – user207421 Jul 30 '16 at 01:10
  • I have the same ISSUE (Java 7 & 8) I found SOCKS proxy and it works, and works ONLY when I point it as SOCKS In the IE, winows PC) ANd when I try to use in in the PROSY connection I receve same Malformed reply from SOCKS server – Andrew Niken Nov 22 '16 at 23:39
  • @EJP how to check if it's a socks4 or socks5 proxy? – user1872384 Jun 01 '17 at 10:03
  • @user1872384 Ask your netadmin? – user207421 Mar 11 '21 at 04:58