标签:资源 包装 shu read cep sock device 输入输出 关闭
package com.ra.car.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON; public class JavaTCPClient { protected static final Logger logger = LoggerFactory .getLogger(JavaTCPClient.class); public static void main(String[] args) { try {/* // 创建Socket对象 Socket socket = new Socket("a.mintaian.com", 8903); logger.info("11111111111111"); // 根据输入输出流和服务端连接 OutputStream outputStream = socket.getOutputStream();// 获取一个输出流,向服务端发送信息 logger.info("2222222222222222222"); PrintWriter printWriter = new PrintWriter(outputStream);// 将输出流包装成打印流 logger.info("3333333333333333333"); printWriter.print("服务端你好,我是客户1,这是我的第"+i+"次请求"); logger.info("444444444444444"); printWriter.flush(); socket.shutdownOutput();// 关闭输出流 logger.info("55555555555"); InputStream inputStream = socket.getInputStream();// 获取一个输入流,接收服务端的信息 logger.info("666666666666666666"); InputStreamReader inputStreamReader = new InputStreamReader( inputStream);// 包装成字符流,提高效率 logger.info("777777777777777777"); BufferedReader bufferedReader = new BufferedReader( inputStreamReader);// 缓冲区 logger.info("888888888888888888888"); String info = ""; String temp = null;// 临时变量 while ( (temp=bufferedReader.readLine())!= null) { logger.info("9999999999999"); info += temp; logger.info("客户端接收服务端发送信息:" + info+i); } logger.info("这是我的第"+i+"次调用!"); // 关闭相对应的资源 bufferedReader.close(); inputStream.close(); printWriter.close(); outputStream.close(); socket.close(); */ Socket socket = new Socket("a.mintaian.com", 8903); OutputStream os = socket.getOutputStream(); PrintWriter printWriter = new PrintWriter(os); Map<String, Object> map=new HashMap<String, Object>(); Map<String, Object> m=new HashMap<String, Object>(); Map<String, Object> m1=new HashMap<String, Object>(); m1.put("subAlarmName", "左边虚线"); String[] str=new String[]{"1","2","3"}; m1.put("adasRes", str); m.put("imei", "862465030008629"); m.put("deviceData", m1); map.put("id", "862465030008629-1509354778289"); map.put("dataPackage", m); map.put("isAdas", "1"); String msg = JSON.toJSONString(map)+"\n"; printWriter.write(msg); printWriter.flush(); // socket.shutdownOutput(); InputStream is = socket.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader( is); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); logger.info("发送的消息是:" + msg); msg = null; while ((msg = bufferedReader.readLine()) != null) { logger.info("msg:" + msg); } logger.info("999999999999999999999999"); // 关闭相对应的资源 // bufferedReader.close(); // is.close(); // printWriter.close(); // os.close(); //socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
标签:资源 包装 shu read cep sock device 输入输出 关闭
原文地址:http://www.cnblogs.com/lazyInsects/p/8000137.html