国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java基于Socket實現(xiàn)簡單的多線程回顯服務(wù)器功能示例

Java基于Socket實現(xiàn)簡單的多線程回顯服務(wù)器功能示例

2020-12-21 11:00zwcwu31 Java教程

這篇文章主要介紹了Java基于Socket實現(xiàn)簡單的多線程回顯服務(wù)器功能,結(jié)合實例形式分析了java使用socket進行多線程數(shù)據(jù)傳輸?shù)南嚓P(guān)操作技巧,需要的朋友以參考下

本文實例講述了Java基于Socket實現(xiàn)簡單的多線程回顯服務(wù)器功能。分享給大家供大家參考,具體如下:

需要兩個類,一個是EchoServer,代表服務(wù)器。另外一個是EchoServerClient,代表客戶端。代碼如下:

?
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
package interview;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
  public static void main(String []args) throws IOException{
    ServerSocket server = new ServerSocket(6789);
    while(true){
      Socket client = server.accept();
      ClientHandler handler = new ClientHandler(client);
      new Thread(handler).start();
    }
  }
  public static class ClientHandler implements Runnable{
    private Socket client;
    @Override
    public void run() {
      InputStreamReader isr = null;
      try {
        isr = new InputStreamReader(client.getInputStream());
        BufferedReader br = new BufferedReader(isr);
        PrintWriter pw = new PrintWriter(client.getOutputStream());
        String msg = br.readLine();
        System.out.println("收到" + client.getInetAddress() + "發(fā)送的" + msg);
        pw.println("收到了你發(fā)的" + msg);
        pw.flush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    public ClientHandler(Socket client){
      this.client = 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
package interview;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class EchoServerClient {
  public static void main(String []args) throws UnknownHostException, IOException{
    Socket client = new Socket("127.0.0.1", 6789);
    Scanner sc = new Scanner(System.in);
    System.out.print("請輸入要發(fā)送的內(nèi)容:");
    String msg = sc.nextLine();
    sc.close();
    PrintWriter pw = new PrintWriter(client.getOutputStream());
    pw.println(msg);
    pw.flush();
    InputStreamReader isr = new InputStreamReader(client.getInputStream());
    BufferedReader br = new BufferedReader(isr);
    System.out.println("服務(wù)器返回:" + br.readLine());
    client.close();
  }
}

NIO多路復(fù)用套接字方法如下:

?
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package interview;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
public class EchoServerNIO {
  private static ServerSocketChannel serverChannel = null;
  private static Selector selector = null;// 多路復(fù)用選擇器
  private static ByteBuffer buffer = null// 緩沖區(qū)
  public static void main(String []args) throws IOException{
    init();
    listen();
  }
  static void init() throws IOException{
    serverChannel = ServerSocketChannel.open();
    buffer = ByteBuffer.allocate(1024);
    serverChannel.socket().bind(new InetSocketAddress(6789));
    serverChannel.configureBlocking(false);
    selector = Selector.open();
    serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  }
  static void listen() throws IOException{
    while(true){
      if(selector.select(5000) != 0){
        Iterator<SelectionKey> it = selector.selectedKeys().iterator();
        while(it.hasNext()){
          SelectionKey key = it.next();
          it.remove();
          handleKey(key);
        }
      }
    }
  }
  static void handleKey(SelectionKey key) throws IOException{
    SocketChannel channel = null;
    if(key.isAcceptable()){
      ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
      channel = serverChannel.accept();
      channel.configureBlocking(false);
      channel.register(selector, SelectionKey.OP_READ);
    }else if(key.isReadable()){
      channel = (SocketChannel)key.channel();
      buffer.clear();
      if(channel.read(buffer) > 0){
        buffer.flip();
        CharBuffer charBuffer = CharsetHelper.decode(buffer);
        String msg = charBuffer.toString();
        System.out.println("收到" + channel.getRemoteAddress() + "的消息:" + msg);
        channel.write(CharsetHelper.encode(CharBuffer.wrap("received your msg:" + msg)));
      }
    }
  }
  public static class CharsetHelper{
    private static final String UTF_8 = "UTF-8";
    private static CharsetEncoder encoder = Charset.forName(UTF_8).newEncoder();
    private static CharsetDecoder decoder = Charset.forName(UTF_8).newDecoder();
    private CharsetHelper() {
    }
    public static ByteBuffer encode(CharBuffer in) throws CharacterCodingException{
      return encoder.encode(in);
    }
    public static CharBuffer decode(ByteBuffer in) throws CharacterCodingException{
      return decoder.decode(in);
    }
  }
}

希望本文所述對大家java程序設(shè)計有所幫助。

原文鏈接:http://blog.csdn.net/zwc2xm/article/details/72916294

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品一区二区无线 | 一区二区三区免费 | 亚洲视频一区二区三区在线观看 | 狠狠躁夜夜躁人人爽天天天天97 | 久久国产精品视频 | 日韩视频不卡 | 日本电影一区 | 国产精品国产a | 欧美成人a | 成人av一区二区三区 | 亚洲第1页 | 亚洲天堂中文字幕 | 欧美二区在线 | 日本中文字幕一区 | 亚洲国产精品久久久 | 亚洲综合区 | 免费av片在线 | 久久五月天婷婷 | 国产成人久久精品一区二区三区 | 亚洲国产免费 | 国产激情久久久久久 | 国产一区二区在线免费观看 | 欧美精品福利视频 | 久草成人网 | 欧美日韩国产在线观看 | 欧美大片免费高清观看 | 午夜影院网站 | 成人免费网站在线观看 | av一区二区三区四区 | 欧美午夜精品 | 久久精品国产99国产精品 | 成人午夜 | 99久久精品一区二区成人 | 亚洲欧美日韩在线 | av免费网站在线观看 | 中文字幕综合在线 | 中文字幕一区二区在线观看 | 在线亚洲免费 | 欧美综合网 | 情一色一乱一欲一区二区 | 污污视频网站免费 |