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. socket listening error

socket listening error

Scheduled Pinned Locked Moved C#
helpsecurityloungeworkspace
6 Posts 2 Posters 1 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.
  • J Offline
    J Offline
    jtmtv18
    wrote on last edited by
    #1

    i had wrote a set of class's for sending serialized objects over the net using sockets not to long ago..it worked perfectly then. I wrote a few programs (chat/file sharing) using the classes and they also worked perfectly. Just tonight i went back to use it agian with some new idea's and i have noticed i cant get my sockets to listen all of a sudden. i traced the error down to when the socket calls BeginAccept (the listening socket which is already done doing the setup to be in the listening state). I dont know if this is a code problem because i know for sure i havent touched the code when i used it, i just used the compiled dll's.I tryed uninstalling my old fire wall to see if thats the problem *zone alarm* but it didnt seem to fix it. The sending portion seems to work but the error only lies when i try to call begin accept intialy, This happens so when a connection does come in it is ready to accept it.here is the code with the error. IPEndPoint ipe = new IPEndPoint(GetMachineIPAddress(),39888); MainListener= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); MainListener.Bind(ipe); MainListener.Listen(300); Thread.Sleep(10); //code excutes fine until the line below. which locks the thread indefinitly. MainListener.BeginAccept(new AsyncCallback(MainListenerBeginAccept),MainListener); if anyone wants to see the full code i would send it...its large and uses compression/serializing/encryption/ect/bla bla bla. thanks alot for your help with this. Jesse M The Code Project Is Your Friend...

    R 1 Reply Last reply
    0
    • J jtmtv18

      i had wrote a set of class's for sending serialized objects over the net using sockets not to long ago..it worked perfectly then. I wrote a few programs (chat/file sharing) using the classes and they also worked perfectly. Just tonight i went back to use it agian with some new idea's and i have noticed i cant get my sockets to listen all of a sudden. i traced the error down to when the socket calls BeginAccept (the listening socket which is already done doing the setup to be in the listening state). I dont know if this is a code problem because i know for sure i havent touched the code when i used it, i just used the compiled dll's.I tryed uninstalling my old fire wall to see if thats the problem *zone alarm* but it didnt seem to fix it. The sending portion seems to work but the error only lies when i try to call begin accept intialy, This happens so when a connection does come in it is ready to accept it.here is the code with the error. IPEndPoint ipe = new IPEndPoint(GetMachineIPAddress(),39888); MainListener= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); MainListener.Bind(ipe); MainListener.Listen(300); Thread.Sleep(10); //code excutes fine until the line below. which locks the thread indefinitly. MainListener.BeginAccept(new AsyncCallback(MainListenerBeginAccept),MainListener); if anyone wants to see the full code i would send it...its large and uses compression/serializing/encryption/ect/bla bla bla. thanks alot for your help with this. Jesse M The Code Project Is Your Friend...

      R Offline
      R Offline
      Ronny Andersson
      wrote on last edited by
      #2

      do you call EndAccept in your AcyncCallBack method? Something like this:

      private void MainListenerBeginAccept(IAsyncResult asyncresult)
      {
          Socket s = (Socket)ar.AsyncState;
          Socket s2 = s.EndAccept(ar);
          // use s2 to communicate with client.
      }
      

      Note I haven't tried it out, so it may or may not work. If you like I can have a look at your MainListenerBeginAccept method. Ronny Andersson

      J 1 Reply Last reply
      0
      • R Ronny Andersson

        do you call EndAccept in your AcyncCallBack method? Something like this:

        private void MainListenerBeginAccept(IAsyncResult asyncresult)
        {
            Socket s = (Socket)ar.AsyncState;
            Socket s2 = s.EndAccept(ar);
            // use s2 to communicate with client.
        }
        

        Note I haven't tried it out, so it may or may not work. If you like I can have a look at your MainListenerBeginAccept method. Ronny Andersson

        J Offline
        J Offline
        jtmtv18
        wrote on last edited by
        #3

        yeah i do....here is the method i use. try running the code in junction with this and see if it works... private void MainListenerBeginAccept(IAsyncResult e) { try { //MessageBox.Show("Acception connection"); Socket main = (Socket)e.AsyncState; Socket secondary = main.EndAccept(e); if(secondary!=null) { StateObject tr = new StateObject(); //MessageBox.Show(secondary.Available.ToString()+" ava"); tr.buffer = new byte[bufferSize]; tr.MainSocket = main; tr.socket = secondary; // //Console.WriteLine("Beging To Accept"); // SocketsConnected.Add(secondary); // //secondary.BeginReceive(tr.buffer,0,tr.buffer.Length,SocketFlags.None,new AsyncCallback(PendingConnectionReveive),tr); } MainListener.BeginAccept(new AsyncCallback(MainListenerBeginAccept),MainListener); } catch(System.ObjectDisposedException){} catch(Exception ex) { OnError(this,ex); } } The Code Project Is Your Friend...

        R 1 Reply Last reply
        0
        • J jtmtv18

          yeah i do....here is the method i use. try running the code in junction with this and see if it works... private void MainListenerBeginAccept(IAsyncResult e) { try { //MessageBox.Show("Acception connection"); Socket main = (Socket)e.AsyncState; Socket secondary = main.EndAccept(e); if(secondary!=null) { StateObject tr = new StateObject(); //MessageBox.Show(secondary.Available.ToString()+" ava"); tr.buffer = new byte[bufferSize]; tr.MainSocket = main; tr.socket = secondary; // //Console.WriteLine("Beging To Accept"); // SocketsConnected.Add(secondary); // //secondary.BeginReceive(tr.buffer,0,tr.buffer.Length,SocketFlags.None,new AsyncCallback(PendingConnectionReveive),tr); } MainListener.BeginAccept(new AsyncCallback(MainListenerBeginAccept),MainListener); } catch(System.ObjectDisposedException){} catch(Exception ex) { OnError(this,ex); } } The Code Project Is Your Friend...

          R Offline
          R Offline
          Ronny Andersson
          wrote on last edited by
          #4

          I wrote a small test prog this morning just for fun. It is a very primitive and small webserver it only sends a greeting back to the webbrowser. Try it out locally on your machine with a webbrowser, then try it out with between two machines. Try changing the port from 80 to something else (I marked the line in red bold in the code) and try calling it again if it doesn't work I agree with you it might be a firewall problem. I've only got one PC at home so I cannot test it between two PCs. God Luck

          using System;
          using System.Net;
          using System.Net.Sockets;
          using System.Text;

          namespace TestSocket
          {
          class MiniWebServer
          {
          public static void OnAcceptConnection(IAsyncResult ar)
          {
          Socket serverSock = (Socket)ar.AsyncState;
          Socket clientSocket = serverSock.EndAccept(ar);
          try
          {
          serverSock.BeginAccept(new AsyncCallback(MiniWebServer.OnAcceptConnection),serverSock);
          ClientStateObject cso = new ClientStateObject();
          cso.worksocket = clientSocket;
          clientSocket.BeginReceive(cso.buffer, 0, 256, SocketFlags.None,
          new AsyncCallback(MiniWebServer.OnDataReceived), cso);
          }
          catch(Exception e)
          {
          serverSock.Close();
          Console.WriteLine(e.ToString());
          }
          }

              public static void OnDataReceived(IAsyncResult ar)
              {
                  ClientStateObject cso = (ClientStateObject)ar.AsyncState;
                  try
                  {
                      int read = cso.worksocket.EndReceive(ar);
                      Console.WriteLine(Encoding.ASCII.GetString(cso.buffer,0,read).ToString());
                      if(read == 256)
                      {
                          cso.worksocket.BeginReceive(cso.buffer,0,256,SocketFlags.None,
                              new AsyncCallback(MiniWebServer.OnDataReceived), cso);
                      }
                      else
                      {
                          cso.buffer = Encoding.ASCII.GetBytes("Hello from the MiniWebServer");
                          cso.worksocket.BeginSend(cso.buffer, 0, cso.buffer.Length, SocketFlags.None, 
                          new AsyncCallback(OnDataSent), cso);
                      }
                  }
                  catch(Exception e)
                  {
                      cso.worksocket.Close();
                      Console.WriteLine(e.ToString());
                  }
             }
          
             public static void OnDataSent(IAsyncResult
          
          J 1 Reply Last reply
          0
          • R Ronny Andersson

            I wrote a small test prog this morning just for fun. It is a very primitive and small webserver it only sends a greeting back to the webbrowser. Try it out locally on your machine with a webbrowser, then try it out with between two machines. Try changing the port from 80 to something else (I marked the line in red bold in the code) and try calling it again if it doesn't work I agree with you it might be a firewall problem. I've only got one PC at home so I cannot test it between two PCs. God Luck

            using System;
            using System.Net;
            using System.Net.Sockets;
            using System.Text;

            namespace TestSocket
            {
            class MiniWebServer
            {
            public static void OnAcceptConnection(IAsyncResult ar)
            {
            Socket serverSock = (Socket)ar.AsyncState;
            Socket clientSocket = serverSock.EndAccept(ar);
            try
            {
            serverSock.BeginAccept(new AsyncCallback(MiniWebServer.OnAcceptConnection),serverSock);
            ClientStateObject cso = new ClientStateObject();
            cso.worksocket = clientSocket;
            clientSocket.BeginReceive(cso.buffer, 0, 256, SocketFlags.None,
            new AsyncCallback(MiniWebServer.OnDataReceived), cso);
            }
            catch(Exception e)
            {
            serverSock.Close();
            Console.WriteLine(e.ToString());
            }
            }

                public static void OnDataReceived(IAsyncResult ar)
                {
                    ClientStateObject cso = (ClientStateObject)ar.AsyncState;
                    try
                    {
                        int read = cso.worksocket.EndReceive(ar);
                        Console.WriteLine(Encoding.ASCII.GetString(cso.buffer,0,read).ToString());
                        if(read == 256)
                        {
                            cso.worksocket.BeginReceive(cso.buffer,0,256,SocketFlags.None,
                                new AsyncCallback(MiniWebServer.OnDataReceived), cso);
                        }
                        else
                        {
                            cso.buffer = Encoding.ASCII.GetBytes("Hello from the MiniWebServer");
                            cso.worksocket.BeginSend(cso.buffer, 0, cso.buffer.Length, SocketFlags.None, 
                            new AsyncCallback(OnDataSent), cso);
                        }
                    }
                    catch(Exception e)
                    {
                        cso.worksocket.Close();
                        Console.WriteLine(e.ToString());
                    }
               }
            
               public static void OnDataSent(IAsyncResult
            
            J Offline
            J Offline
            jtmtv18
            wrote on last edited by
            #5

            i think it might be something my firewall because i cant even get a response out of this line of code serverSock.BeginAccept(new AsyncCallback(MiniWebServer.OnAcceptConnection),serverSock); it locks the program up every single time. i also noticed something strange...i like to seed my Random class with the current time..so i pass in this Random rn = new Random((int)DateTime.Now.Ticks); but every time i ran the code line above it would throw a stack overflow exception. I also tryed uninstalling / disabling the firewall and it didnt seem to do anything.Should i try reinstalling the framework or something ? any ideas ? The Code Project Is Your Friend...

            J 1 Reply Last reply
            0
            • J jtmtv18

              i think it might be something my firewall because i cant even get a response out of this line of code serverSock.BeginAccept(new AsyncCallback(MiniWebServer.OnAcceptConnection),serverSock); it locks the program up every single time. i also noticed something strange...i like to seed my Random class with the current time..so i pass in this Random rn = new Random((int)DateTime.Now.Ticks); but every time i ran the code line above it would throw a stack overflow exception. I also tryed uninstalling / disabling the firewall and it didnt seem to do anything.Should i try reinstalling the framework or something ? any ideas ? The Code Project Is Your Friend...

              J Offline
              J Offline
              jtmtv18
              wrote on last edited by
              #6

              I found the problem. I was using this program called steganos internet anonm sounded intresting...makes your browser essentially hidden...but it must of be acting as a firewall also...blocking the framework form listening...my firewall was off...but even when it wasnt it said the my program was listening, so steganos must of be running between the too. Anyways thanks sooo much for your help i really apreciate it. Jesse M. The Code Project Is Your Friend...

              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