import java.net.Socket;
import java.net.SocketException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TCPClient {
public static void main(String[] args) throws IOException{
String server = "202.105.21.214"; // 服务器IP
// 发送的数据
byte data[] ={0x3F,0x40,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0X00,0x00,0X00,0X00,0x05,0x24,0x78};
byte[] recData=new byte[100];//接收数据缓冲
int servPort = 16445;//端口
Socket socket = new Socket(server, servPort);
System.out.println("Connected to server...sending command string");
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(data); // 发送
// 接收数据
int totalBytesRcvd = 0; // Total bytes received so far
totalBytesRcvd=in.read(recData);//接收
String hexString="";
for(int i=0;i<totalBytesRcvd;i++)
hexString+= Integer.toHexString(0xff&recData[i])+",";//转成十六进制显示
System.out.println("Received: " + hexString);
socket.close(); // Close the socket and its streams
}
}
java tcp ip 接口 tcpclient java
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章