0
votes

I'm receiving socket errors in Java, it connects to the socket each time with a different proxy and it returns an error, I checked the proxies so they're not dead. I'm not sure what is causing this.

Here is the main functions.

public static Socket proxiedSocket(String[] con, String[] Prox)
{
    int port = Integer.parseInt(Prox[1]);
    int chatport = Integer.parseInt(con[1]);
    InetSocketAddress SOCKS = new InetSocketAddress(Prox[0], port);
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, SOCKS);
    Socket socket = new Socket(proxy);
    InetSocketAddress inet = new InetSocketAddress(con[0], chatport);
    try
    {
        socket.connect(inet, 7000); // 5 Second Timeout
    }
    catch(SocketTimeoutException e)
    {
        log("Could not connect connect to socket! Reason: timed out!");
        try
        {
            socket.close();
        }
        catch(Exception ee) { log("Could not close socket!"); }
    }
    catch(IOException e)
    {
        log(e.getMessage());
        return socket;
    }
    return socket;
}

public static void send(Socket socket, String data)
{
    try
    {
        DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());

        // Send first message
        dOut.writeByte(1);
        dOut.writeUTF(data);
        dOut.flush(); // Send off the data

        // Send the exit message
        dOut.writeByte(EOF);
        dOut.flush();
        dOut.close();
    }
    catch (Exception e)
    {
        log("Error sending data to server: " + e.getMessage());
    }
}

public static String read(Socket socket)
{
    try
    {
        int b;
        ByteArrayOutputStream ba = new ByteArrayOutputStream(200);
        InputStream reader = socket.getInputStream();
        String packet = "";
        while((b = reader.read()) > 0)
        {
            ba.write(b);
        }
        if(b == -1)
        {
            ba.close();
            return "False";
        }
        packet = ba.toString("UTF-8");
        ba.flush();
        ba.close();
        return packet;
    }
    catch(Exception e)
    {
        log("Error while reading from server: " + e.getMessage());
        return "False"; 
    }
}

This is multi threading by the way.

so after each thread calls this: Socket socket = core.proxiedSocket(this.chatcon, this.proxy);

These are the errors I get.


Error while reading from server: Socket is closed

Malformed reply from SOCKS server

Connection refused: connect

Error sending data to server: Socket is not connected

Error while reading from server: Socket is not connected

Connection reset

connect timed out

Thanks in advance, if something is unclear please say so in the comments!

1

1 Answers

0
votes

Socket is closed

You closed the socket and continued to use it.

Malformed reply from SOCKS server

You connected to something that probably isn't a SOCKS server.

Connection refused

There is nothing listening at the target IP:port

socket is not connected

You didn't connect the socket.

connection reset

There are a lot of causes of this but the most common is sending data to a connection that had already been closed by the peer. In other words, an application protocol error.

connect timed out

Probably a firewall is preventing you from connecting to the target.

The reason for several of these errors is that you are swallowing exceptions and returning the socket anyway. Don't write code like this. Let the caller deal with the exceptions. Don't return unusable sockets.