美文网首页
1.TCP/IP+BIO

1.TCP/IP+BIO

作者: Bre_eze | 来源:发表于2018-07-06 22:30 被阅读0次

    即TCP协议下阻塞式传输

    客户端:

    @SuppressWarnings("resource")
    Socket socket = new Socket("127.0.0.1", 8989);
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
    out.println("hello");
    

    服务端:

    ServerSocket ss = new ServerSocket(8989);
    ss.setSoTimeout(10000);
    Socket socket = ss.accept();
    InputStream is = socket.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String info = br.readLine();
    System.out.println("Server receive info: " + info);
    

    代码缺点:
    服务端不能同时接收多个连接请求。

    相关文章

      网友评论

          本文标题:1.TCP/IP+BIO

          本文链接:https://www.haomeiwen.com/subject/kxciuftx.html