Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Thread.Sleep()

Thread.Sleep()

Scheduled Pinned Locked Moved C#
sysadmin
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrithula8
    wrote on last edited by
    #1

    Hi I have used multithreading for udp client/server communication.When i run the server both the threads are executing at the same time.So i used a Thread.Sleep() after the 1st thread.So the 1st thread works properly.When it comes to the 2nd thread it gives"the existing connection was forcibly closed by the remote host".Can you please give me your suggestion

    M 1 Reply Last reply
    0
    • M mrithula8

      Hi I have used multithreading for udp client/server communication.When i run the server both the threads are executing at the same time.So i used a Thread.Sleep() after the 1st thread.So the 1st thread works properly.When it comes to the 2nd thread it gives"the existing connection was forcibly closed by the remote host".Can you please give me your suggestion

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      is it because thread 2 is being called from in thread 1? it would be better if you could post some example code

      Life goes very fast. Tomorrow, today is already yesterday.

      M 1 Reply Last reply
      0
      • M musefan

        is it because thread 2 is being called from in thread 1? it would be better if you could post some example code

        Life goes very fast. Tomorrow, today is already yesterday.

        M Offline
        M Offline
        mrithula8
        wrote on last edited by
        #3

        Please give me your suggestion asap

        public UdpServer()
        {
        try
        {
        startServer = new Thread(new ThreadStart(start_server));
        startServer.Start();
        }
        catch (Exception e)
        {
        Console.WriteLine(e.Message);
        //startServer.Abort();
        }

               Thread.Sleep(20000);
                try
                {               
                    
                    startServer2 = new Thread(new ThreadStart(start\_server2));                
                    startServer2.Start();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    //startServer2.Abort();
                }
        
            } public static void start\_server()
            {
               
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
                try
                {
                    Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    newsock.Bind(ipep);
                    Console.WriteLine("Waiting for a client...");
        
                    while (true)
                    {  //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
                            EndPoint tmpRemote = (EndPoint)ipep;
                            // EndPoint tmpRemote = (sender);
        
                            byte\[\] data = new byte\[1024\];
                            Console.WriteLine("hai");
        
                            int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                            
                            Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                            Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  
        
                            data = new byte\[1024\];
                            string ss = "Welcome to the Server";
                            data = Encoding.ASCII.GetBytes(ss);
                            newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);
        
                            Console.WriteLine("\\nSent Acknowledgement");
                   
        
                        }
                }
                        catch (SocketException e)
                        {
                            Console.WriteLine(e.Message);
                        }
        
        
                        //start\_server();
                        //startServer.Start();            
        
            }
        

        public static void start_

        M D 2 Replies Last reply
        0
        • M mrithula8

          Please give me your suggestion asap

          public UdpServer()
          {
          try
          {
          startServer = new Thread(new ThreadStart(start_server));
          startServer.Start();
          }
          catch (Exception e)
          {
          Console.WriteLine(e.Message);
          //startServer.Abort();
          }

                 Thread.Sleep(20000);
                  try
                  {               
                      
                      startServer2 = new Thread(new ThreadStart(start\_server2));                
                      startServer2.Start();
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine(e.Message);
                      //startServer2.Abort();
                  }
          
              } public static void start\_server()
              {
                 
                      IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
                  try
                  {
                      Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                      newsock.Bind(ipep);
                      Console.WriteLine("Waiting for a client...");
          
                      while (true)
                      {  //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
                              EndPoint tmpRemote = (EndPoint)ipep;
                              // EndPoint tmpRemote = (sender);
          
                              byte\[\] data = new byte\[1024\];
                              Console.WriteLine("hai");
          
                              int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                              
                              Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                              Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  
          
                              data = new byte\[1024\];
                              string ss = "Welcome to the Server";
                              data = Encoding.ASCII.GetBytes(ss);
                              newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);
          
                              Console.WriteLine("\\nSent Acknowledgement");
                     
          
                          }
                  }
                          catch (SocketException e)
                          {
                              Console.WriteLine(e.Message);
                          }
          
          
                          //start\_server();
                          //startServer.Start();            
          
              }
          

          public static void start_

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          mrithula8 wrote:

          Please give me your suggestion asap

          This is not the way people communicate here. No one gets paid to help anyone, they do it because they wish to.

          जय हिंद

          1 Reply Last reply
          0
          • M mrithula8

            Please give me your suggestion asap

            public UdpServer()
            {
            try
            {
            startServer = new Thread(new ThreadStart(start_server));
            startServer.Start();
            }
            catch (Exception e)
            {
            Console.WriteLine(e.Message);
            //startServer.Abort();
            }

                   Thread.Sleep(20000);
                    try
                    {               
                        
                        startServer2 = new Thread(new ThreadStart(start\_server2));                
                        startServer2.Start();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        //startServer2.Abort();
                    }
            
                } public static void start\_server()
                {
                   
                        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
                    try
                    {
                        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        newsock.Bind(ipep);
                        Console.WriteLine("Waiting for a client...");
            
                        while (true)
                        {  //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
                                EndPoint tmpRemote = (EndPoint)ipep;
                                // EndPoint tmpRemote = (sender);
            
                                byte\[\] data = new byte\[1024\];
                                Console.WriteLine("hai");
            
                                int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                                
                                Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                                Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  
            
                                data = new byte\[1024\];
                                string ss = "Welcome to the Server";
                                data = Encoding.ASCII.GetBytes(ss);
                                newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);
            
                                Console.WriteLine("\\nSent Acknowledgement");
                       
            
                            }
                    }
                            catch (SocketException e)
                            {
                                Console.WriteLine(e.Message);
                            }
            
            
                            //start\_server();
                            //startServer.Start();            
            
                }
            

            public static void start_

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            Sorry i didnt read the exception in your OP. That exception says that there is a problem with the host you are connected to. i.e. the server. This means the server is cancelling the connection for one reason or another, it could be your server is limiting connections to just one (your first thread) and thats why the second fails. I cant help much with this kind of thing, but you basically need to look at settings on your server somewhere.

            Life goes very fast. Tomorrow, today is already yesterday.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups