Advertisement
anhkiet2507

GiaiThich_TCP Server

Oct 11th, 2022
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package com.kietna.tcpserver;
  2. /*
  3.     Translated by Nguyen Anh Kiet
  4.     Source: http://www2.ic.uff.br/~michael/kr1999/2-application/2_06-sockettcp.htm
  5.     Cách đọc: Đọc giải thích ở trên, Code ở dưới.
  6. */
  7. /*
  8. java.io Chứng các classes phục vụ input và output streams như BufferedReader and DataOutputStream
  9. java.net chức các class hỗ trợ về mạng. Cụ thể là Socket classes (ServerSocket hoặc clientSocket )
  10.  
  11. */
  12. import java.io.*;
  13. import java.net.*;
  14.  
  15. public class main {
  16.     public static void main(String[] args) throws IOException {
  17.         /*
  18.             Khai báo 2 String
  19.             clientSentence: String nhận được từ người dùng
  20.             capitalizedSentence: String đã được viết hoa để trả về người dùng
  21.         */
  22.         String clientSentence;
  23.         String capitalizedSentence;
  24.         // Tạo 1 Object kiểu ServerSocket mang tên WelcomeSocket, có số hiệu 6789 để lắng nghe kết nối từ Client
  25.         ServerSocket welcomeSocket = new ServerSocket(6789); //Replace 6789 to listening port you want
  26.         /*
  27.             The above line creates a new socket, called connectionSocket, when some client knocks on welcomeSocket.
  28.             TCP then establishes a direct virtual pipe between clientSocket at the client and connectionSocket at the server.
  29.             The client and server can then send bytes to each other over the pipe, and all bytes sent arrive at the other side in order.  
  30.             With connectionSocket established, the server can continue to listen for other requests from other clients for the application using welcomeSocket.
  31.             (This version of the program doesn't actually listen for more connection requests. But it can be modified with threads to do so.)
  32.             The program then creates several stream objects, analogous to the stream objects created in clientSocket.
  33.         */
  34.         while(true){
  35.             /*
  36.                 Tạo một Socket mới mang tên connectionSocket.
  37.                 Khi Client kết nối với welcomeSocket.
  38.                 TCP tạo ra một đường dẫn ảo giữa clientSocket ở Client và connectionSocket ở Server
  39.                 Client và Server sẽ gửi thông tin qua đường dẫn này
  40.                 Việc thiết lập connectionSocket cho phép server có thể tiếp tục lắng nghe các request từ các client khác thông qua welcomeSocket
  41.             */
  42.             Socket connectionSocket = welcomeSocket.accept();
  43.             /*
  44.                 Tạo 2 đối tượng Stream đính kèm với connectionSocket
  45.                 inFromClient lấy dữ liệu từ Client
  46.                 outToClient gửi dữ liệu về Client
  47.             */
  48.             BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  49.             DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  50.             // Đọc chuỗi clientSentence từ client gửi lên
  51.             clientSentence = inFromClient.readLine();
  52.             // Tạo ra chuỗi in hoa capitalizedSentence bằng thông tin từ chuỗi clientSentence hàm toUpperCase()
  53.             capitalizedSentence = clientSentence.toUpperCase() + '\n';
  54.             // Gửi lại chuỗi về Client qua stream outToClient
  55.             outToClient.writeBytes(capitalizedSentence);
  56.         }
  57.        
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement