New topic: 

TCPsocket question

<http://forums.realsoftware.com/viewtopic.php?t=46730>

         Page 1 of 1
   [ 5 posts ]                 Previous topic | Next topic          Author  
Message        jpkelly          Post subject: TCPsocket questionPosted: Mon Jan 
28, 2013 12:25 pm                         
Joined: Sun Jan 27, 2013 9:50 pm
Posts: 2                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?  
 
                             Top                timhare          Post subject: 
Re: TCPsocket questionPosted: Mon Jan 28, 2013 3:40 pm                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12034
Location: Portland, OR  USA                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.  
 
                             Top                jpkelly          Post subject: 
Re: TCPsocket questionPosted: Mon Jan 28, 2013 4:07 pm                         
Joined: Sun Jan 27, 2013 9:50 pm
Posts: 2                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.   
                             Top                wpurvis          Post subject: 
Re: TCPsocket questionPosted: Mon Jan 28, 2013 4:31 pm                         
Joined: Tue Oct 04, 2005 10:55 am
Posts: 36
Location: Fort Myers, FL                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.   
                             Top                timhare          Post subject: 
Re: TCPsocket questionPosted: Mon Jan 28, 2013 5:00 pm                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12034
Location: Portland, OR  USA                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.   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 5 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to