NutzCN Logo
问答 nutz Socket工具类,连不上啊
发布于 2916天前 作者 qq_16dd5808 3002 次浏览 复制 上一个帖子 下一个帖子
标签: sockets
System.out.println(Sockets.sendText("192.168.11.109", 19006, StringKit.bytes2hexString(bytes)));

这个地址和端口是肯定能链接的。但是使用工具类报错


java.lang.RuntimeException: java.net.UnknownHostException: 192.168.11.109 at org.nutz.lang.Lang.wrapThrow(Lang.java:180) at org.nutz.lang.socket.Sockets.send(Sockets.java:60) at org.nutz.lang.socket.Sockets.send(Sockets.java:28) at org.nutz.lang.socket.Sockets.sendText(Sockets.java:82) at test.sunyu.kit.common.TestOther.t2(TestOther.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.net.UnknownHostException: 192.168.11.109 at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at org.nutz.lang.socket.Sockets.send(Sockets.java:47) ... 25 more
18 回复

语句是这的

        Socket socket = null;
        try {
            socket = new Socket();
            socket.connect(InetSocketAddress.createUnresolved(host, port), timeout);
            ....
         }

要不你这样连,看看会不会报错

Socket socket = new Socket("192.168.11.109", 19006);

Socket socket = new Socket("192.168.11.109", 19006);

这样是没错的

System.out.println(Sockets.sendText("192.168.11.109", 19006, StringKit.bytes2hexString(bytes)));

这样就不行。报错

执行这个语句

Socket socket = new Socket("192.168.11.109", 19006);

会打印

[DEBUG][2016-12-28 16:14:47.586]test.sunyu.kit.common.TestOther.ttt(TestOther.java:62)Socket[addr=/192.168.11.109,port=19006,localport=52821]

没有错误

恩,我提交一下

ok,我过一会去下载快照试试看

<dependency>
            <groupId>org.nutz</groupId>
            <artifactId>nutz</artifactId>
            <version>1.r.60-SNAPSHOT</version>
        </dependency>
<repository>
            <id>nutz-ossrh</id>
            <url>http://maven.nutz.cn/nexus/content/repositories/ossrh</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>nexus-nutzcn-thirdparty</id>
            <name>Nexus nutzcn thirdparty</name>
            <url>http://maven.nutz.cn/nexus/content/repositories/thirdparty</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </repository>

是这样配置么?怎么下载不了了

// 接收服务器的反馈
            if (!socket.isClosed()) {
                InputStream sReturn = socket.getInputStream();
                Streams.write(ops, sReturn);
            }

执行到

 Streams.write(ops, sReturn);

这句话,就一直卡住了,不动弹了。也不报错。
socket服务肯定是没问题的,因为老代码,原生的是可以跑通的,并且能接收到返回值

System.out.println(Sockets.sendText("192.168.11.109", 19006, StringKit.bytes2hexString(bytes)));

不行,上面这句话还是一直卡死,也不报错。

用下面这个测试方法是可以的

@Test
    public void ttt() throws IOException {
        byte[] data = ByteKit.hexString2bytes("232301FE686F6D657231323334353637383930313301001E100C1C0A1A0D828938393836303242343033313633303030383036350000FF");
        Socket socket = new Socket("192.168.11.109", 19006);
        logger.debug("{}", socket);
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        out.write(data);
        int totalBytesRcvd = 0;
        int bytesRcvd;
        while (totalBytesRcvd < data.length) {
            if ((bytesRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1)
                throw new SocketException("Connection closed prematurely");
            totalBytesRcvd += bytesRcvd;
        }
        logger.debug("{}", data);
        socket.close();
    }

会打印

[DEBUG][2016-12-28 16:45:35.057]test.sunyu.kit.common.TestOther.ttt(TestOther.java:66)Socket[addr=/192.168.11.109,port=19006,localport=53691]
[DEBUG][2016-12-28 16:45:35.065]test.sunyu.kit.common.TestOther.ttt(TestOther.java:77)[35, 35, 1, 1, 104, 111, 109, 101, 114, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 51, 1, 0, 30, 16, 12, 28, 10, 26, 13, -126, -119, 56, 57, 56, 54, 48, 50, 66, 52, 48, 51, 49, 54, 51, 48, 48, 48, 56, 48, 54, 53, 0, 0, 0]

@qq_16dd5808 debug一下看看到哪局了

跟踪断点,在

// 接收服务器的反馈
            if (!socket.isClosed()) {
                InputStream sReturn = socket.getInputStream();
                Streams.write(ops, sReturn);//执行到这里
            }

再跟进去,执行到Streams.java

public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException {
        if (null == ops || null == ins)
            return 0;

        byte[] buf = new byte[bufferSize];
        int len;
        long bytesCount = 0;
        while (-1 != (len = ins.read(buf))) {//执行到这里,就一直卡住了。不动弹了,也不报错
            bytesCount += len;
            ops.write(buf, 0, len);
        }

稍等。我再跟一下

唉,截了图片,不过这里没发上传。我把图片粘贴进来,我的头像居然变成图片了。。。。

 public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException {
        if (null == ops || null == ins)
            return 0;

        byte[] buf = new byte[bufferSize];
        int len;
        long bytesCount = 0;
        while (-1 != (len = ins.read(buf))) {//首先断点到这里了,第一次和第二次可以执行下去,第三次就没反应了
            bytesCount += len;
            ops.write(buf, 0, len);
        }
        // 啥都没写,强制触发一下写
        // 这是考虑到 walnut 的输出流实现,比如你写一个空文件
        // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了
        // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动
        // 所以这里触发一个写,它就知道,喔你要写个空喔。
        if (0 == bytesCount) {//第一次和第二次可以走到这里
            ops.write(buf, 0, 0);
        }
        ops.flush();
        return bytesCount;
    }

@qq_16dd5808 那是一个echo服务器?发什么回什么?

按你的代码看,是读取到指定的长度就结束

而Sockets是读取到流结束才退出

那边是个socket服务器,接收车机数据,然后返回应答。

这是返回的内容

[35, 35, 1, 1, 104, 111, 109, 101, 114, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 51, 1, 0, 30, 16, 12, 28, 10, 26, 13, -126, -119, 56, 57, 56, 54, 48, 50, 66, 52, 48, 51, 49, 54, 51, 48, 48, 48, 56, 48, 54, 53, 0, 0, 0]

发送的是

 byte[] data = ByteKit.hexString2bytes("232301FE686F6D657231323334353637383930313301001E100C1C0A1A0D828938393836303242343033313633303030383036350000FF");

响应完, 并不会主动连接??? 所以Sockets一直在等待?

添加回复
请先登陆
回到顶部