2017-06-14 25 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并且之后不发送数据?