服务器之家

服务器之家 > 正文

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

时间:2020-10-22 17:30     来源/作者:jingxian

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

服务器端(Server):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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:

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

Client:

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

同步非阻塞(NIO)

异步非阻塞(AIO)

以上这篇Java网络通信基础编程(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部