如题,我想用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));
}
解决方案 »
- 如何得到字符串并且放到arraylist<string>中
- 求助各位高手,小弟先行谢过
- 为什么无法调用类,运行提示"Syntax error on token 'new'"
- 一个关于new的问题
- 用FileReader和FileWriter 与 用FileOutputStream和FileInputStream 来完成文件之间的copy有什么区别么??
- 关于dos下引入jar包问题
- 救命!产生84个数,其中有4个0,4个1,4个2~~~~~~,4个20,顺序是随即的
- windows资源管理器中的文件(JAVA生成)的连续打印问题?
- 求一个数据库连接池,高分相送
- 用java来实现某一段IP上的ftp搜索,该怎样下手做?3X!
- Swing JTextArea 必填项验证是否为空的问题
- 有趣的Java题目!!!
return null;下面还System.out.println();.....你要打印什么
程序执行只出现:<Command ID=ROUTER_LINK_STATUS Paras=ACK@@0>
void connect(SocketAddress endpoint)
将此套接字连接到服务器。
这个值是多少,不会是没接全吧