zoukankan      html  css  js  c++  java
  • 网络编程 编程聊天小程序

    第一

    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();
      }
      
     }
    }
  • 相关阅读:
    linux redis安装 5.0.2
    Linux nginx安装步骤 centos7
    fastjson JSONObject.toJSONString 出现 $ref: "$."的解决办法(重复引用)
    docker redis安装及配置(外网访问 关闭安全限制 设置密码)
    JDK dump
    mysql8 修改root密码
    docker系列详解<二>之常用命令
    获取地理位置
    js调用摄像头
    点击时扩散效果
  • 原文地址:https://www.cnblogs.com/wangqianbao/p/13144444.html
Copyright © 2011-2022 走看看