欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年06月15日 09:09:21   作者:kingxss  
這篇文章主要講解Java Socket編程中TCP的基本使用,希望能給大家做一個參考。

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

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 */ 
  } 
} 

二.客戶端代碼:

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ù)一個客戶端。

查看更多Java的語法,大家可以關(guān)注:《Thinking in Java 中文手冊》、《JDK 1.7 參考手冊官方英文版》、《JDK 1.6 API java 中文參考手冊》、《JDK 1.5 API java 中文參考手冊》,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring boot使用多線程過程步驟解析

    Spring boot使用多線程過程步驟解析

    這篇文章主要介紹了Spring boot使用多線程過程步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot實現(xiàn)數(shù)據(jù)預(yù)熱的方式小結(jié)

    SpringBoot實現(xiàn)數(shù)據(jù)預(yù)熱的方式小結(jié)

    這里用到的數(shù)據(jù)預(yù)熱,就是在項目啟動時將一些數(shù)據(jù)量較大的數(shù)據(jù)加載到緩存中(筆者這里用的Redis),那么在項目啟動有哪些方式可以實現(xiàn)數(shù)據(jù)預(yù)熱呢,本文就來給大家講講幾種實現(xiàn)數(shù)據(jù)預(yù)熱的方式,需要的朋友可以參考下
    2023-09-09
  • RestTemplate實現(xiàn)多種底層HTTP客戶端類庫的切換用法

    RestTemplate實現(xiàn)多種底層HTTP客戶端類庫的切換用法

    這篇文章主要為大家詳細的講解了RestTemplate實現(xiàn)多種底層HTTP客戶端類庫的切換示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步
    2022-03-03
  • MyBatisPlus PaginationInterceptor分頁插件的使用詳解

    MyBatisPlus PaginationInterceptor分頁插件的使用詳解

    這篇文章主要介紹了MyBatisPlus PaginationInterceptor分頁插件的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • spring?項目實現(xiàn)限流方法示例

    spring?項目實現(xiàn)限流方法示例

    這篇文章主要為大家介紹了spring項目實現(xiàn)限流的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • SpringBoot數(shù)據(jù)校驗功能的實現(xiàn)

    SpringBoot數(shù)據(jù)校驗功能的實現(xiàn)

    這篇文章主要介紹了SpringBoot數(shù)據(jù)校驗功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • SpringBoot2.0整合WebSocket代碼實例

    SpringBoot2.0整合WebSocket代碼實例

    這篇文章主要介紹了SpringBoot2.0整合WebSocket代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • MyBatis-Plus與Druid結(jié)合Dynamic-datasource實現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例

    MyBatis-Plus與Druid結(jié)合Dynamic-datasource實現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例

    Dynamic-DataSource 可以和絕大多是連接層插件搭配使用,比如:mybatis,mybatis-plus,hibernate等,本文就來介紹一下MyBatis-Plus與Druid結(jié)合Dynamic-datasource實現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例,感興趣的可以了解一下
    2023-10-10
  • Java中數(shù)組的創(chuàng)建與傳參方法(學(xué)習(xí)小結(jié))

    Java中數(shù)組的創(chuàng)建與傳參方法(學(xué)習(xí)小結(jié))

    這篇文章主要介紹了Java中數(shù)組的創(chuàng)建與傳參方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • MAC算法之消息摘要算法HmacMD5的實現(xiàn)

    MAC算法之消息摘要算法HmacMD5的實現(xiàn)

    這篇文章主要介紹了MAC算法之消息摘要算法HmacMD5的實現(xiàn)的相關(guān)資料,這里提供實例,幫助大家學(xué)習(xí)理解這部分知識,需要的朋友可以參考下
    2017-08-08

最新評論