socket编程小范例
2016-05-11 17:12
323 查看
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import com.cn.hnust.util.SocketUtil;
public class TestSocket {
public static void main(String args[]) throws IOException, InterruptedException {
Socket socket = new Socket("192.168.2.200", 2000);
try{
System.out.println("socket = " + socket);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
byte[] s=new byte[]{0x53,0x35,0x10,0x01,0x03,0x05,0x03,0x08,0x01,(byte) 0xFF,0x00,0x08,0x00,0x02,(byte) 0xff,0x02};
out.write(s);
byte[] b= new byte[20];
in.read(b);
String data = SocketUtil.bytesToHexString(b);
System.err.println(data.substring(32, data.length()));
}finally{
System.out.println("close the Client socket and the io.");
socket.close();
}
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import com.cn.hnust.util.SocketUtil;
public class TestSocket {
public static void main(String args[]) throws IOException, InterruptedException {
Socket socket = new Socket("192.168.2.200", 2000);
try{
System.out.println("socket = " + socket);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
byte[] s=new byte[]{0x53,0x35,0x10,0x01,0x03,0x05,0x03,0x08,0x01,(byte) 0xFF,0x00,0x08,0x00,0x02,(byte) 0xff,0x02};
out.write(s);
byte[] b= new byte[20];
in.read(b);
String data = SocketUtil.bytesToHexString(b);
System.err.println(data.substring(32, data.length()));
}finally{
System.out.println("close the Client socket and the io.");
socket.close();
}
}
}
相关文章推荐
- 文件上传示例代码
- javaw.exe是什么?两个class文件可以同事运行
- python文件处理
- Java 实现ES批量索引
- eclipse 闪退
- java性能优化之二 循环里面不使用hibernate创建对象
- phpcms 修改域名
- 关于django migrations的使用
- Java中的vector类使用示例小结
- struts2与spring集成时,关于class属性及成员bean自动注入的问题
- 关于C++全局变量声明的问题
- 1.python基础学习__函数中的参数
- Java ArrayList toArray() 方法的正确使用
- spring mybatis 动态代理
- django的admin后台注册model并显示
- c++第五次上机实验——教师干部类
- hdu1847——Good Luck in CET-4 Everybody!(巴什博奕)
- C++实现双链表
- C++实现双链表
- C++实现双链表