asynchronous socket problem when connecting to localhost

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • darthghandi@gmail.com

    asynchronous socket problem when connecting to localhost

    I am trying to create a server application using asynchronous
    sockets. I run into a problem when I try to connect to my server
    using a non-.net program. I can establish the connection, and send
    some packets in response to the programs first message, but when I
    receive a response back, it's the last packet I sent. After that
    packet, I receive the packet I really wanted. This only happens when
    I connect with local host. I tried connecting with that same program
    from another computer, and there is no problem. Any ideas?
    Thanks,
    Chris

    Here's some of the code:
    public void Bind(int l_port)
    {;
    m_port = l_port;
    IPHostEntry ipHostInfo = Dns.GetHostEntr y("");
    IPAddress ipAddress = ipHostInfo.Addr essList[1];
    //m_tcpEndPoint = new IPEndPoint(IPAd dress.Any, m_port);
    m_tcpEndPoint = new IPEndPoint(ipAd dress, m_port);
    m_mainSocket = new Socket(AddressF amily.InterNetw ork,
    SocketType.Stre am, ProtocolType.Tc p);
    m_mainSocket.Bi nd(m_tcpEndPoin t);
    m_mainSocket.Li sten(100);
    m_callBack = new AsyncCallback(O nClientConnect) ;
    m_mainSocket.Be ginAccept(m_cal lBack, m_mainSocket);
    }
    public void OnClientConnect (IAsyncResult ar)
    {
    try
    {
    Socket stateOfRecieveS ock = (Socket)ar.Asyn cState;
    m_workSocks[sockCount] =
    stateOfRecieveS ock.EndAccept(a r);
    OnSend(m_workSo cks[sockCount]);
    sockCount++;
    AsyncCallback recievedData = new
    AsyncCallback(O nClientConnect) ;
    stateOfRecieveS ock.BeginAccept (recievedData,
    stateOfRecieveS ock);
    }
    catch (Exception ex)
    {
    throw new Exception("Ther e was a problem receiving
    data.", ex);
    }
    }
    //SendMessage is called after receiving a packet from the
    client and sending one as well

    public void SendMessage(IOB uffer stateOfTheSock)
    {
    if (stateOfTheSock .TheSocket.Conn ected)
    {
    //add data to the buffer
    AsyncCallback callMe = new
    AsyncCallback(G otMoreData);

    stateOfTheSock. TheSocket.Begin Send(stateOfThe Sock.TheBuffer, 0,
    stateOfTheSock. TheBuffer.Lengt h, SocketFlags.Non e, callMe,
    stateOfTheSock) ;
    }
    public void GotMoreData(IAs yncResult ar)
    {
    IOBuffer moreSocks = (IOBuffer)ar.As yncState;
    if (moreSocks.TheS ocket.Connected )
    {
    moreSocks.Bytes Received =
    moreSocks.TheSo cket.EndReceive (ar);
    moreSocks.Clear Buffer();
    AsyncCallback more = new AsyncCallback(G otMoreData);
    moreSocks.TheSo cket.BeginRecei ve(moreSocks.Th eBuffer,
    0, moreSocks.TheBu ffer.Length, SocketFlags.Non e, more, moreSocks);
    }
    }

  • Michael Rubinstein

    #2
    Re: asynchronous socket problem when connecting to localhost

    Chris, server talking to itself?
    You server starts sending right after accepting connection. It is
    unusual.Normall y a client would send something upon connection to indicate
    it is ready, the server would respond after receiving some data. This will
    also ensure the packet goes to a correct client.

    Michael

    <darthghandi@gm ail.comwrote in message
    news:1170531749 .150385.275240@ s48g2000cws.goo glegroups.com.. .
    >I am trying to create a server application using asynchronous
    sockets. I run into a problem when I try to connect to my server
    using a non-.net program. I can establish the connection, and send
    some packets in response to the programs first message, but when I
    receive a response back, it's the last packet I sent. After that
    packet, I receive the packet I really wanted. This only happens when
    I connect with local host. I tried connecting with that same program
    from another computer, and there is no problem. Any ideas?
    Thanks,
    Chris
    >
    Here's some of the code:
    public void Bind(int l_port)
    {;
    m_port = l_port;
    IPHostEntry ipHostInfo = Dns.GetHostEntr y("");
    IPAddress ipAddress = ipHostInfo.Addr essList[1];
    //m_tcpEndPoint = new IPEndPoint(IPAd dress.Any, m_port);
    m_tcpEndPoint = new IPEndPoint(ipAd dress, m_port);
    m_mainSocket = new Socket(AddressF amily.InterNetw ork,
    SocketType.Stre am, ProtocolType.Tc p);
    m_mainSocket.Bi nd(m_tcpEndPoin t);
    m_mainSocket.Li sten(100);
    m_callBack = new AsyncCallback(O nClientConnect) ;
    m_mainSocket.Be ginAccept(m_cal lBack, m_mainSocket);
    }
    public void OnClientConnect (IAsyncResult ar)
    {
    try
    {
    Socket stateOfRecieveS ock = (Socket)ar.Asyn cState;
    m_workSocks[sockCount] =
    stateOfRecieveS ock.EndAccept(a r);
    OnSend(m_workSo cks[sockCount]);
    sockCount++;
    AsyncCallback recievedData = new
    AsyncCallback(O nClientConnect) ;
    stateOfRecieveS ock.BeginAccept (recievedData,
    stateOfRecieveS ock);
    }
    catch (Exception ex)
    {
    throw new Exception("Ther e was a problem receiving
    data.", ex);
    }
    }
    //SendMessage is called after receiving a packet from the
    client and sending one as well
    >
    public void SendMessage(IOB uffer stateOfTheSock)
    {
    if (stateOfTheSock .TheSocket.Conn ected)
    {
    //add data to the buffer
    AsyncCallback callMe = new
    AsyncCallback(G otMoreData);
    >
    stateOfTheSock. TheSocket.Begin Send(stateOfThe Sock.TheBuffer, 0,
    stateOfTheSock. TheBuffer.Lengt h, SocketFlags.Non e, callMe,
    stateOfTheSock) ;
    }
    public void GotMoreData(IAs yncResult ar)
    {
    IOBuffer moreSocks = (IOBuffer)ar.As yncState;
    if (moreSocks.TheS ocket.Connected )
    {
    moreSocks.Bytes Received =
    moreSocks.TheSo cket.EndReceive (ar);
    moreSocks.Clear Buffer();
    AsyncCallback more = new AsyncCallback(G otMoreData);
    moreSocks.TheSo cket.BeginRecei ve(moreSocks.Th eBuffer,
    0, moreSocks.TheBu ffer.Length, SocketFlags.Non e, more, moreSocks);
    }
    }
    >

    Comment

    • darthghandi@gmail.com

      #3
      Re: asynchronous socket problem when connecting to localhost

      On Feb 4, 6:43 am, "Michael Rubinstein"
      <mSPAM_REMOVEr@ m®ubinstein.com wrote:
      Chris, server talking to itself?
      You server starts sending right after accepting connection. It is
      unusual.Normall y a client would send something upon connection to indicate
      it is ready, the server would respond after receiving some data. This will
      also ensure the packet goes to a correct client.
      >
      Michael
      >
      <darthgha...@gm ail.comwrote in message
      >
      news:1170531749 .150385.275240@ s48g2000cws.goo glegroups.com.. .
      >
      I am trying to create a server application using asynchronous
      sockets. I run into a problem when I try to connect to my server
      using a non-.net program. I can establish the connection, and send
      some packets in response to the programs first message, but when I
      receive a response back, it's the last packet I sent. After that
      packet, I receive the packet I really wanted. This only happens when
      I connect with local host. I tried connecting with that same program
      from another computer, and there is no problem. Any ideas?
      Thanks,
      Chris
      >
      Here's some of the code:
      public void Bind(int l_port)
      {;
      m_port = l_port;
      IPHostEntry ipHostInfo = Dns.GetHostEntr y("");
      IPAddress ipAddress = ipHostInfo.Addr essList[1];
      //m_tcpEndPoint = new IPEndPoint(IPAd dress.Any, m_port);
      m_tcpEndPoint = new IPEndPoint(ipAd dress, m_port);
      m_mainSocket = new Socket(AddressF amily.InterNetw ork,
      SocketType.Stre am, ProtocolType.Tc p);
      m_mainSocket.Bi nd(m_tcpEndPoin t);
      m_mainSocket.Li sten(100);
      m_callBack = new AsyncCallback(O nClientConnect) ;
      m_mainSocket.Be ginAccept(m_cal lBack, m_mainSocket);
      }
      public void OnClientConnect (IAsyncResult ar)
      {
      try
      {
      Socket stateOfRecieveS ock = (Socket)ar.Asyn cState;
      m_workSocks[sockCount] =
      stateOfRecieveS ock.EndAccept(a r);
      OnSend(m_workSo cks[sockCount]);
      sockCount++;
      AsyncCallback recievedData = new
      AsyncCallback(O nClientConnect) ;
      stateOfRecieveS ock.BeginAccept (recievedData,
      stateOfRecieveS ock);
      }
      catch (Exception ex)
      {
      throw new Exception("Ther e was a problem receiving
      data.", ex);
      }
      }
      //SendMessage is called after receiving a packet from the
      client and sending one as well
      >
      public void SendMessage(IOB uffer stateOfTheSock)
      {
      if (stateOfTheSock .TheSocket.Conn ected)
      {
      //add data to the buffer
      AsyncCallback callMe = new
      AsyncCallback(G otMoreData);
      >
      stateOfTheSock. TheSocket.Begin Send(stateOfThe Sock.TheBuffer, 0,
      stateOfTheSock. TheBuffer.Lengt h, SocketFlags.Non e, callMe,
      stateOfTheSock) ;
      }
      public void GotMoreData(IAs yncResult ar)
      {
      IOBuffer moreSocks = (IOBuffer)ar.As yncState;
      if (moreSocks.TheS ocket.Connected )
      {
      moreSocks.Bytes Received =
      moreSocks.TheSo cket.EndReceive (ar);
      moreSocks.Clear Buffer();
      AsyncCallback more = new AsyncCallback(G otMoreData);
      moreSocks.TheSo cket.BeginRecei ve(moreSocks.Th eBuffer,
      0, moreSocks.TheBu ffer.Length, SocketFlags.Non e, more, moreSocks);
      }
      }
      That was my first thought as well. I am trying to create a server for
      an already built client (weird I know, but it's for school and I just
      do what I'm told). I tried making the server complete the TCP
      connection and then wait for the client to send what it's supposed to
      send, but I just keep waiting for it. That's when I tried to send the
      info I'm supposed to send first, then listen for the response. That
      works at first, then I run into the problem listed above. Maybe I'll
      try sending one then waiting for another before I send one again.
      We'll see if that works.
      On another note, does anyone know how to listen on localhost and all
      the given IP's all at once? The only way I have found to do this is
      to use IPAdress.Any, but I heard that this wasn't secure. Don't know
      much about that. If anyone has any info an that, please share.
      Thanks,
      Chris

      Comment

      • Michael Rubinstein

        #4
        Re: asynchronous socket problem when connecting to localhost

        Chris, if your client is not correctly written, then there is so much
        you can do. The TCP connection is established once the client receives the
        CallBack from BeginConnect() or whatever Winsock code it uses. At that point
        the client can issue Send() or BeginSend().
        >
        On another note, does anyone know how to listen on localhost and all
        the given IP's all at once? The only way I have found to do this is
        to use IPAdress.Any, but I heard that this wasn't secure.
        >
        If you are concerned about security then the firewall is a better place to
        address it.

        Good luck, Michael


        <darthghandi@gm ail.comwrote in message
        news:1170821995 .249611.115500@ p10g2000cwp.goo glegroups.com.. .
        On Feb 4, 6:43 am, "Michael Rubinstein"
        <mSPAM_REMOVEr@ m®ubinstein.com wrote:
        Chris, server talking to itself?
        You server starts sending right after accepting connection. It is
        unusual.Normall y a client would send something upon connection to indicate
        it is ready, the server would respond after receiving some data. This will
        also ensure the packet goes to a correct client.
        >
        Michael
        >
        <darthgha...@gm ail.comwrote in message
        >
        news:1170531749 .150385.275240@ s48g2000cws.goo glegroups.com.. .
        >
        I am trying to create a server application using asynchronous
        sockets. I run into a problem when I try to connect to my server
        using a non-.net program. I can establish the connection, and send
        some packets in response to the programs first message, but when I
        receive a response back, it's the last packet I sent. After that
        packet, I receive the packet I really wanted. This only happens when
        I connect with local host. I tried connecting with that same program
        from another computer, and there is no problem. Any ideas?
        Thanks,
        Chris
        >
        Here's some of the code:
        public void Bind(int l_port)
        {;
        m_port = l_port;
        IPHostEntry ipHostInfo = Dns.GetHostEntr y("");
        IPAddress ipAddress = ipHostInfo.Addr essList[1];
        //m_tcpEndPoint = new IPEndPoint(IPAd dress.Any, m_port);
        m_tcpEndPoint = new IPEndPoint(ipAd dress, m_port);
        m_mainSocket = new Socket(AddressF amily.InterNetw ork,
        SocketType.Stre am, ProtocolType.Tc p);
        m_mainSocket.Bi nd(m_tcpEndPoin t);
        m_mainSocket.Li sten(100);
        m_callBack = new AsyncCallback(O nClientConnect) ;
        m_mainSocket.Be ginAccept(m_cal lBack, m_mainSocket);
        }
        public void OnClientConnect (IAsyncResult ar)
        {
        try
        {
        Socket stateOfRecieveS ock = (Socket)ar.Asyn cState;
        m_workSocks[sockCount] =
        stateOfRecieveS ock.EndAccept(a r);
        OnSend(m_workSo cks[sockCount]);
        sockCount++;
        AsyncCallback recievedData = new
        AsyncCallback(O nClientConnect) ;
        stateOfRecieveS ock.BeginAccept (recievedData,
        stateOfRecieveS ock);
        }
        catch (Exception ex)
        {
        throw new Exception("Ther e was a problem receiving
        data.", ex);
        }
        }
        //SendMessage is called after receiving a packet from the
        client and sending one as well
        >
        public void SendMessage(IOB uffer stateOfTheSock)
        {
        if (stateOfTheSock .TheSocket.Conn ected)
        {
        //add data to the buffer
        AsyncCallback callMe = new
        AsyncCallback(G otMoreData);
        >
        stateOfTheSock. TheSocket.Begin Send(stateOfThe Sock.TheBuffer, 0,
        stateOfTheSock. TheBuffer.Lengt h, SocketFlags.Non e, callMe,
        stateOfTheSock) ;
        }
        public void GotMoreData(IAs yncResult ar)
        {
        IOBuffer moreSocks = (IOBuffer)ar.As yncState;
        if (moreSocks.TheS ocket.Connected )
        {
        moreSocks.Bytes Received =
        moreSocks.TheSo cket.EndReceive (ar);
        moreSocks.Clear Buffer();
        AsyncCallback more = new AsyncCallback(G otMoreData);
        moreSocks.TheSo cket.BeginRecei ve(moreSocks.Th eBuffer,
        0, moreSocks.TheBu ffer.Length, SocketFlags.Non e, more, moreSocks);
        }
        }
        That was my first thought as well. I am trying to create a server for
        an already built client (weird I know, but it's for school and I just
        do what I'm told). I tried making the server complete the TCP
        connection and then wait for the client to send what it's supposed to
        send, but I just keep waiting for it. That's when I tried to send the
        info I'm supposed to send first, then listen for the response. That
        works at first, then I run into the problem listed above. Maybe I'll
        try sending one then waiting for another before I send one again.
        We'll see if that works.
        On another note, does anyone know how to listen on localhost and all
        the given IP's all at once? The only way I have found to do this is
        to use IPAdress.Any, but I heard that this wasn't secure. Don't know
        much about that. If anyone has any info an that, please share.
        Thanks,
        Chris


        Comment

        • Peter Duniho

          #5
          Re: asynchronous socket problem when connecting to localhost

          On Tue, 06 Feb 2007 20:19:55 -0800, <darthghandi@gm ail.comwrote:
          [...]
          On another note, does anyone know how to listen on localhost and all
          the given IP's all at once? The only way I have found to do this is
          to use IPAdress.Any, but I heard that this wasn't secure. Don't know
          much about that. If anyone has any info an that, please share.
          I don't see why using "any" for your IP address is inherently any less
          secure than using a specific IP address. And it's the only way for you to
          allow a single socket to accept connections on more than one IP address.

          Comment

          • Chris Mullins [MVP]

            #6
            Re: asynchronous socket problem when connecting to localhost

            "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
            news:op.tnej4id p8jd0ej@petes-computer.local. ..
            On Tue, 06 Feb 2007 20:19:55 -0800, <darthghandi@gm ail.comwrote:
            >[...]
            >On another note, does anyone know how to listen on localhost and all
            >the given IP's all at once? The only way I have found to do this is
            >to use IPAdress.Any, but I heard that this wasn't secure. Don't know
            >much about that. If anyone has any info an that, please share.
            >
            I don't see why using "any" for your IP address is inherently any less
            secure than using a specific IP address. And it's the only way for you to
            allow a single socket to accept connections on more than one IP address.
            I agree - the only time using "Any" (0.0.0.0) would be insecure is when one
            of your IP Addresses is considered insecure. For example, often I want to
            start a service process up (say, IIS for testing) and have it ONLY listen on
            127.0.0.1 - this helps mitigate the threat surface.

            If I have IIS listen on my main ip (192.168.100.10 0, say), then I'm now
            allowing anyone to come attack it.

            If the goal is to have my socket app listen on all sockets, then there's no
            security difference that I can see between binding to the "any" address, or
            enumerating the addresses and binding to each invividually.

            --
            Chris Mullins, MCSD.NET, MCPD:Enterprise , MVP C#



            Comment

            • darthghandi@gmail.com

              #7
              Re: asynchronous socket problem when connecting to localhost

              On Feb 8, 12:19 pm, "Chris Mullins [MVP]" <cmull...@yahoo .comwrote:
              "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote in message
              >
              news:op.tnej4id p8jd0ej@petes-computer.local. ..
              >
              On Tue, 06 Feb 2007 20:19:55 -0800, <darthgha...@gm ail.comwrote:
              [...]
              On another note, does anyone know how to listen on localhost and all
              the given IP's all at once? The only way I have found to do this is
              to use IPAdress.Any, but I heard that this wasn't secure. Don't know
              much about that. If anyone has any info an that, please share.
              >
              I don't see why using "any" for your IP address is inherently any less
              secure than using a specific IP address. And it's the only way for you to
              allow a single socket to accept connections on more than one IP address.
              >
              I agree - the only time using "Any" (0.0.0.0) would be insecure is when one
              of your IP Addresses is considered insecure. For example, often I want to
              start a service process up (say, IIS for testing) and have it ONLY listen on
              127.0.0.1 - this helps mitigate the threat surface.
              >
              If I have IIS listen on my main ip (192.168.100.10 0, say), then I'm now
              allowing anyone to come attack it.
              >
              If the goal is to have my socket app listen on all sockets, then there's no
              security difference that I can see between binding to the "any" address, or
              enumerating the addresses and binding to each invividually.
              >
              --
              Chris Mullins, MCSD.NET, MCPD:Enterprise , MVP C#http://www.coversant.net/blogs/cmullins
              Thanks for the help. I solved the problem by sending ignored data
              inbetween recieves. I was just wondering if anyone knew of anything I
              should look out for when doing this.
              Thanks,

              Comment

              • Peter Duniho

                #8
                Re: asynchronous socket problem when connecting to localhost

                On Sat, 10 Feb 2007 10:08:20 -0800, <darthghandi@gm ail.comwrote:
                Thanks for the help. I solved the problem by sending ignored data
                inbetween recieves. I was just wondering if anyone knew of anything I
                should look out for when doing this.
                Yes. Sending extra "ignored data" is a hack and there's no reason you
                should need to do it. If you do, it means you have an unsolved bug in
                your own code. Adding a new bug to your code in order to avoid a
                different bug already in your code is not a good programming practice.

                Pete

                Comment

                • darthghandi@gmail.com

                  #9
                  Re: asynchronous socket problem when connecting to localhost

                  On Feb 10, 12:44 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                  wrote:
                  On Sat, 10 Feb 2007 10:08:20 -0800, <darthgha...@gm ail.comwrote:
                  Thanks for the help. I solved the problem by sending ignored data
                  inbetween recieves. I was just wondering if anyone knew of anything I
                  should look out for when doing this.
                  >
                  Yes. Sending extra "ignored data" is a hack and there's no reason you
                  should need to do it. If you do, it means you have an unsolved bug in
                  your own code. Adding a new bug to your code in order to avoid a
                  different bug already in your code is not a good programming practice.
                  >
                  Pete
                  After many moons, I found my unsolved bug. It seems the socket had
                  already given me the packet I was looking for combined with the
                  earlier packet. I had (wrongly) assumed that when I did a
                  BeginRecieve(), I would just get one message in the buffer instead of
                  two. Now I am rewriting the code to support multiple messages in one
                  buffer.
                  Thanks again for your time,
                  Chris

                  Comment

                  • JR

                    #10
                    Re: asynchronous socket problem when connecting to localhost

                    Sockets send and receive bytes, not messages. This is a common mistake. The
                    receiver needs to split the byte stream into messages.

                    In practive, for shorter messages with some delay between them, you will
                    often receive complete messages each time.

                    JR

                    <darthghandi@gm ail.com???
                    ??????:11716882 93.154328.27950 0@l53g2000cwa.g ooglegroups.com ...
                    On Feb 10, 12:44 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                    wrote:
                    >On Sat, 10 Feb 2007 10:08:20 -0800, <darthgha...@gm ail.comwrote:
                    Thanks for the help. I solved the problem by sending ignored data
                    inbetween recieves. I was just wondering if anyone knew of anything I
                    should look out for when doing this.
                    >>
                    >Yes. Sending extra "ignored data" is a hack and there's no reason you
                    >should need to do it. If you do, it means you have an unsolved bug in
                    >your own code. Adding a new bug to your code in order to avoid a
                    >different bug already in your code is not a good programming practice.
                    >>
                    >Pete
                    >
                    After many moons, I found my unsolved bug. It seems the socket had
                    already given me the packet I was looking for combined with the
                    earlier packet. I had (wrongly) assumed that when I did a
                    BeginRecieve(), I would just get one message in the buffer instead of
                    two. Now I am rewriting the code to support multiple messages in one
                    buffer.
                    Thanks again for your time,
                    Chris
                    >

                    Comment

                    Working...