Connection is not closed

Discussion in 'Questions (Windows Mobile)' started by ClarkVent, Nov 9, 2008.

  1. ClarkVent

    ClarkVent Member Licensed User

    I have the following piece of code which is called once every minute or so (inside a timer event):

    Code:
    client.New1
        client.Connect( RemoteHostIP, RemoteHostPort )
        BinaryFile.New1( client.GetStream, 
    True )
        BinaryFile.WriteString( SendString )
        client.Close
    This sends the contents of "SendString" (which is only about 80 bytes in size) to the server at "RemoteHostIP:RemoteHostPort" just fine. However, client.Close doesn't actually close the connection.

    So if I run the program for about 10 minutes, it will have opened 10 connections which are only closed when I actually exit the program.

    Why?
     
  2. agraham

    agraham Expert Licensed User

    The Network client is .NET TcpClient. From "Network Programming for the Microsoft .NET Framework" P179.
    Also The TcpClient Close method does not close the underlying TCP connection

    You could try this and see if it works. Obj1 is a Door library Object
    Code:
    Client.New1
    Obj1.New1(
    False)
      ...
    Client.Connect(....)
      ...
    Obj1.FromLibrary(
    "Main.Client""m_DataStream", B4PObject(2))
    Obj1.RunMethod(
    "Close")
    Client.Close
     
  3. ClarkVent

    ClarkVent Member Licensed User

    I'm getting a NullReferenceException error on this line:

    Code:
    Obj1.FromLibrary("Main.Client""m_DataStream", B4PObject(2))
     
  4. agraham

    agraham Expert Licensed User

    :signOops: Yes you will, I was looking at the wrong object :sign0013:

    Try this - we'll get there in the end!
    Code:
    Client.New1
    Obj1.New1(
    False)
      ...
    Client.Connect(....)
      ...
    Obj1.Value = Client.GetStream
    Obj1.RunMethod2(
    "Close"0"System.Int32"))
    Client.Close
     
  5. ClarkVent

    ClarkVent Member Licensed User

    This didn't work:

    Code:
    Obj1.RunMethod2("Close"0"System.Int32"))
    This of course did:

    Code:
    Obj1.RunMethod("Close")
    That closed the connection perfectly! Thanks for your help! :)
     
  6. ClarkVent

    ClarkVent Member Licensed User

    On another note (and slightly off topic), this code:

    Code:
    client.New1
    client.Connect( RemoteHostIP, RemoteHostPort )
    BinaryFile.New1( client.GetStream, 
    True )
    BinaryFile.WriteString( 
    "Test" )
    client.Close
    doesn't actually send "Test" to the remote host, but "<some character>Test". It always sends some (seemingly) random character first. Is this a bug?
     
  7. agraham

    agraham Expert Licensed User

    No, it's a length indicator, from the BinaryFile help "WriteString ... Writes a string prefixed with its length to the file." Binary file can mix types in the stream so for those that don't have a fixed length like a string a count indicator is needed so that the end of the string can be determined. This is fine when you have a BinaryFile at each end but for sending arbitrary data to a server the additional characters are a nuisance.

    Time to ask other people how they manage it as I do very little Network stuff passing real data around. Anybody?
     
  8. ClarkVent

    ClarkVent Member Licensed User

    I guess I should RTFM more often... ;)

    Anyway, what I eventually did was convert the string to a byte array and send it using WriteBytes2() instead.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice