欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java网络通信基础编程(必看篇)

时间:2020-11-17 01:27:07|栏目:JAVA代码|点击:

方式一:同步阻塞方式(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中的HashMap类

栏    目:JAVA代码

下一篇:Java 时间日期详细介绍及实例

本文标题:Java网络通信基础编程(必看篇)

本文地址:http://www.codeinn.net/misctech/23475.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有