zoukankan      html  css  js  c++  java
  • java中tcp小样例

    服务端:

    ServerSocket service = new ServerSocket(7777);
    		Socket socket = service.accept();
    		InputStream input = socket.getInputStream();
    		OutputStream out = socket.getOutputStream();
    		BufferedInputStream bufinput = new BufferedInputStream(input);
    		BufferedOutputStream bufout = new BufferedOutputStream(out);
    		byte [] buf = new byte[1024];
    		int len = 0;
    		
    		StringBuffer sb = new StringBuffer();
            String message = new String(buf,0,bufinput.read(buf));
            System.out.println(message);
            
            out.write("服务端:
    小客你好啊!".getBytes());

    client:

    Socket client = new Socket("127.0.0.1",7777);
            OutputStream out = client.getOutputStream();
            InputStream input = client.getInputStream();
            BufferedOutputStream bufout = new BufferedOutputStream(out);
            BufferedInputStream bufinput = new BufferedInputStream(input);
            bufout.write("客户端:
    你好".getBytes());
            bufout.flush();
            byte [] buf = new byte [1024];
            String message = new String(buf,0,bufinput.read(buf));
            System.out.println(message);


  • 相关阅读:
    团队冲刺八
    第十一周学习进度
    团队冲刺七
    团队冲刺六
    团队冲刺五
    冲刺第五天
    冲刺第四天
    冲刺第三天
    冲刺第二天
    冲刺第一天
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7103178.html
Copyright © 2011-2022 走看看