0

I am trying to send multiple xml files on each thread(1 xml datafile per thread) to a web server. The problem i am seem to be getting is that it only sends one files and the connection is closed, which means that the threads are using the same connection stream. What do i need to do in my try/catch code to make sure it creates a new connection for each thread so they are independant connection but i think im doing that already but i dont know why it keeps closing the connection and not receiving the response from the webserver, it only responds to first xml thread and sometimes it responds to them all .

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();
            }
        }
    }
}
Freon
  • 47
  • 1
  • 11
  • Then call "WebRequest.Create" to create a new connection... –  Aug 02 '16 at 13:21
  • Possible duplicate of [Send multiple WebRequest in Parallel.For](http://stackoverflow.com/questions/7477261/send-multiple-webrequest-in-parallel-for) – Sinatr Aug 02 '16 at 13:29
  • Its there isnt it? In the try catch part where its run on each thread? shouldnt it create a new one anyway? but it doesnt – Freon Aug 02 '16 at 13:29
  • @Sinatr ill check it out and let you know. (Update) Seems like that doesnt work still get (The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. – Freon Aug 02 '16 at 13:31
  • The web server should handle multiple requests/responses. – Freon Aug 02 '16 at 13:34

1 Answers1

1

Your code seems OK, just tweak this bit see what happens

Replace:

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

With:

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

I'm suspicious about the race condition in that bit.

akardon
  • 43,164
  • 4
  • 34
  • 42
  • Alright i changed it, First xml goes through, second one causes the connection to be closed. But sometimes it both work? Maybe im testing it too fast that the server doesnt close the connection fast enough before i retest it? – Freon Aug 02 '16 at 13:53
  • Alright so i tested it with 3 XML files, and The first two threads are sent and receive a response from web server and the 3rd on returns a forced connection close exception. I feel like its decides to do w/e it wants. – Freon Aug 02 '16 at 13:59
  • Cool, have you implemented the server-side yourself? If that's the case please copy that code either. You're creating separate `request` objects in your threads so they shouldn't interfere. By the way , I tweaked the code give it a try as well – akardon Aug 02 '16 at 14:23
  • By the way , I tweaked the code give it a try as well – akardon Aug 02 '16 at 14:27
  • Ah i didnt implement the serverside, – Freon Aug 02 '16 at 14:30
  • Sometimes it get the response for 2/3 XML files and sometimes 3/3. – Freon Aug 02 '16 at 14:32
  • Its the same thing that happens, im pretty sure it might have to do how the webserver processes it because i go onto the server status page and it says its processed 3 requests. – Freon Aug 02 '16 at 14:42