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. Sockets in C#

Sockets in C#

Scheduled Pinned Locked Moved C#
csharpsysadminquestion
8 Posts 4 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 the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project. The client is now able to communicate with 1 server at one ip and port.But i want the client to call another udp server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc) How i can do this?Please give me your suggestions

    L U 2 Replies Last reply
    0
    • M mrithula8

      Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project. The client is now able to communicate with 1 server at one ip and port.But i want the client to call another udp server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc) How i can do this?Please give me your suggestions

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, what could be the problem? duplicate what you already have. And if there are any blocking calls, consider using an extra thread/backgroundworker. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      • M mrithula8

        Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project. The client is now able to communicate with 1 server at one ip and port.But i want the client to call another udp server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc) How i can do this?Please give me your suggestions

        U Offline
        U Offline
        User 3491102
        wrote on last edited by
        #3

        "Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.

        modified on Saturday, March 14, 2009 1:30 PM

        M 4 Replies Last reply
        0
        • U User 3491102

          "Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.

          modified on Saturday, March 14, 2009 1:30 PM

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

          Hi, If iam defining a new endpoint the client will be able to call yet another udp server.But i want the client to recursively call the servers at different port for every 15 secs. //client

          private void SendMessage()
          {

                  try
                  {
                      
                      listBox6.Items.Add("Connecting....");
              IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
          

          Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
          listBox6.Items.Add("Connected");

                      byte\[\] data = new byte\[1024\];
                      String snd = "hello";
                      data = Encoding.ASCII.GetBytes(snd);
                      listBox6.Items.Add("Transmitting...");
                 sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);                 
                     listBox6.Items.Add("Sent...");
                     EndPoint tmpRemote = (EndPoint)ipep;
                      listBox6.Items.Add("Message received from {0}:");
                      listBox6.Items.Add(ipep.ToString());
                      data = new byte\[1024\];
                      int recv = sock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                      String zz=Encoding.ASCII.GetString(data, 0, recv);
                      listBox6.Items.Add(zz);
                      if (zz == "Welcome to the Server")
                      {
                          lb9.Text = "Active";
                          lb9.BackColor = Color.Green;
                      }
                      else{
                          lb9.Text = "Inactive";
                          lb9.BackColor=Color.Red;
                      }              
                     
                  }
          
                  catch (SocketException e)
                  {
                      //Console.WriteLine("Error..... " + e.StackTrace);
                      MessageBox.Show(e.Message);
          
                  }
              }
          

          //server

          public static void start_server()
          {
          IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);

                      Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                      newsock.Bind(ipep);
                      Console.WriteLine("Waiting for a client...");
          
                      while (true)
                      {
                          try
                          {
                              //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
          
          J 1 Reply Last reply
          0
          • U User 3491102

            "Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.

            modified on Saturday, March 14, 2009 1:30 PM

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

            Hi, I used multithreading concept so the client is able to communicate with any number of servers.Using the same concept i tried to call the 2nd server after 10 secs.When it comes to the client the 2nd time it gives"Invalid OperationException Cross-thread operation not valid: Control 'listBox6' accessed from a thread other than the thread it was created on." //server

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

                    try
                    {
                        Thread.Sleep(10000);
            

            startServer2 = new Thread(new hreadStart(start_server2));

                        startServer2.Start();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
            
                }
            

            public static void start_server()
            {

                        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
            
                        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        newsock.Bind(ipep);
                        Console.WriteLine("Waiting for a client...");
            
                        while (true)
                        {
                            try
                            {
                                //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("gfgjfk");
            
            
                                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");
            
            1 Reply Last reply
            0
            • M mrithula8

              Hi, If iam defining a new endpoint the client will be able to call yet another udp server.But i want the client to recursively call the servers at different port for every 15 secs. //client

              private void SendMessage()
              {

                      try
                      {
                          
                          listBox6.Items.Add("Connecting....");
                  IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
              

              Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
              sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
              listBox6.Items.Add("Connected");

                          byte\[\] data = new byte\[1024\];
                          String snd = "hello";
                          data = Encoding.ASCII.GetBytes(snd);
                          listBox6.Items.Add("Transmitting...");
                     sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);                 
                         listBox6.Items.Add("Sent...");
                         EndPoint tmpRemote = (EndPoint)ipep;
                          listBox6.Items.Add("Message received from {0}:");
                          listBox6.Items.Add(ipep.ToString());
                          data = new byte\[1024\];
                          int recv = sock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                          String zz=Encoding.ASCII.GetString(data, 0, recv);
                          listBox6.Items.Add(zz);
                          if (zz == "Welcome to the Server")
                          {
                              lb9.Text = "Active";
                              lb9.BackColor = Color.Green;
                          }
                          else{
                              lb9.Text = "Inactive";
                              lb9.BackColor=Color.Red;
                          }              
                         
                      }
              
                      catch (SocketException e)
                      {
                          //Console.WriteLine("Error..... " + e.StackTrace);
                          MessageBox.Show(e.Message);
              
                      }
                  }
              

              //server

              public static void start_server()
              {
              IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);

                          Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                          newsock.Bind(ipep);
                          Console.WriteLine("Waiting for a client...");
              
                          while (true)
                          {
                              try
                              {
                                  //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
              
              J Offline
              J Offline
              Jimmanuel
              wrote on last edited by
              #6

              did you try my last[^] suggestion?

              1 Reply Last reply
              0
              • U User 3491102

                "Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.

                modified on Saturday, March 14, 2009 1:30 PM

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

                Hi The following code gives me the output: Waiting for client... hai Waiting for client2..... hai Message received from... hello Sent ackn.... hai Message received from... server Sent ackn.... I want it to be like: Waiting for client... hai Message received from... hello Sent ackn.... Waiting for client 2... I tried to change but im getting"the existing connection was forcibly closed by the remote host"..Please help me with this //client

                public void SendMessage()
                {

                        try
                        {
                           
                            listBox6.Items.Add("Connecting....");
                            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
                            //IPEndPoint ipep = new IPEndPoint();
                            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
                            //sock.Bind(ipep);
                            listBox6.Items.Add("Connected");
                            byte\[\] data = new byte\[1024\];
                            String snd = "hello";
                            data = Encoding.ASCII.GetBytes(snd);
                            listBox6.Items.Add("Transmitting...");
                            sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);//sent hello                
                            //sock.SendTo(data, ipep);//sent hello                
                
                            listBox6.Items.Add("Sent...");
                            EndPoint tmpRemote = (EndPoint)ipep;
                            listBox6.Items.Add("Message received from {0}:");
                            listBox6.Items.Add(ipep.ToString());
                            data = new byte\[1024\];
                            int recv = sock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                            String zz=Encoding.ASCII.GetString(data, 0, recv);
                            listBox6.Items.Add(zz);
                            if (zz == "Welcome to the Server")
                            {
                                lb9.Text = "Active";
                                lb9.BackColor = Color.Green;
                            }
                            else{
                                lb9.Text = "Inactive";
                                lb9.BackColor=Color.Red;
                            }              
                        }
                
                        catch (SocketException e)
                        {
                            //Console.WriteLine("Error..... " + e.StackTrace);
                            MessageBox.Show(e.Message);
                
                        }
                        //SendMessage1();
                    }
                
                    public void SendMessage1()
                    {
                    //same as above
                
                1 Reply Last reply
                0
                • U User 3491102

                  "Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.

                  modified on Saturday, March 14, 2009 1:30 PM

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

                  Hi I used the Thread.Sleep()

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

                         try
                          {
                              Thread.Sleep(20000);
                             
                              startServer2 = new Thread(new ThreadStart(start\_server2));
                             
                              startServer2.Start();
                          }
                          catch (Exception e)
                          {
                              Console.WriteLine(e.Message);
                              //startServer2.Abort();
                          }
                  
                      }
                  

                  Im getting the output as Waiting for client... hai Message received.... hello Sent ack Waiting for client2... hai At this it shows"existing connection was forcibly closed by the remote host".Please give me your suggestion

                  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