• 简单的聊天时范例(客户端)


    package j2se.core.net.chat;


    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;

    /**
    * 简单的聊天室范例(服务器端)
    */
    public class Server {

    public static void main(String[] args) throws IOException {
    ServerSocket server = new ServerSocket(8888);
    while (true) {
    final Socket socket = server.accept();
    Thread t1 = new Thread() {
    @Override
    public void run() {
    DataInputStream input;
    try {
    input = new DataInputStream(socket.getInputStream());
    while (true) {
    String s = input.readUTF();
    System.out.println("客户端说:" + s);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    };
    t1.start();

    Thread t2 = new Thread() {
    @Override
    public void run() {
    BufferedReader br = new BufferedReader(
    new InputStreamReader(System.in));
    try {
    DataOutputStream out = new DataOutputStream(
    socket.getOutputStream());
    while (true) {
    String s = br.readLine();
    out.writeUTF(s);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    };
    t2.start();
    }
    }

    }

    package j2se.core.net.chat;

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    import java.net.UnknownHostException;

    /**
    * 简单的聊天时范例(客户端)
    */
    public class Client {

    public static void main(String[] args) throws UnknownHostException, IOException {
    final Socket s = new Socket("127.0.0.1", 8888);
    Thread t1 = new Thread() {
    @Override
    public void run() {
    DataInputStream input;
    try {
    input = new DataInputStream(s.getInputStream());
    while (true) {
    String s = input.readUTF();
    System.out.println("服务端说:" + s);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    };
    t1.start();

    Thread t2 = new Thread() {
    @Override
    public void run() {
    BufferedReader br = new BufferedReader(
    new InputStreamReader(System.in));
    try {
    DataOutputStream out = new DataOutputStream(s.getOutputStream());
    while (true) {
    String s = br.readLine();
    out.writeUTF(s);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    };
    t2.start();
    }

    }

  • 相关阅读:
    Appium自动化测试之adb连接逍遥安卓模拟器
    monkey测试常用命令
    Appium自动化测试环境搭建(一)
    Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家
    Appium自动化测试之元素定位方法
    fiddler模拟弱网
    在python中使用except捕获任何类型的异常
    python中求列表的并集差集
    Linux下Shell脚本实现IP地址校验
    运行时“ImportError: DLL load failed: 找不到指定的模块”
  • 原文地址:https://www.cnblogs.com/angel512/p/5868150.html
Copyright © 2020-2023  润新知