一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

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

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

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

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

一.服務(wù)端代碼:

?
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服務(wù)端是單線程,一次只能服務(wù)一個(gè)客戶端。

查看更多Java的語(yǔ)法,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品香蕉99久久久久网站 | 出差被灌醉绝伦的上司日本 | 成人午夜影院在线观看 | 韩国免费视频 | 四虎影视永久免费视频观看 | 国内精品视频一区二区三区 | 久久这里只有精品视频e | 亚洲国产精品嫩草影院久久 | 窝窝影院午夜色在线视频 | 狠狠做五月深爱婷婷天天综合 | 亚州在线视频 | 四虎永久免费地址ww417 | 亚洲 日韩 国产 中文视频 | 特级老女人淫片高清视频 | 无限观看社区在线视频 | 五月色婷婷久久综合 | 香蕉eeww99国产精选播放 | 黑人巨大vs北条麻妃在线 | 日本成日本片人免费 | 亚洲另类老妇videos | www.四色 | 国产精品色拉拉免费看 | 欧美一级片观看 | 色视频国产 | 国产精品网页 | 青久草视频| 亚飞与亚基高清国语在线观看 | 国士李风起全文在线阅读 | 第一次出血videos | 天莱男模gary | 国产成人yy精品1024在线 | 国产在线精品香蕉综合网一区 | 春意午夜影院 | 欧美va在线观看 | 成人免费国产欧美日韩你懂的 | 黄漫免费观看 | 日韩一区二区中文字幕 | a级片在线播放 | 乌克兰xxxxx 我要色色网 | 99在线精品视频 | 色老妈|