2017-06-14 62 views
0

我想了解url和我的代碼之間的連續連接。我每500m/s發送一次數據到json的http。然而,當我檢查WireShark時,連接斷開並重新連接。我嘗試了三種(httpwebrequest,cURL,webclient),我想要的是讓它們持續連接。現在,我只想要cURL來做到這一點。以下是我嘗試過的代碼,以及wireshark捕獲的代碼,讓您更好地理解。C#HttpWebRequest或cURL想要連續連接

捲曲 100M/S功能

static private void SystemStatusTimer_Elapsed(object sender, ElapsedEventArgs e) 
    { 

     string json = new JavaScriptSerializer().Serialize(new 
     { 
      cpuusage = CPU, 
      availablememory = AVAILMEMORY, 
      availableharddiskspace = AVAILABLEHD 
     }); 
     Easy.WriteFunction wf = SystemStatusWF; //response 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_URL, SYSTEMSTATUS_SERVER); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, json); 
     var resultOK = SystemStatusEasy.Perform(); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf); //response    
    } 

主要功能

static void Main() 
    {    
     Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); 

     SystemStatusEasy = new Easy(); 
     var slistHeaders = new Slist(); 
     slistHeaders.Append("Content-Type: application/json"); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_VERBOSE, 1L); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, slistHeaders); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_CUSTOMREQUEST, "POST"); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_POST, true); 
     SystemStatusEasy.SetOpt((CURLoption)213, 1L); //keep alive(not sure in c#) 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_TIMEOUT, false); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, "cookie.txt"); 
     SystemStatusEasy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, "cookie.txt"); 

     systemStatusTimer = new Timer(500); 
     systemStatusTimer.Elapsed += SystemStatusTimer_Elapsed; 
     systemStatusTimer.Start();    

     Console.ReadKey(); 
    } 

Wireshark Capture

正如你可以在Wireshark的捕捉看到,他們正在被FIN/ACK,然後斷開連接Syn再次連接。我只想在程序啓動時使用一個SYN,然後在進程結束時再使用FIN/ACK。任何幫助將會得到讚揚。

+0

你的意思是你想保持連接嗎?你不能確保它,你能做的最好的就是你已經做了什麼,要求服務器保持連接的活性,但是如果服務器在發送響應之後決定關閉連接,那麼你無法做任何事情來保證它的打開。 – Gusman

+0

那麼它保持活動狀態,它將打開服務器和程序之間的連接,直到服務器關閉連接?如果是我可以知道如何使cURL保持連接狀態?我這樣做(SystemStatusEasy.SetOpt((CURLoption)213,1L);)因爲我找不到.NET的正確API,但我知道在C中,它確實有curl_easy_setopt(curl,CURLOPT_TCP_KEEPALIVE,1L);.它可以在https://curl.haxx.se/libcurl/c/CURLOPT_TCP_KEEPALIVE.html中找到。我知道CURLOPT_TCP_KEEPALIVE在枚舉中的數量是213.所以我確實試圖把它像SystemStatusEasy.SetOpt((CURLoption)213,1L); –

+0

但不知道我做對了或沒有..沒有太多的信息關於C#和cURL .. T^T任何建議可憐的掙扎的傢伙? –

回答

0

所以這是我用HttpWebRequest做的。

static HttpWebRequest httpWebRequest; 
static void Main() 
    { 
     httpWebRequest = (HttpWebRequest)WebRequest.Create(SYSTEMSTATUS_SERVER); // 

     systemStatusTimer = new Timer(500); 
     systemStatusTimer.Elapsed += SystemStatusTimer_Elapsed; 
     systemStatusTimer.Start(); 

     Console.ReadKey(); 
    } 

static private void SystemStatusTimer_Elapsed(object sender, ElapsedEventArgs e) 
    { 

     httpWebRequest.ContentType = "application/json"; 
     httpWebRequest.Method = "POST"; 
     httpWebRequest.KeepAlive = true; 
     httpWebRequest.CookieContainer = cookieContainer; 
     httpWebRequest.ServicePoint.SetTcpKeepAlive(true, 100000, 1000); 
     Console.WriteLine(cookieContainer); 

     Console.WriteLine("system Status 100m/s : {0}\t{1}\t{2}", CPU, AVAILABLEHD, AVAILMEMORY); 
     using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) 
     { 
      string json = new JavaScriptSerializer().Serialize(new 
      { 
       cpuusage = CPU,      
       availablememory = AVAILMEMORY, 
       availableharddiskspace = AVAILABLEHD 
      }); 

      streamWriter.Write(json); 
     } 

     var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 
     using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
     { 
      var result = streamReader.ReadToEnd(); 
      var result_header = httpResponse.Headers; 

      Console.WriteLine("The response of sysStatus from server : " + result);    
     } 
    } 

最後來自wireshark的結果。 wireshark capture

哦,它只給出一個SYN和一個FIN/ACK。但是我不斷地發送系統數據到服務器500m/s。爲什麼它給了我FIN/ACK並且之後不發送數據?