zoukankan      html  css  js  c++  java
  • 网络编程

    服务器

    public class SimpleServer extends Thread{
    private ServerSocket serverSocket;
    public SimpleServer(int port) throws IOException{
    //类被实例化,服务器被创建
    serverSocket=new ServerSocket(port);
    //异常
    // serverSocket.setSoTimeout(10000);
    }
    @Override
    public void run() {
    while(true){
    try {
    Socket socket=serverSocket.accept();
    // /打印谁连接了我们的服务器
    // PrintWriter printWriter=new PrintWriter(socket.getOutputStream());
    DataOutputStream out=new DataOutputStream(socket.getOutputStream());
    out.writeUTF("我是服务器,你来干什么");


    DataInputStream dis=new DataInputStream(socket.getInputStream());
    String s=dis.readUTF();
    System.out.println(s);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public static void main(String[] args) {
    try {
    Thread thread=new SimpleServer(9000);
    thread.start();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    客服端

    public class SimpleClient {
    public static void main(String[] args) {
    try {
    Socket socket=new Socket("localhost", 9000);
    //接受服务区信息
    DataInputStream inputStream=new DataInputStream(socket.getInputStream());
    String s=inputStream.readUTF();
    System.out.println(s);//打印服务器的信息

    //客户机发送信息到服务器
    DataOutputStream input=new DataOutputStream(socket.getOutputStream());
    input.writeUTF("我是小黑,我来找你,服务器你好");
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

  • 相关阅读:
    OpenSSL生成证书、密钥
    js中对String去空格
    正则表达式
    webapi调用
    记一次完整的CI持续集成配置过程(.net core+Jenkins+Gitea)
    处理asp.net core连接mysql的一个异常Sequence contains more than one matching element
    asp.net core 3.1+mysql8.0+Hangfire遇到的异常解决记
    升级到asp.net core 3.1遇到的json异常
    了解ASP.NET Core端点路由
    asp.net core 2.2升到3.1遇到的问题小记
  • 原文地址:https://www.cnblogs.com/chenxi2016/p/5440873.html
Copyright © 2011-2022 走看看