> For the complete documentation index, see [llms.txt](https://docs.proudnet.com/proudnet.cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.proudnet.com/proudnet.cn/proudnet/using_pn/server_client/using_server.md).

# 如何使用伺服器

## 定時器循環、RMI、事件處理

您不必在伺服器上每小時呼叫它，但如果您想定期執行某些操作，則需要<mark style="color:orange;">指定計時器回呼函數</mark>、<mark style="color:orange;">回呼週期</mark>以及啟動時傳遞給<mark style="color:orange;">回呼函數</mark>的自訂資料伺服器。

以下是指定它們的位置。

<table data-full-width="true"><thead><tr><th width="99">語言</th><th width="233">定時器回呼函數</th><th width="342">回呼週期</th><th>自訂數據</th></tr></thead><tbody><tr><td>C++</td><td>Proud.INetServerEvent.OnTimerCallback</td><td>Proud.CStartServerParameter.m_timerCallbackInterval</td><td>Proud.CStartServerParameter.m_timerCallbackContext</td></tr><tr><td>C#</td><td>Nettention.Proud.NetServer.TickDelegate</td><td>Nettention.Proud.StartServerParameter.timerCallbackIntervalMs</td><td>-</td></tr></tbody></table>

{% hint style="success" %}
**參考**\
[**從伺服器接收和回呼事件**  ](/proudnet.cn/proudnet-note/notes/main_loop.md#undefined-2)\
[**從伺服器回呼定時器**](/proudnet.cn/proudnet-note/notes/main_loop.md#undefined-3)
{% endhint %}

## 獲取各種資訊

從 <mark style="color:orange;">Server</mark> 中，您可以檢索各種訊息，例如客戶端清單、P2P 群組資訊、客戶端 IP 資訊和 ping 時間。

<table data-full-width="true"><thead><tr><th width="422">C++ 函數</th><th>C# 函數</th><th>註釋</th></tr></thead><tbody><tr><td>Proud.CNetServer.GetClientCount</td><td>Nettention.Proud.NativeNetServer.GetClientCount</td><td>獲取連線的客戶端數量。</td></tr><tr><td>Proud.CNetServer.GetClientHostIDs</td><td>Nettention.Proud.NetServer.GetClientHostIDs</td><td>取得已連線客戶端的 HostID 清單。</td></tr><tr><td>Proud.CNetServer.GetClientInfo</td><td>Nettention.Proud.NetServer.GetClientInfo</td><td>使用作為參數給出的 HostID 來獲取有關客戶端的信息。</td></tr><tr><td>Proud.CNetServer.GetJoinedP2PGroups</td><td>-</td><td>取得以參數形式給出的 HostID 的客戶端所屬的所有 p2p 群組。</td></tr><tr><td>Proud.CNetServer.GetLastUnreliablePingMs</td><td>Nettention.Proud.NetServer.GetLastUnreliablePingMs</td><td>傳回最近的 ping 時間（以毫秒為單位）。</td></tr><tr><td>Proud.CNetServer.GetP2PGroupInfo</td><td>Nettention.Proud.NetServer.GetP2PGroupInfo</td><td>檢索有關作為參數給出的 p2p 組的資訊。</td></tr><tr><td>Proud.CNetServer.GetP2PGroups</td><td>-</td><td>取得所有 p2p 組。</td></tr><tr><td>Proud.CNetServer.GetRecentUnreliablePingMs</td><td>Nettention.Proud.NetServer.GetRecentUnreliablePingMs</td><td>傳回最近的 ping 時間（以毫秒為單位）。</td></tr><tr><td>Proud.CNetServer.GetRemoteIdentifiableLocalAddrs</td><td>-</td><td>取得客戶端可以識別的伺服器的監聽連接埠位址。</td></tr><tr><td>Proud.CNetServer.GetStats</td><td>Nettention.Proud.NetServer.GetStats</td><td>取得伺服器狀態資訊。</td></tr><tr><td>Proud.CNetServer.GetTcpListenerLocalAddr</td><td>Nettention.Proud.NetServer.GetTcpListenerLocalAddr</td><td>取得可用於 TCP 通訊的所有本機位址。</td></tr><tr><td>Proud.CNetServer.GetTimeMs</td><td>Nettention.Proud.NetServer.GetTimeMs</td><td>取得目前伺服器時間（以毫秒為單位）。</td></tr><tr><td>Proud.CNetServer.GetUdpListenerLocalAddrs</td><td>-</td><td>取得可用於 UDP 通訊的所有本機位址。</td></tr><tr><td>Proud.CNetServer.IsConnectedClient</td><td>Nettention.Proud.NativeNetServer.IsConnectedClient</td><td>傳回以參數形式給出的 HostID 的客戶端是否已連線。</td></tr></tbody></table>

## 伺服器之間通信

當伺服器以相互分工的方式運作時，需要建立連線以在各個伺服器之間進行通訊。 此時，您可以使用P2P群組輕鬆設定伺服器之間的通訊。

> * 留下一台<mark style="color:orange;">Farm server</mark>。 伺服器電腦所在的 LAN 群組稱為<mark style="color:orange;">server farm</mark>。
> * 所有其他伺服器在連接到場伺服器的<mark style="color:orange;">Farm Client</mark>端之間執行 P2P 通訊。

每個場客戶端或伺服器進程都可以直接相互通信，無論它是伺服器還是客戶端。 隨機形式的網狀通訊是可能的，並且可以建立它們之間的自由依賴關係。

伺服器間通訊建議使用<mark style="color:orange;">net\_main</mark>，同時也要考慮通訊的<mark style="color:orange;">message\_length</mark>。

{% hint style="success" %}
**參考**

[**伺服器場 - 維基**](https://ko.wikipedia.org/wiki/%EC%84%9C%EB%B2%84_%ED%8C%9C)
{% endhint %}

***

## :arrow\_left: [返回](/proudnet.cn/proudnet/using_pn/server_client.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.proudnet.com/proudnet.cn/proudnet/using_pn/server_client/using_server.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
