如题,我想用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));
}

解决方案 »

  1.   

    差不多啊,问题是什么?为什么你返回null?  
    return null;下面还System.out.println();.....你要打印什么
      

  2.   

    应该出现这个:<Command ID=ROUTER_LINK_STATUS Paras=ACK@@0><Command ID=CROSS_POINT Paras=0_0_0@@0>
    程序执行只出现:<Command ID=ROUTER_LINK_STATUS Paras=ACK@@0>
      

  3.   

    Socket
    void connect(SocketAddress endpoint) 
    将此套接字连接到服务器。 
      

  4.   

    假如你的服务器没问题,就是你传入的参数错了。对比下两次操作的字节码吧,你确定是用content.getBytes("utf-8")这个字节串?
      

  5.   

    。。你看看 int i= in.read(data);
    这个值是多少,不会是没接全吧