萬維網和互聯網改變了我們的生活和相互交流的方式,以及我們進行科學、工程和商業的方式。現代生活完全是圍繞著網路來驅動或以網路為中心的。企業不斷尋求新的方式來以引入創新的新方式生產和交流各種服務。在本文中,我們將討論 Java 中的套接字程式設計。
套接字為 OSI 模型傳輸層的網路程式設計提供了介面。使用套接字的網路通訊在網路上隨處可見。除此之外,用 Java 編寫的套接字程式可以與用非 Java 語言(如 C、C++、Python 等)編寫的套接字程式進行通訊
廣告 該類別中的熱門課程 程式語言 - 專業化 | 54 課程系列 | 4 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
Socket 類別方法
Socket 類別方法是在 Java 中找到的。套接字綁定了一個連接埠號,以便 TCP 識別要傳送資料的連接埠號碼。 Java 提供了一組類,其中一個是 java.net。這用於網頁應用的快速開發。 java.net 套件中存在的關鍵類別、介面和異常簡化了創建客戶端和伺服器程式所涉及的複雜性:
課程有:
- 內容處理程序
- 資料報包
- 資料報套接字
- 資料報套接字 Imp 1
- HTTP URL 連線
- 我的網址
- 多播套接字
- 伺服器套接字
- 插座
- 套接字小鬼 1
- 網址
- URL 連接
- URL 編碼器
- URL 流處理程序
介面有:
- 內容處理工廠
- 檔名映射
- Socket Impl 廠
- URL 流處理工廠
例外情況是:
- 綁定異常
- 連線異常
- 格式錯誤的 URL 異常
- 沒有到主機的路由異常
- 協定異常
- 套接字異常
- 未知主機異常
- 未知服務異常
TCP/IP 套接字程式設計
java.net 套件中使用了兩個類,用於建立程式。
- 伺服器套接字
- 插座
伺服器程式透過輸入和輸出流進行通訊。如果有連線請求,那麼就會有一個新的套接字發揮作用,這裡是與它建立的連結。
方法1 – 建立伺服器Socket程式
用 Java 建立伺服器套接字程式有多個步驟。
建立伺服器套接字程式的簡單步驟如下:
第 1 步:Socket 伺服器已開啟。
Server Socket General= new Server Socket(PO)
這裡 PO 是連接埠號碼。
這裡連接埠號碼被分配給伺服器網絡,透過該網路它將使用輸入/輸出流進行通訊。
第2步:有客戶要求,我們必須耐心等待。
Socket General= server. accept()
這是伺服器。 accept()等待客戶端,這裡socket的名字是Client。
第 3 步:建立 I/O 流以便建立連線
資料輸入流
is = new Data Input Stream(client. Get Input Stream());
資料輸出流
os = new Data Output Stream(client. get Output Stream());
輸入流和輸出流被分配了它們的Get Input Stream(),並分別呼叫它們。
第 4 步:已建立與客戶的聯繫。
收到客戶的:
string hello = br. Read Line();
寄給客戶:
br. Write Bytes("How are you\n");
以下程式碼與接收請求並向客戶端發送請求的客戶端進行通訊。
第 5 步:最後,Socket 退出。
最後使用close socket函數來關閉並結束socket程式設計。
伺服器套接字的簡單範例如下:
一個用來連接伺服器的簡單程式。
代碼:
import java.net.*; import java.io.*; public class SimpleMachine { public static void main(String args[]) throws IOException { // On port 1362 server port is registered ServerSocket soc = new ServerSocket(1362); Socket soc1=soc.accept(); // Link is accepted after waiting // Linked with the socket there should be a connection OutputStream s1out = soc1.getOutputStream(); DataOutputStream dosH = new DataOutputStream (s1out); // A string command is sent dosH.writeUTF("Hello how are you"); // The connection can be closed but the server socket cannot. dosH.close(); s1out.close(); soc1.close(); } }
方法2 – 建立一個簡單的伺服器套接字程式
現在我們將看到一個用 Java 寫的簡單客戶端程式。
用Java建立一個簡單的客戶端程式的步驟如下所示:
第 1 步:建立 Socket 物件。
Socket client= new Socket(server, port_id)
伺服器和Port ID已連線;即伺服器連接到該Port ID。
第 2 步:建立輸入/輸出流。
is = new Data Input Stream(client.getInputStream());
os = new Data Output Stream(client.getOutputStream());
輸入流是,輸出流os用於與客戶端通訊。
Step 3: Input/Output streams are made for talking to the Client.
Data is read from the server:
string hello = br. readLine();
Send data to the server:
br.writeBytes("How are you\n")
This step communicates with the server. The input stream and output stream both communicates with the server.
Step 4: Close the Socket when done.
This function will close the client when it is finally done.
An example of a simple server socket program is shown below.
A simple program for the client.
Code:
import java.net.*; import java.io.*; public class SimpleMachineClient { public static void main(String args[]) throws IOException { // At port 1325, connection to the server is opened Socket s1H = new Socket("host",1325); // Read the input stream by getting an input file from the socket Input Stream s1I = s1. getInputStream(); Data Input Stream disH = new Data Input Stream(s1In); String str = new String (disH.readUTF()); System.out.println(str); // After it is done, we can close the connection. disH.close(); s1I.close(); s1H.close(); } }
Conclusion
Socket programming is beneficial in Java and in any other programming language. A program written in Java can connect with a program written in C or C++. In other words, the language of the socket program doesn’t matter when there has to be a connection between the two. In this article, we have basically seen the Simple Server and the Simple Client example where there is a connection between the server socket and in the other, there is a connection between the server socket. We have used TCP/IP programming for the same. However, there are a lot of programming techniques like UDP programming techniques and URL programming techniques. We have not seen examples of such in this article. We have stressed on TCP/IP programming technique.
以上是Java 中的套接字程式設計的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器