gpt4 book ai didi

java - SocketImpl Setoption 中的套接字关闭异常

转载 作者:行者123 更新时间:2023-12-02 13:29:18 25 4
gpt4 key购买 nike

我正在尝试设置套接字的SO_KEEPALIVE时间。

我创建了一个类 SocketBuilder 来使用 SocketImpl 构建套接字实例。源代码如下,

public class SocketBuilder {

private static SocketImpl si;

public static Socket createCVPSocket() throws Exception {
if (si == null) {
init();
}
return new CSocket(si);
}


private static void init() throws SocketException {
@SuppressWarnings("rawtypes")
Constructor cons = null;
try {
cons = Class.forName("java.net.SocksSocketImpl")
.getDeclaredConstructor();
} catch (NoSuchMethodException | SecurityException
| ClassNotFoundException e) {
throw new RuntimeException(
"Not able to access socket implementation.");
}
cons.setAccessible(true);
SocketImpl si = null;
try {
si = (SocketImpl) cons.newInstance();
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException("Not able to create instance of socket.");
}
if (si != null) {
si.setOption(SocketImpl.SO_KEEPALIVE, new Integer(60));
}
}

private static class CSocket extends Socket {
protected CSocket(SocketImpl si) throws SocketException, Exception {
super(si);
}
}
public static void main(String[] args) {
try {
Socket sock = SocketBuilder.createCVPSocket();
System.out.println(sock);
} catch (Exception e) {
e.printStackTrace();
}
}
}

我收到 java.net.SocketException: Socket Closed 异常。如果我删除行 si.setOption(SocketImpl.SO_KEEPALIVE, new Integer(60)); 则它可以正常工作。但我想设置 SocketImpl.SO_KEEPALIVE 。如何设置套接字的SO_KEEPALIVE

最佳答案

您的代码中存在一些错误:

  1. SocketImpl si = null; 此声明与您的类字段重叠

  2. setOption 仅在套接字打开/连接时有效

  3. 完成后必须关闭套接字

    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    import java.net.*;

    public class SocketBuilder {
    private static SocketImpl si;

    public static Socket createCVPSocket() throws Exception {
    if (si == null) {
    init();
    }
    return new CSocket(si);
    }


    private static void init() throws SocketException {
    @SuppressWarnings("rawtypes")
    Constructor cons = null;
    try {
    cons = Class.forName("java.net.SocksSocketImpl")
    .getDeclaredConstructor();
    } catch (NoSuchMethodException | SecurityException
    | ClassNotFoundException e) {
    throw new RuntimeException(
    "Not able to access socket implementation.");
    }
    cons.setAccessible(true);
    si = null;
    try {
    si = (SocketImpl) cons.newInstance();
    } catch (InstantiationException | IllegalAccessException
    | IllegalArgumentException | InvocationTargetException e) {
    throw new RuntimeException("Not able to create instance of socket.");
    }

    }

    private static class CSocket extends Socket {
    protected CSocket(SocketImpl si) throws SocketException, Exception {
    super(si);
    super.bind(new InetSocketAddress("127.0.0.1", 8888));

    si.setOption(SocketImpl.SO_KEEPALIVE, Boolean.TRUE);
    }
    }
    public static void main(String[] args) {
    try {
    Socket sock = SocketBuilder.createCVPSocket();
    System.out.println(sock);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }

关于java - SocketImpl Setoption 中的套接字关闭异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43245532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com