BeginAccept doesn't work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nuno Magalhaes

    BeginAccept doesn't work

    Does anyone know why the BeginAccept doesn't work? If, in the code
    below, I do the normal Accept function I can get the client socket but
    it seems that the callback isn't really called.
    Here's the code:
    -----------------------------
    private Socket socket;
    private Socket clientSocket;

    public TcpServer(strin g serverIP,int serverPort)
    {
    clientSocket=nu ll;
    socket=new
    Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
    IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
    socket.Bind(ep) ;
    socket.Listen(1 0);
    socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
    }

    private void AcceptConnectio n(IAsyncResult ar)
    {
    Socket socket=(Socket) ar.AsyncState;
    clientSocket=so cket.EndAccept( ar);
    //throw new Exception("TCP Accepted on
    #"+clientSocket .RemoteEndPoint .ToString());
    throw new Exception("Yuhu u");
    }

  • Stoitcho Goutsev \(100\)

    #2
    Re: BeginAccept doesn't work

    Nuno,

    I don't see the error in this code.
    However the code doesn't show what serverIP and serverPort are. What I'd
    suggest after calling socket.Listen check socket.Connecte d to make sure that
    all the socket initialization went OK.
    Second is make sure that there are no any firewalls or antivirus programs
    that may block the access to the listening port.



    --

    Stoitcho Goutsev (100)



    "Nuno Magalhaes" <nunommagalhaes @hotmail.com> wrote in message
    news:1138399003 .207926.285830@ g47g2000cwa.goo glegroups.com.. .[color=blue]
    > Does anyone know why the BeginAccept doesn't work? If, in the code
    > below, I do the normal Accept function I can get the client socket but
    > it seems that the callback isn't really called.
    > Here's the code:
    > -----------------------------
    > private Socket socket;
    > private Socket clientSocket;
    >
    > public TcpServer(strin g serverIP,int serverPort)
    > {
    > clientSocket=nu ll;
    > socket=new
    > Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
    > IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
    > socket.Bind(ep) ;
    > socket.Listen(1 0);
    > socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
    > }
    >
    > private void AcceptConnectio n(IAsyncResult ar)
    > {
    > Socket socket=(Socket) ar.AsyncState;
    > clientSocket=so cket.EndAccept( ar);
    > //throw new Exception("TCP Accepted on
    > #"+clientSocket .RemoteEndPoint .ToString());
    > throw new Exception("Yuhu u");
    > }
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: BeginAccept doesn't work

      Nuno Magalhaes <nunommagalhaes @hotmail.com> wrote:[color=blue]
      > Does anyone know why the BeginAccept doesn't work? If, in the code
      > below, I do the normal Accept function I can get the client socket but
      > it seems that the callback isn't really called.[/color]

      Could you post a short but complete program which demonstrates the
      problem?

      See http://www.pobox.com/~skeet/csharp/complete.html for details of
      what I mean by that.

      You haven't really posted enough of the code for us to try it for
      ourselves.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Peter Bromberg [C# MVP]

        #4
        RE: BeginAccept doesn't work

        One more comment - throwing exceptions to send yourself "messages" is
        probably a good habit to get out of!

        Console.WriteLi ne or System.Diagnost ics.Debug.Write Line or even
        MessageBox.Show work just fine. Exceptions are expensive when they are
        thrown, and should be used only when something goes really wrong.
        Peter

        --
        Co-founder, Eggheadcafe.com developer portal:

        UnBlog:





        "Nuno Magalhaes" wrote:
        [color=blue]
        > Does anyone know why the BeginAccept doesn't work? If, in the code
        > below, I do the normal Accept function I can get the client socket but
        > it seems that the callback isn't really called.
        > Here's the code:
        > -----------------------------
        > private Socket socket;
        > private Socket clientSocket;
        >
        > public TcpServer(strin g serverIP,int serverPort)
        > {
        > clientSocket=nu ll;
        > socket=new
        > Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
        > IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
        > socket.Bind(ep) ;
        > socket.Listen(1 0);
        > socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
        > }
        >
        > private void AcceptConnectio n(IAsyncResult ar)
        > {
        > Socket socket=(Socket) ar.AsyncState;
        > clientSocket=so cket.EndAccept( ar);
        > //throw new Exception("TCP Accepted on
        > #"+clientSocket .RemoteEndPoint .ToString());
        > throw new Exception("Yuhu u");
        > }
        >
        >[/color]

        Comment

        • Nuno Magalhaes

          #5
          Re: BeginAccept doesn't work

          It's a really short program cause it's the beginning...

          The form: http://pwp.netcabo.pt/0141404701/RTSPServer.cs
          The server: http://pwp.netcabo.pt/0141404701/TcpServer.cs

          If you could help me I would be really appreciated... doing an Accept
          on TcpServer.cs does accept the client (like Internet Explorer
          "http://localhost:555/") so it's not from any firewall or antivirus.

          Thanks for any help.

          Jon wrote:[color=blue]
          > Nuno Magalhaes <nunommagalhaes @hotmail.com> wrote:[color=green]
          > > Does anyone know why the BeginAccept doesn't work? If, in the code
          > > below, I do the normal Accept function I can get the client socket but
          > > it seems that the callback isn't really called.[/color]
          >
          > Could you post a short but complete program which demonstrates the
          > problem?
          >
          > See http://www.pobox.com/~skeet/csharp/complete.html for details of
          > what I mean by that.
          >
          > You haven't really posted enough of the code for us to try it for
          > ourselves.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          > If replying to the group, please do not mail me too[/color]

          Comment

          • Nuno Magalhaes

            #6
            Re: BeginAccept doesn't work

            I understand that exceptions are very expensive but how can I, with
            Console.WriteLi ne, see it's output? In the debug window?

            Comment

            • Nuno Magalhaes

              #7
              Re: BeginAccept doesn't work

              One thing I've noticed is that if I try that code on a simple form load
              event it works ok like this:

              ....form load event...
              Socket socket=new
              Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
              IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
              socket.Bind(ep) ;
              socket.Listen(1 0);
              socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
              ....
              private void AcceptConnectio n(IAsyncResult ar)
              {
              throw new Exception("Yuhu u");
              }

              But when I move this code into another class it doesn't work.
              I tried this code on a command line class and it works but it doesn't
              work when I try to call the class from a form.
              Anyone knows why?

              Jon wrote:[color=blue]
              > Nuno Magalhaes <nunommagalhaes @hotmail.com> wrote:[color=green]
              > > Does anyone know why the BeginAccept doesn't work? If, in the code
              > > below, I do the normal Accept function I can get the client socket but
              > > it seems that the callback isn't really called.[/color]
              >
              > Could you post a short but complete program which demonstrates the
              > problem?
              >
              > See http://www.pobox.com/~skeet/csharp/complete.html for details of
              > what I mean by that.
              >
              > You haven't really posted enough of the code for us to try it for
              > ourselves.
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              > If replying to the group, please do not mail me too[/color]

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: BeginAccept doesn't work

                Nuno Magalhaes <nunommagalhaes @hotmail.com> wrote:[color=blue]
                > It's a really short program cause it's the beginning...
                >
                > The form: http://pwp.netcabo.pt/0141404701/RTSPServer.cs
                > The server: http://pwp.netcabo.pt/0141404701/TcpServer.cs
                >
                > If you could help me I would be really appreciated... doing an Accept
                > on TcpServer.cs does accept the client (like Internet Explorer
                > "http://localhost:555/") so it's not from any firewall or antivirus.[/color]

                Well, I'm not sure why there's a difference between Accept and
                BeginAccept, but if you change the socket address to IPAddress.Any, it
                works fine. I suspect the problem is that it's not listening on the
                loopback address if you just specify the actual network interface
                address.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Nick Hounsome

                  #9
                  Re: BeginAccept doesn't work


                  "Nuno Magalhaes" <nunommagalhaes @hotmail.com> wrote in message
                  news:1138407782 .167994.206560@ g43g2000cwa.goo glegroups.com.. .[color=blue]
                  >I understand that exceptions are very expensive but how can I, with
                  > Console.WriteLi ne, see it's output? In the debug window?
                  >[/color]

                  Use Debug.WriteLine and a program like DebugView
                  (http://www.sysinternals.com/Utilities/DebugView.html)

                  This will allow you to see the output whether or not you are running in the
                  debugger


                  Comment

                  • Nuno Magalhaes

                    #10
                    Re: BeginAccept doesn't work

                    It works now. It was the GC that was marking the server object to be
                    collected. It didn't detect that the callback was present.

                    Writing a bit more of my RTSP server solved this problem, in a simple
                    thread on RTSPServer.cs.

                    Jon wrote:[color=blue]
                    > Nuno Magalhaes <nunommagalhaes @hotmail.com> wrote:[color=green]
                    > > It's a really short program cause it's the beginning...
                    > >
                    > > The form: http://pwp.netcabo.pt/0141404701/RTSPServer.cs
                    > > The server: http://pwp.netcabo.pt/0141404701/TcpServer.cs
                    > >
                    > > If you could help me I would be really appreciated... doing an Accept
                    > > on TcpServer.cs does accept the client (like Internet Explorer
                    > > "http://localhost:555/") so it's not from any firewall or antivirus.[/color]
                    >
                    > Well, I'm not sure why there's a difference between Accept and
                    > BeginAccept, but if you change the socket address to IPAddress.Any, it
                    > works fine. I suspect the problem is that it's not listening on the
                    > loopback address if you just specify the actual network interface
                    > address.
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    > If replying to the group, please do not mail me too[/color]

                    Comment

                    • Willy Denoyette [MVP]

                      #11
                      Re: BeginAccept doesn't work

                      Funny, this is what I told you what happened to your object and what you
                      could do to solve this issue in the other thread of yours, but in that same
                      thread you said:
                      <
                      None of the solutions actually worked but creating a thread and
                      determining if the client is connected (through a function) worked now.[color=blue]
                      >[/color]
                      , mind to share what you did exactly?

                      Willy.

                      "Nuno Magalhaes" <nunommagalhaes @hotmail.com> wrote in message
                      news:1138447206 .481313.109580@ g44g2000cwa.goo glegroups.com.. .
                      | It works now. It was the GC that was marking the server object to be
                      | collected. It didn't detect that the callback was present.
                      |
                      | Writing a bit more of my RTSP server solved this problem, in a simple
                      | thread on RTSPServer.cs.
                      |
                      | Jon wrote:
                      | > Nuno Magalhaes <nunommagalhaes @hotmail.com> wrote:
                      | > > It's a really short program cause it's the beginning...
                      | > >
                      | > > The form: http://pwp.netcabo.pt/0141404701/RTSPServer.cs
                      | > > The server: http://pwp.netcabo.pt/0141404701/TcpServer.cs
                      | > >
                      | > > If you could help me I would be really appreciated... doing an Accept
                      | > > on TcpServer.cs does accept the client (like Internet Explorer
                      | > > "http://localhost:555/") so it's not from any firewall or antivirus.
                      | >
                      | > Well, I'm not sure why there's a difference between Accept and
                      | > BeginAccept, but if you change the socket address to IPAddress.Any, it
                      | > works fine. I suspect the problem is that it's not listening on the
                      | > loopback address if you just specify the actual network interface
                      | > address.
                      | >
                      | > --
                      | > Jon Skeet - <skeet@pobox.co m>
                      | > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                      | > If replying to the group, please do not mail me too
                      |


                      Comment

                      • Nuno Magalhaes

                        #12
                        Re: BeginAccept doesn't work

                        Yes, you were right. Using the object elsewhere in the code makes the
                        object unavailable for GC and the BeginAccept then works inside the
                        TcpServer object.

                        Willy Denoyette [MVP] wrote:[color=blue]
                        > Funny, this is what I told you what happened to your object and what you
                        > could do to solve this issue in the other thread of yours, but in that same
                        > thread you said:
                        > <
                        > None of the solutions actually worked but creating a thread and
                        > determining if the client is connected (through a function) worked now.[color=green]
                        > >[/color]
                        > , mind to share what you did exactly?
                        >
                        > Willy.
                        >
                        > "Nuno Magalhaes" <nunommagalhaes @hotmail.com> wrote in message
                        > news:1138447206 .481313.109580@ g44g2000cwa.goo glegroups.com.. .
                        > | It works now. It was the GC that was marking the server object to be
                        > | collected. It didn't detect that the callback was present.
                        > |
                        > | Writing a bit more of my RTSP server solved this problem, in a simple
                        > | thread on RTSPServer.cs.
                        > |
                        > | Jon wrote:
                        > | > Nuno Magalhaes <nunommagalhaes @hotmail.com> wrote:
                        > | > > It's a really short program cause it's the beginning...
                        > | > >
                        > | > > The form: http://pwp.netcabo.pt/0141404701/RTSPServer.cs
                        > | > > The server: http://pwp.netcabo.pt/0141404701/TcpServer.cs
                        > | > >
                        > | > > If you could help me I would be really appreciated... doing an Accept
                        > | > > on TcpServer.cs does accept the client (like Internet Explorer
                        > | > > "http://localhost:555/") so it's not from any firewall or antivirus.
                        > | >
                        > | > Well, I'm not sure why there's a difference between Accept and
                        > | > BeginAccept, but if you change the socket address to IPAddress.Any, it
                        > | > works fine. I suspect the problem is that it's not listening on the
                        > | > loopback address if you just specify the actual network interface
                        > | > address.
                        > | >
                        > | > --
                        > | > Jon Skeet - <skeet@pobox.co m>
                        > | > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                        > | > If replying to the group, please do not mail me too
                        > |[/color]

                        Comment

                        Working...