socket客户端工具类
程序员文章站
2022-07-12 16:38:34
...
public String ConnectScoket(String host,int port,String xmlString ) throws IOException{ Socket socket = new Socket(host, port); socket.setSoTimeout(10000); //流 输出流字符流 OutputStream outs=socket.getOutputStream(); System.out.println("发送的数据="+xmlString); outs.write(xmlString.getBytes("utf-8")); outs.flush(); //接收服务器返回数据 InputStream ins=socket.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(ins)); String s=reader.readLine(); System.out.println("接到服务器回复:"+s); socket.close(); return s; }