成都网站建设设计

将想法与焦点和您一起共享

利用Java怎么样实现网络通信功能

利用Java怎么样实现网络通信功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联建站主营城厢网站建设的网络公司,主营网站建设方案,成都app开发,城厢h5成都微信小程序搭建,城厢网站营销推广欢迎城厢等地区企业咨询

方式一:同步阻塞方式(BIO):

服务器端(Server):

package com.ietree.basicskill.socket.mode1;

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

/**
 * 服务端
 */
public class Server {
  // 端口号
  final static int PORT = 8765;
  public static void main(String[] args) {
    ServerSocket server = null;
     try {
      server = new ServerSocket(PORT);
      System.out.println("Server start......");
      // 进行阻塞
      Socket socket = server.accept();
      // 创建一个程序执行客户端的任务
      new Thread(new ServerHandler(socket)).start();
      
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if(server != null){
        try {
          server.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      server = null;
    }
  }
}

采用多线程来处理接收到的请求(ServerHandler):

package com.ietree.basicskill.socket.mode1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerHandler implements Runnable {
  private Socket socket;
  public ServerHandler(Socket socket) {
    this.socket = socket;
  }

  @Override
  public void run() {
    BufferedReader in = null;
    PrintWriter out = null;
    try {
      in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
      out = new PrintWriter(this.socket.getOutputStream(), true);
      String body = null;
      while (true) {
        body = in.readLine();
        if(body == null){
          break;
        }
        System.out.println("Server: " + body);
        out.println("服务器端回送响应的数据。");
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if(in != null){
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if(out != null){
        try {
          out.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      if(socket != null){
        try {
          socket.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      socket = null;
    }
  }
}

客户端(Client):

package com.ietree.basicskill.socket.mode1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * 客户端
 */
public class Client {
  final static String ADDRESS = "127.0.0.1";
  final static int PORT = 8765;
  public static void main(String[] args) {
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    try {
      socket = new Socket(ADDRESS, PORT);
      in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      out = new PrintWriter(socket.getOutputStream(), true);
      
      // 向服务器端发送数据
      out.println("接收到客户端的请求数据......");
      String response = in.readLine();
      System.out.println("Client: " + response);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if(in != null){
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if(out != null){
        try {
          out.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      if(socket != null){
        try {
          socket.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      socket = null;
    }
  }
}

程序输出:

Server:

Server start......
Server: 接收到客户端的请求数据......

Client:

Client: 服务器端回送响应的数据。

同步非阻塞(NIO)

异步非阻塞(AIO)

看完上述内容,你们掌握利用Java怎么样实现网络通信功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


标题名称:利用Java怎么样实现网络通信功能
路径分享:http://chengdu.cdxwcx.cn/article/jeodpc.html