socket编程小范例

来源:互联网 发布:sed linux 编辑:程序博客网 时间:2024/05/18 00:10


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();
}


}


}
0 0