1.这几天公司来了一个无线通信项目表示以前没做过啊 查阅了网上很多资料大致分为两种情况,下面
代码是其中一种的 也是百试不同,首先我们先确保设备是完好的 ,用网络调试助手先测一下。
发送框内一串命令发出后 可以收到设备给返回的另一串返回值,这说明设备是没有问题的。那么我们进入到程序中。
我们先把输入值写死,以下为代码
public class TestDemo3 {
public static void main(String[] args) throws UnknownHostException, IOException {
try {
Socket socket;
socket = new Socket("192.168.1.7",23);
System.out.println("连接成功!");
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
String sendInfo="01 03 0451 0001 D4 EB";
pw.write(sendInfo);
System.out.println("发送成功!");
pw.flush();
socket.shutdownOutput();
String replyInfo=null;
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());
while(!((replyInfo=br.readLine())==null)){
System.out.println("接收服务器的数据信息:"+replyInfo);
}
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这基本也是网上抄的其中一种,返回的值却为null 这说明没有接收到端口返回的值或者发送的值错误。有很多不解还请各位网络通信大神指点一二。