简体   繁体   English

接收Java中的套接字错误

[英]Receiving Socket Errors in Java

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. 我收到了Java中的套接字错误,它每次都使用不同的代理连接到套接字,并且返回错误,我检查了代理以确保它们没有失效。 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); 因此,在每个线程调用此代码之后: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 来自SOCKS服务器的格式错误的回复

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! 在此先感谢,如果不清楚,请在评论中说出来!

Socket is closed 插座已关闭

You closed the socket and continued to use it. 您关闭了插座并继续使用它。

Malformed reply from SOCKS server 来自SOCKS服务器的格式错误的回复

You connected to something that probably isn't a SOCKS server. 您已连接到可能不是SOCKS服务器的设备。

Connection refused 拒绝连接

There is nothing listening at the target IP:port 目标IP:端口没有监听

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. 不要返回不可用的套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM