如题,我想用java来实现 向服务器发送指令并得到响应的数据。服务器已配置好,并使用超级终端测试通过。
程序该如何实现我的实现方式:
private static Socket socket; public static String sendMes(String content, String host, int port) {
try {
byte []data=content.getBytes("utf-8");
socket = new Socket(host, port);
OutputStream out = socket.getOutputStream();
out.write(data);
out.flush();
socket.shutdownOutput();
InputStream in=socket.getInputStream();
data=new byte[1024];
int i= in.read(data);
in.close();
socket.close();
String s=new String(data,0,i,"utf-8");
return s;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return null; }
public static void main(String[] args) {
String content = "<Command ID=CROSS_POINT Paras=0@@0>";
String host = "192.168.0.196";
int port = 2552;
System.out.println(sendMes(content,host,port));
}
程序该如何实现我的实现方式:
private static Socket socket; public static String sendMes(String content, String host, int port) {
try {
byte []data=content.getBytes("utf-8");
socket = new Socket(host, port);
OutputStream out = socket.getOutputStream();
out.write(data);
out.flush();
socket.shutdownOutput();
InputStream in=socket.getInputStream();
data=new byte[1024];
int i= in.read(data);
in.close();
socket.close();
String s=new String(data,0,i,"utf-8");
return s;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return null; }
public static void main(String[] args) {
String content = "<Command ID=CROSS_POINT Paras=0@@0>";
String host = "192.168.0.196";
int port = 2552;
System.out.println(sendMes(content,host,port));
}
return null;下面还System.out.println();.....你要打印什么
程序执行只出现:<Command ID=ROUTER_LINK_STATUS Paras=ACK@@0>
void connect(SocketAddress endpoint)
将此套接字连接到服务器。
这个值是多少,不会是没接全吧