首頁  >  文章  >  Java  >  Java 中的套接字程式設計

Java 中的套接字程式設計

WBOY
WBOY原創
2024-08-30 16:27:37899瀏覽

萬維網和互聯網改變了我們的生活和相互交流的方式,以及我們進行科學、工程和商業的方式。現代生活完全是圍繞著網路來驅動或以網路為中心的。企業不斷尋求新的方式來以引入創新的新方式生產和交流各種服務。在本文中,我們將討論 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 套件中使用了兩個類,用於建立程式。

  • 伺服器套接字
  • 插座

伺服器程式透過輸入和輸出流進行通訊。如果有連線請求,那麼就會有一個新的套接字發揮作用,這裡是與它建立的連結。

Java 中的套接字程式設計

方法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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn