• 网络编程 编程聊天小程序


    第一

    package zdian;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.util.Scanner;
    public class ServerTest {
     public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      try {
       Socket c = new Socket("192.168.0.116",5928);
       System.out.print("请输入:");
       String name =sc.nextLine();
       OutputStream os = c.getOutputStream();
       String info = String.format("我是:%s, 我的IP是:%s. ",name,InetAddress.getLocalHost());
      // InputStream is =  c.getInputStream();
       os.write(info.getBytes("utf-8"));
       InputStream is =c.getInputStream();
       byte[] buf =new byte[1024];
       is.read(buf);
       System.out.println(new String(buf,"utf-8"));
       os.flush();
       is.close();
       os.close();
      } catch (IOException e) {
       
       e.printStackTrace();
     }
     }
    }
    第2
    package zdian;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class Socker {
     public static void main(String[] args) {
      ServerSocket s;
      try {
       s = new ServerSocket(5928);
       Socket server = s.accept();
       //System.out.println("有人访问.");
       InputStream is =server.getInputStream();
       byte[] buf =new byte[1024];
       is.read(buf);
       System.out.println(new String(buf,"utf-8"));
       
       OutputStream os = server.getOutputStream();
       os.write("欢迎你! ".getBytes("utf-8"));
       os.flush();
       is.close();
       os.close();
      // os.close();
      } catch (IOException e) {
       
       e.printStackTrace();
      }
      
     }
    }
  • 相关阅读:
    luogu P2852 [USACO06DEC]Milk Patterns G
    FZOJ 4267 树上统计
    CF1303G Sum of Prefix Sums
    luogu P5311 [Ynoi2011]成都七中
    luogu P5306 [COCI2019] Transport
    SP34096 DIVCNTK
    luogu P5325 【模板】Min_25筛
    luogu P1742 最小圆覆盖
    求两直线交点坐标
    1098: 复合函数求值(函数专题)
  • 原文地址:https://www.cnblogs.com/wangqianbao/p/13144444.html
Copyright © 2020-2023  润新知