2016-08-02 2 views
0

각 스레드 (스레드 당 1ml 데이터 파일)에서 여러 xml 파일을 웹 서버로 보내려고합니다. 내가 겪고있는 것 같습니다 그것은 단지 하나의 파일을 보내고 스레드가 동일한 연결 스트림을 사용하고 있다는 의미 연결이 닫힙니다. 내가 뭘 try/catch 코드를 각각의 스레드에 대한 새 연결을 만들 수 있도록 할 필요가 그래서 그들은 독립적 인 연결 있지만 나는 이미 그것을하고 있지만 내가 왜 연결을 닫고 응답을받지 않는지 모르겠다 생각 Logged 웹 서버에서 첫 xml 스레드에만 응답하고 때로는 모두에 응답합니다.동일한 연결을 사용하는 다중 스레드 웹 요청

foreach (Thread t in threads) 
{ 
    t.Start(names[0]); 
    names.RemoveAt(0); 
} 

으로 :

for(int i = 0; i < threads.Count; i++)) 
{ 
    var t= threads[i]; 
    var name=names[i]; 
    t.Start(name); 
} 

내가 경쟁 조건에 대한 의심 해요

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Xml; 

namespace XMLSender 
{ 
    class Program 
    { 
     private static string serverUrl; 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Please enter the URL to send the XML File"); 
      serverUrl = Console.ReadLine(); 


      List<Thread> threads = new List<Thread>(); 
      List<string> names = new List<string>(); 
      string fileName = ""; 

      do 
      { 
       Console.WriteLine("Please enter the XML File you Wish to send"); 
       fileName = Console.ReadLine(); 
       if (fileName != "start") 
       { 
        Thread t = new Thread(new ParameterizedThreadStart(send)); 
        threads.Add(t); 
        names.Add(fileName); 
       } 
      } 
      while (fileName != "start"); 

      foreach (Thread t in threads) 
      { 
       t.Start(names[0]); 
       names.RemoveAt(0); 
      } 
      foreach (Thread t in threads) 
      { 
       t.Join(); 
      } 

     } 
     static private void send(object data) 
     { 
      try 
      { 
       //Set the connection limit of HTTP 
       System.Net.ServicePointManager.DefaultConnectionLimit = 10000; 

       //ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };    
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(serverUrl)); 

       byte[] bytes; 

       //Load XML data from document 
       XmlDocument doc = new XmlDocument(); 
       doc.Load((string)data); 
       string xmlcontents = doc.InnerXml; 

       //Send XML data to Webserver 
       bytes = Encoding.ASCII.GetBytes(xmlcontents); 
       request.ContentType = "text/xml; encoding='utf-8'"; 
       request.ContentLength = bytes.Length; 
       request.Method = "POST"; 
       Stream requestStream = request.GetRequestStream(); 
       requestStream.Write(bytes, 0, bytes.Length); 
       requestStream.Close(); 

       // Get response from Webserver 
       HttpWebResponse response; 
       response = (HttpWebResponse)request.GetResponse(); 
       Stream responseStream = response.GetResponseStream(); 
       string responseStr = new StreamReader(responseStream).ReadToEnd(); 
       Console.Write(responseStr + Environment.NewLine); 

      } 
      catch (Exception e) 
      { 
       Console.WriteLine("An Error Occured" + Environment.NewLine + e); 
       Console.ReadLine(); 
      } 
     } 
    } 
} 
+0

그런 다음 "WebRequest.Create"를 호출하여 새 연결을 만듭니다 ... –

+0

[여러 개의 WebRequest를 Parallel.For에서 보내기] (http://stackoverflow.com/questions/7477261/send-multiple-webrequest- in-parallel-for) – Sinatr

+0

거기에 그것입니까? try catch 부분에서 각 스레드에서 실행됩니까? 어쨌든 새로운 것을 만들지 않겠습니까? 하지만 그것은 – Freon

답변

1

귀하의 코드는 OK 것, 바로이 비트가

가 대체 무슨 일이 일어 나는지 조정할 그 비트.

+0

좋아, 먼저 xml이 처리되고 두 번째 연결이 닫힙니다.하지만 때때로 둘 다 작동합니까? 너무 빠르게 테스트하고 있습니다. 서버가 테스트를하기 전에 연결을 빨리 닫지는 않았습니까? – Freon

+0

3 XML 파일로 테스트 했으므로 첫 번째 두 스레드는 웹 서버에서 응답을 받고 세 번째는 강제 연결 닫기 예외를 반환합니다. 나는 그것이 원하는 w/e를하기로 결정한 것처럼 느낍니다. – Freon

+0

멋지 네요, 서버 쪽을 직접 구현 했습니까? 그렇다면 코드를 복사하십시오. 방해하지 않아야 할 스레드에 별도의'request' 객체를 생성하고 있습니다. 그건 그렇고, 나는 코드를 시도해 봤어. – akazemis

관련 문제