Connection is not closed

ClarkVent

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

B4X:
    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?
 

agraham

Expert
Licensed User
Longtime User
The Network client is .NET TcpClient. From "Network Programming for the Microsoft .NET Framework" P179.
TcpClient does not offer a method to shut down the connection, but the close method should be called when the connection is done to release the underlying network resources.
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
B4X:
Client.New1
Obj1.New1(False)
  ...
Client.Connect(....)
  ...
Obj1.FromLibrary("Main.Client", "m_DataStream", B4PObject(2))
Obj1.RunMethod("Close")
Client.Close
 

ClarkVent

Member
Licensed User
I'm getting a NullReferenceException error on this line:

B4X:
Obj1.FromLibrary("Main.Client", "m_DataStream", B4PObject(2))
 

agraham

Expert
Licensed User
Longtime User
I'm getting a NullReferenceException error on this line:
:signOops: Yes you will, I was looking at the wrong object :sign0013:

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

ClarkVent

Member
Licensed User
This didn't work:

B4X:
Obj1.RunMethod2("Close", 0, "System.Int32"))

This of course did:

B4X:
Obj1.RunMethod("Close")

That closed the connection perfectly! Thanks for your help! :)
 

ClarkVent

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

B4X:
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?
 

agraham

Expert
Licensed User
Longtime User
It always sends some (seemingly) random character first. Is this a bug?
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?
 

ClarkVent

Member
Licensed User
No, it's a length indicator, from the BinaryFile help "WriteString ... Writes a string prefixed with its length to the file."

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.
 
Top