zoukankan      html  css  js  c++  java
  • socket

    server

    package socket_server;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class MyService {
    public static void main(String[] args) {
    try {
    ServerSocket s=new ServerSocket(8888);
    Socket s1=s.accept();
    InputStream inp = s1.getInputStream();
    OutputStream out = s1.getOutputStream();
    byte[] b=new byte[1024];
    int len =-1;
    len =inp.read(b);
    String str=new String(b, 0, len);
    if(str.equals("ok")){
    out.write("好好考试,争取被录取".getBytes());
    }
    s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    client

    package socket_client;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class MyClient {
    public static void main(String[] args) {
    try {
    Socket s=new Socket("192.168.74.179",8888);
    InputStream inp = s.getInputStream();
    OutputStream out = s.getOutputStream();
    out.write("ok".getBytes());
    byte[] b=new byte[1024];
    int len=-1;
    len=inp.read(b);
    String str =new String(b, 0, len);
    System.out.println(str);
    s.close();
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

  • 相关阅读:
    leetcode--95. Unique Binary Search Trees II
    leetcode--96. Unique Binary Search Trees
    leetcode分类总结
    leetcode-115. Distinct Subsequences
    tcpdump安装配置及抓包分析
    dp经典案例
    tree的各种问题
    大数的阶乘算法
    由mmap引发的SIGBUS
    win10系统下如何查看端口被哪个进程占用
  • 原文地址:https://www.cnblogs.com/joyous-day/p/6936397.html
Copyright © 2011-2022 走看看