Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Connection is not closed

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-09-2008, 12:51 AM
Junior Member
 
Join Date: Nov 2007
Posts: 18
Default Connection is not closed

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?
Reply With Quote
  #2 (permalink)  
Old 11-09-2008, 10:29 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

The Network client is .NET TcpClient. From "Network Programming for the Microsoft .NET Framework" P179.
Quote:
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
Code:
Client.New1
Obj1.New1(
False)
  ...
Client.Connect(....)
  ...
Obj1.FromLibrary(
"Main.Client""m_DataStream", B4PObject(2))
Obj1.RunMethod(
"Close")
Client.Close
Reply With Quote
  #3 (permalink)  
Old 11-09-2008, 10:18 PM
Junior Member
 
Join Date: Nov 2007
Posts: 18
Default

I'm getting a NullReferenceException error on this line:

Code:
Obj1.FromLibrary("Main.Client""m_DataStream", B4PObject(2))
Reply With Quote
  #4 (permalink)  
Old 11-10-2008, 08:17 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by ClarkVent View Post
I'm getting a NullReferenceException error on this line:
Yes you will, I was looking at the wrong object

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
Reply With Quote
  #5 (permalink)  
Old 11-10-2008, 10:57 AM
Junior Member
 
Join Date: Nov 2007
Posts: 18
Default

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!
Reply With Quote
  #6 (permalink)  
Old 11-10-2008, 11:01 AM
Junior Member
 
Join Date: Nov 2007
Posts: 18
Default

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?
Reply With Quote
  #7 (permalink)  
Old 11-10-2008, 11:28 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by ClarkVent View Post
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?
Reply With Quote
  #8 (permalink)  
Old 11-10-2008, 02:33 PM
Junior Member
 
Join Date: Nov 2007
Posts: 18
Default

Quote:
Originally Posted by agraham View Post
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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect ActiveSync connection Lasse Questions (Windows Mobile) 2 07-27-2008 08:51 AM
Connection between different socket mircome Questions (Windows Mobile) 3 07-19-2008 11:05 AM
Force Data Connection #777 WM6.0 Jamie Questions (Windows Mobile) 2 03-03-2008 03:47 PM
hold on a GPRS-connection schimanski Questions (Windows Mobile) 3 01-15-2008 04:48 PM
Application does not end until msgbox is closed Mistrel Bug Reports 0 10-13-2007 06:26 AM


All times are GMT. The time now is 03:10 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0