博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
向继电器发送socket请求(python+java)
阅读量:6980 次
发布时间:2019-06-27

本文共 2866 字,大约阅读时间需要 9 分钟。

近日,有一需求,向连接在内网的继电器发送socket请求,加以控制。原本并不复杂,只是io流/socket转换的问题,实操中却出现python代码没问题,java代码执行无响应的问题,问题很好定位:没有发送正确的请求指令。进而确定是编码的问题,python预设全局编码格式为utf-8,java端只需指定请求字节码为utf-8即可。

python实现: 

#! /usr/bin/env python# -*- coding:utf-8 -*-# __author__ = "NYA"import socketsoc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)address=('172.18.20.188',5002)soc.connect(address)message="on1"message="off1"message="read1"#res=soc.recv(512)#print ressoc.send(message)total=[]i=0while True:    res=soc.recv(1)    if i>8:        total.append(res)    i+=1    if str(res)=='1': break#print resthen=''.join(total)print thensoc.close()

java实现:

import java.io.*;import java.net.Socket;import java.util.regex.Pattern;public class TestSocket {    public static void main(String[] args) {        /*        * command:        *   on1 off1 read1        *   on2 off2 read2        * */        try {            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));            boolean flag = true;            while (flag) {                System.out.println("输入信息: ");                String str = input.readLine();                if ("bye".equals(str)) {                    flag = false;                } else {                    String s = sendCommand(str);                    System.out.println(s);                }            }            input.close();            System.err.println("good bye");        } catch (Exception e) {            e.printStackTrace();        }    }    public static String sendCommand(String command) {        String result;        String ip = "172.18.20.188";        int port = 5000;        Socket socket = null;        try {            socket = new Socket(ip,port);            socket.setSoTimeout(1000); // read 超时            OutputStream outputStream = socket.getOutputStream();            byte[] receive = new byte[1];            byte[] bytes = command.getBytes("UTF-8"); // 转码 ×××            outputStream.write(bytes);            InputStream inputStream = socket.getInputStream();            StringBuilder sb = new StringBuilder();            int i = 0 ;            while (true) {                inputStream.read(receive);                String now = new String(receive);                if (i > 8) sb.append(now);                if (i > 10) {                    if (isInteger(now)) break;                }                i++;            }            result =  sb.toString();        } catch (Exception e) {            //e.printStackTrace();            result="err";        }        // 释放socket连接        try {            socket.close();        } catch (IOException e) {            e.printStackTrace();        }        return result;    }    public static boolean isInteger(String str) {        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");        return pattern.matcher(str).matches();    }}

转载于:https://www.cnblogs.com/nyatom/p/10212872.html

你可能感兴趣的文章
python 使用PyTesser--安装
查看>>
MAC 上使用pem秘钥 远程登录linux
查看>>
无需编译,1分钟安装Ubuntu官方构建的最新版Linux内核
查看>>
解压即用,Ubuntu上Nginx/Apache/PHP编译打包
查看>>
详解-斗鱼弹幕API-接入(斗鱼弹幕服务器第三方接入协议)
查看>>
table设置border没有空隙
查看>>
升级 Vim 7.4 On Ubuntu 13.10, 13.04, 12.04, Linux...
查看>>
Maven的setting.xml 配置详解
查看>>
Pycharm中autopep8设置
查看>>
Python3.7源码在windows(VS2015)下的编译和安装
查看>>
在Java中如何避免“!=null”式的判空语句?
查看>>
手动编译内核
查看>>
openshift client 命令 之 groups
查看>>
vsphere web client 加载不上本地镜像提示加载插件超时
查看>>
db2 之 入门实验
查看>>
开始Jquery的学习生涯
查看>>
手机测试项目时报INSTALL_FAILED_INSUFFICIENT_STORAGE
查看>>
10_css选择符类型1.html
查看>>
修改 liteide 的 godoc 文档样式
查看>>
阿里镜像
查看>>