簡體   English   中英

如何使用Chilkat套接字類C#構建TCP IP列表器以讀取傳入消息

[英]How to build TCP IP listner to read incoming messages using chilkat socket class c#

我正在使用chilkat套接字類。 問題是我想保持套接字打開狀態,可以說我執行了表單並第一次打開了特定IP上的端口以偵聽消息。現在我只能第一次成功接收消息,此后消息我想讓我的應用程序繼續監聽並在收到新消息時接收消息。

我們有幾個客戶端,它們將在同一端口和ip上連接並發送一些文本消息。

但是我無法做到這一點。 我需要構建一個偵聽器,該偵聽器將繼續偵聽,並且一旦收到需要處理的任何消息。 任何使用chilkat類或具有此類應用程序經驗的人都請向我建議如何實現此功能,因為我無法在CHILKAT網站上找到此類應用程序的好例子,或者可能是我沒有經驗如何精確編碼這種功能。

編輯1:傑米

是的,我們已經開發了REST WCF服務,並且它們工作正常,但是問題在於REST WCF服務的響應出現了很大的響應標頭,我們不希望這樣做,因為在我們的企業應用程序中,Windows Phone 7手機還將進行通信和發送短信,並且僅出於移動設備的原因,我們試圖減少需要傳遞回的數據,並且通過使用套接字,我們可以避免額外的響應頭,並且由於成本原因,SMS不是我們的選擇。 如果您對Web服務有任何建議以盡量減少數據,請共享它。

您考慮過Web服務嗎? 幾乎所有可以發送Http請求的語言都可以使用它們。 如果您可以控制客戶端應用程序,那么Web Service絕對是正確的途徑。

http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html

編輯:

您是否考慮過簡單的http上傳字節,並帶有http響應代碼。 即Http正常,Http失敗。 您可以將狀態代碼自定義為適合您項目的任何內容。

編輯2:

也許只使用http狀態代碼作為響應的RPC樣式方法可能是合適的。 檢查此問題以獲取提示。 用C#進行json調用

基本上,您只是向URL發送一些字符串,然后返回狀態碼。 那是非常小的。

編輯3:

這是我用Reflector從一些舊代碼中提取出來的內容。 這僅是該過程的基本要點。 顯然,在第一個請求上應該有一個using語句。

public void SMS(Uri address, string data)
{

   // Perhaps string data is JSON, or perhaps its something delimited who knows.
   // Json seems to be the pretty lean.
    try
    {
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
        request.Method = "POST";
        // If we don't setup proxy information then IE has to resolve its current settings
        // and adds 500+ms to the request time.
        request.Proxy = new WebProxy();
        request.Proxy.IsBypassed(address);
        request.ContentType = "application/json;charset=utf-8";
        // If your only sending two bits of data why not add custom headers?
        // If you only send headers, no need for the StreamWriter.
        // request.Headers.Add("SMS-Sender","234234223");
        // request.Headers.Add("SMS-Body","Hey mom I'm keen for dinner tonight :D");
        request.Headers.Add("X-Requested-With", "XMLHttpRequest");
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.WriteLine(data);
        writer.Close();
        using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                // Either read the stream or get the status code and description.
                // Perhaps you won't even bother reading the response stream or the code 
                // and assume success if no HTTP error status causes an exception.
            }
        }
    }
    catch (WebException exception)
    {
        if (exception.Status == WebExceptionStatus.ProtocolError)
        {
            // Something,perhaps a HTTP error is used for a failed SMS?
        }
    }
}

請記住僅使用Http狀態代碼和描述進行響應。 並確保將請求的代理設置為繞過請求的URL,以節省解析IE代理的時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM