激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java Socket編程實例(一)- TCP基本使用

Java Socket編程實例(一)- TCP基本使用

2020-05-16 16:05kingxss JAVA教程

這篇文章主要講解Java Socket編程中TCP的基本使用,希望能給大家做一個參考。

一.服務端代碼:

?
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
import java.net.*; // for Socket, ServerSocket, and InetAddress
import java.io.*; // for IOException and Input/OutputStream
 
public class TCPEchoServer {
 
  private static final int BUFSIZE = 32; // Size of receive buffer
 
  public static void main(String[] args) throws IOException {
 
    int servPort = 5500;
 
    // Create a server socket to accept client connection requests
    ServerSocket servSock = new ServerSocket(servPort);
 
    int recvMsgSize; // Size of received message
    byte[] receiveBuf = new byte[BUFSIZE]; // Receive buffer
 
    while (true) { // Run forever, accepting and servicing connections
      Socket clntSock = servSock.accept(); // Get client connection
 
      SocketAddress clientAddress = clntSock.getRemoteSocketAddress();
      System.out.println("Handling client at " + clientAddress);
 
      InputStream in = clntSock.getInputStream();
      OutputStream out = clntSock.getOutputStream();
 
      // Receive until client closes connection, indicated by -1 return
      while ((recvMsgSize = in.read(receiveBuf)) != -1) {
        out.write(receiveBuf, 0, recvMsgSize);
      }
 
      clntSock.close(); // Close the socket. We are done with this client!
    }
    /* NOT REACHED */
  }
}

二.客戶端代碼:

?
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
import java.net.*;
import java.io.*;
 
public class TCPEchoClient {
 
  public static void main(String[] args) throws IOException {
 
    String server = "127.0.0.1"; // Server name or IP address
    int servPort = 5500; //// Server port
    byte[] data = "Hi, World".getBytes();
 
    // Create socket that is connected to server on specified port
    Socket socket = new Socket(server, servPort);
    System.out.println("Connected to server...sending echo string");
 
    InputStream in = socket.getInputStream();
    OutputStream out = socket.getOutputStream();
 
    out.write(data); // Send the encoded string to the server
 
    // Receive the same string back from the server
    int totalBytesRcvd = 0; // Total bytes received so far
    int bytesRcvd; // Bytes received in last read
    while (totalBytesRcvd < data.length) {
      if ((bytesRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1)
        throw new SocketException("Connection closed prematurely");
      totalBytesRcvd += bytesRcvd;
    } // data array is full
 
    System.out.println("Received: " + new String(data));
    socket.close(); // Close the socket and its streams
  }
}

上述代碼的TCP服務端是單線程,一次只能服務一個客戶端。

查看更多Java的語法,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片在哪看 | 日韩视频在线不卡 | 中文字幕国 | 日本羞羞的午夜电视剧 | 久久精品视频免费 | 激情在线观看视频 | 福利在线免费 | 日韩在线视频一区二区三区 | 国产亚洲精品久久久久5区 日韩一级片一区二区三区 国产精品久久久久av | 国产视频在线播放 | 中国洗澡偷拍在线播放 | 成人午夜在线免费观看 | 特级a欧美做爰片毛片 | 色综合久久久久久久久久久 | 国产毛片自拍 | 一区二区三区四区精品 | 超久久 | lutube成人福利在线观看污 | 国产精品视频一区二区三区四区五区 | 国产午夜免费福利 | 91美女视频在线 | 九九热视频在线免费观看 | 欧美爱爱视频网站 | 成人午夜精品久久久久久久蜜臀 | 亚洲二区不卡 | 国产精品视频一区二区三区四区五区 | 一区二区三区四区在线观看视频 | 欧美一级久久 | 国产精品999在线观看 | 日本成人一区二区 | 韩国三级日本三级香港三级黄 | 91网站永久免费看 | 国产精品视频中文字幕 | 鲁丝片一区二区三区免费入口 | 竹内纱里奈和大战黑人 | 欧美老逼| 91成人午夜性a一级毛片 | 毛片免费在线 | 一级做a爰片性色毛片2021 | 成人激情综合网 | 日韩视频不卡 |