flush on socks connection

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/utils/SocksSocketFactory.java | 14 
1 file changed, 8 insertions(+), 6 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/utils/SocksSocketFactory.java 🔗

@@ -14,23 +14,25 @@ public class SocksSocketFactory {
 
 	private static final byte[] LOCALHOST = new byte[]{127,0,0,1};
 
-	public static void createSocksConnection(Socket socket, String destination, int port) throws IOException {
+	public static void createSocksConnection(final Socket socket, final String destination, final int port) throws IOException {
 		final InputStream proxyIs = socket.getInputStream();
 		final OutputStream proxyOs = socket.getOutputStream();
 		proxyOs.write(new byte[]{0x05, 0x01, 0x00});
-		byte[] response = new byte[2];
-		proxyIs.read(response);
-		if (response[0] != 0x05 || response[1] != 0x00) {
+		proxyOs.flush();
+		final byte[] handshake = new byte[2];
+		proxyIs.read(handshake);
+		if (handshake[0] != 0x05 || handshake[1] != 0x00) {
 			throw new SocksConnectionException("Socks 5 handshake failed");
 		}
-		byte[] dest = destination.getBytes();
+		final byte[] dest = destination.getBytes();
 		final ByteBuffer request = ByteBuffer.allocate(7 + dest.length);
 		request.put(new byte[]{0x05, 0x01, 0x00, 0x03});
 		request.put((byte) dest.length);
 		request.put(dest);
 		request.putShort((short) port);
 		proxyOs.write(request.array());
-		response = new byte[7 + dest.length];
+		proxyOs.flush();
+		final byte[] response = new byte[7 + dest.length];
 		proxyIs.read(response);
 		if (response[1] != 0x00) {
 			if (response[1] == 0x04) {