服务端:
public static void startSocket() { Socket socket = null; BufferedReader br = null; PrintWriter pw = null; try { ServerSocket server = new ServerSocket(9090); while (true) { socket = server.accept(); // System.out.println("客户端信息:" + socket.getLocalSocketAddress()); InputStream in = socket.getInputStream(); InputStreamReader is = new InputStreamReader(in); br = new BufferedReader(is); String info = br.readLine(); System.out.println("接收客户端的信息" + info); OutputStream out = socket.getOutputStream(); pw = new PrintWriter(out); pw.print("返回数据给客户端"); pw.flush(); // 关闭 in.close(); is.close(); br.close(); out.close(); pw.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { // pw.close(); // br.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
客户端:
for (int i = 0; i <2; i++) { Socket socket = null; PrintWriter pw = null; BufferedReader br = null; try { socket = new Socket("123.57.150.181", 9090); OutputStream out = socket.getOutputStream(); // System.out.println("??????????" + socket.getRemoteSocketAddress()); pw = new PrintWriter(out); // pw.println("funName=Tick&DeviceId=460040017107640" + i); // pw.println("funName=deviceRegister&IMSI=46002012731449"); pw.println("funName=pathHistory&IMSI=460040017107470&sendingTime=2010-08-24T10:40:40&positions=113.887972,22.717676/&speed=-52446704/+618475291/"); pw.flush(); InputStream in = socket.getInputStream(); br = new BufferedReader(new InputStreamReader(in)); String info = br.readLine(); System.out.println(info); in.close(); out.close(); br.close(); pw.close(); socket.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { pw.close(); socket.close(); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
时间: 2024-10-09 18:27:31