Real Software Forums
http://forums.realsoftware.com/

TCPsocket question
http://forums.realsoftware.com/viewtopic.php?f=2&t=46730
Page 1 of 1

Author:  jpkelly [ Mon Jan 28, 2013 12:25 pm ]
Post subject:  TCPsocket question

I have a piece of equipment which can be controlled by ASCII commands. I have set up a TCPSocket to send data to the device with the following code:

Dim s as TCPSocket
s = New TCPSocket
s.port = val(portField.Text)
s.address = addressField.Text

//Make the call
const timeOut = 5000000
dim toStart as double

toStart = Microseconds
s.Connect

Do
s.Poll
Loop Until (s.IsConnected) or (Microseconds - toStart > timeOut)

If s.IsConnected Then
s.Write sendText.Text + (chr(13)+chr(10))

ListBox1.AddRow ("Connected - Remote Address: " + s.RemoteAddress)

ListBox1.AddRow ("Handle: " + str(s.Handle))

ListBox1.AddRow ("Socket Error: " + str(s.LastErrorCode))

else
Print "Call failed to connect within " + cstr(timeOut / 1000000) + " seconds."
ListBox1.AddRow ("Call failed to connect within " + cstr(timeOut / 1000000) + " seconds.")
spinner.Visible = false
connected = false
ListBox1.AddRow ("Socket Error: " + str(s.LastErrorCode))
s.Write "not connected..." + (chr(13)+chr(10))
s.Disconnect

End If


The code connects and sends the data and stays connected.

I have a disconnect button with the following code:

if s.IsConnected then
s.Disconnect
Print "Disconnecting"
else
Print "can't disconnect, not connected"
end if


When clicking the Disconnect button the program hangs on the line
if s.IsConnected then

on resuming throws a Nil object error.

Clearly I am unclear on the concept here.

Why can't I use methods for the s.object outside of the code that created it?
If the Socket is still connected does the object/socket exist?

How can I reference the TCPSocket object methods like s.Write or s.Disconnect?

Author:  timhare [ Mon Jan 28, 2013 3:40 pm ]
Post subject:  Re: TCPsocket question

Quote:
Dim s as TCPSocket

This creates an object that is local to the method it is in. It is not accessible from anywhere else, such as a disconnect button. Make the socket a property of the window so you can access it from multiple places in your code.

Author:  jpkelly [ Mon Jan 28, 2013 4:07 pm ]
Post subject:  Re: TCPsocket question

Thanks for the help, that worked!

What is the best practice for connecting/sending data to an external device?
Is it good or bad to leave the connection open?
This is a situation where commands need to be sent on demand and no other connections will likely be made to the device.
I realize this a rather vague question but any input will be appreciated.

Author:  wpurvis [ Mon Jan 28, 2013 4:31 pm ]
Post subject:  Re: TCPsocket question

jpkelly wrote:
Thanks for the help, that worked!

What is the best practice for connecting/sending data to an external device?
Is it good or bad to leave the connection open?
This is a situation where commands need to be sent on demand and no other connections will likely be made to the device.
I realize this a rather vague question but any input will be appreciated.


In general, it's neither good nor bad to leave a connection open. It's fine. You'll have to handle the situation where the device has closed the connection, and reconnect, but that's no problem.

Author:  timhare [ Mon Jan 28, 2013 5:00 pm ]
Post subject:  Re: TCPsocket question

If you use a synchronous approach, as you have, then it's best to open and close the connection each time - handle all the details in one place. If you go async, and put your code in the socket's events, then it's better to leave the socket open. But that's just my opinion based on reliability vs. overhead.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/