Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Nov 23, 2017 9:09 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: TCP Socket Write and Listen
PostPosted: Fri Dec 14, 2012 9:25 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
Hello,

Network novice question.

I want to control application which supports commands over TCP.
After command was sent I need to read result on same port.

Need I use 2 TCPSockets or can I use 1 for Write and Listen ?

Example : I need to send string "STATUS" to app on TCP port 7001 and need to receive results on same port.

Code examples are welcome.

Thanks

Martin

_________________
---

@MacOS 10.7.5/MBPro


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Fri Dec 14, 2012 9:29 am 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
If it's the same port then you only need one socket.

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Fri Dec 14, 2012 10:21 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
Thanks Bob,

It does not work for me. Bellow is my workflow.

I have set TCPsocket with Address 192.168.x.1 and port 7001 where 192.168.x.1 is PC with app which I need to control over TCP.
Computer with MyApp is on 192.168.x.2
on start of app I use Socket.Connect
I am sending command with Socket.Write("Command")
And on Event Socket.DataAvailable I have code to show buffer: TextField.AppendText (Socket.ReadAll))

Is it way how it should work ?
Need i use Listen Method ?
Need I set my IP somewhere ?

Thanks

Martin

_________________
---

@MacOS 10.7.5/MBPro


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Fri Dec 14, 2012 12:08 pm 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
I guess the first question to ask is do you ever really connect to the other PC? Do you have anything in the Connected Event http://docs.realsoftware.com/index.php/ ... .Connected or in the Error event http://docs.realsoftware.com/index.php/SocketCore.Error?

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Sat Dec 15, 2012 5:43 am 
Offline
User avatar

Joined: Fri Oct 28, 2005 7:05 am
Posts: 565
Location: Emsworth, UK
mapmusic wrote:
Network novice question.
...
Need I use 2 TCPSockets or can I use 1 for Write and Listen ?


I find the best way to teach people how to use TCP is to ask them to think of it like a telephone conversation

Where telephones allow humans to communicate, TCP Sockets allow applications to communicate.
+ A TCP socket is a virtual telephone.
+ IP addresses and ports are 'just' the virtual phone numbers for calling these virtual telephones.
+ An IP is the number for the host device (PC) and a Port identifies a particular socket on the device.

So, at the far end we have an application which is 'on-hook' and listening for a call.
At the local end we have an application which wants to communicate with the far end.
The local end picks up it's virtual phone (socket) and dials the far end (IP and Port), which it does by calling .connect.
Providing the number is right, the network does it's stuff and the call reaches the application listening at the far end.
The far end hears it's virtual phone ringing, picks up and the near and far end applications are connected.

Just like a phone call;
+ A call may fail to connect for a number of different reasons.
+ It takes time for the network to connect the call.
+ Once connected, both applications can talk to the other, while they remain connected.
+ Both applications must speak the same language (protocol).


Quote:
Example : I need to send string "STATUS" to app on TCP port 7001 and need to receive results on same port.
Code examples are welcome.


This is off the top of my head, so it may contain typos
//done for you when you add a TCP Socket to a form
Dim Socket1 as New TCPSocket

// populate the events we want to respond to
Sub Socket1.Error
If me.LastErrorCode = 102 then
Listbox1.Addrow "Socket disconnected"
Else
Listbox1.AddRow ("Socket Error: " + cstr(me.LastErrorCode)
End If
End Sub

Sub Socket1.Connected
Listbox1.AddRow ("Connected to host " + me.RemoteAddress)
Listbox1.AddRow ("Saying 'STATUS'")
me.Write "STATUS"
End Sub

Sub Socket1.DataAvailable
Dim data As String
While me.BytesAvailable > 0
data = data + me.ReadAll
Wend
Listbox1.Addrow "Received '" + data + "'"
End Sub

//Initiate a call
Sub Button1.Action
const remoteHost = "192.168.0.1"
const remoteIsListeningOnPort = 7001

//The 'number' of the application we want to call
Socket1.Address = remoteHost
Socket1.Port = remoteIsListeningOnPort

ListBox1.AddRow ("Calling " + me.Address + ":" + cstr(me.Port))

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

toStart = Microseconds
Socket1.Connect
Do
Socket1.Poll
Loop Until (Socket1.IsConnected) or (Microseconds - toStart > timeOut)

If Not Socket1.IsConnected Then
ListBox1.Addrow("Call failed to connect within " + cstr(timeOut / 1000000) + " seconds.")
End If
End Sub


Now remember what I said about both applications needing to speak the same language. Most application protocols require some sort of delimiter. Experience leads me to think you may need to send "STATUS" + EndOfLine to get the far end to respond.

HTH

_________________
Yes it's me in the avatar


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Mon Dec 17, 2012 4:10 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
Hello msssltd !

Thanks for last words in your reply...

The trick was TCPCommand + EndOfLine.Windows

I used only CHR (13) and Windows EOL is Chr(13)+Chr(10).

Again Many thanks... :D

_________________
---

@MacOS 10.7.5/MBPro


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Mon Dec 17, 2012 8:21 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
Just Question about new socket objects...

If I want to create new TCPSocket dynamically via commands instead of drag to project...
Dim Socket1 as New TCPSocket
Sub Socket1.Error
...code...
End Sub


How to declare methods (Subs) like Socket1. Error, DataAvailable etc... in code for dynamically created objects ?


Thanks a lot

_________________
---

@MacOS 10.7.5/MBPro


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Mon Dec 17, 2012 9:30 am 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
Use AddHandler. http://docs.realsoftware.com/index.php/AddHandler

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket Write and Listen
PostPosted: Tue Dec 18, 2012 2:33 am 
Offline
User avatar

Joined: Fri Oct 28, 2005 7:05 am
Posts: 565
Location: Emsworth, UK
Quote:
How to declare methods (Subs) like Socket1. Error, DataAvailable etc... in code for dynamically created objects ?

Subclassing is your friend.

You can create a custom control by;
1. Add a new class to the project.
2. Give it a name like MyUDPSocket.
3. Set the Super to UDPSocket.
4. Double click your new object and a code editor opens.
5. Populate the .Error and .DataAvailable event handlers as you like.

You create a new instance of your new MyUDPSocket class at runtime using
Dim MyUDPSocket1 as new MyUDPSocket

Every new instance of your custom control has the default behaviour (you) programmed into the event handlers at runtime.

Subclassing in RS is relatively easy and pretty darn powerful but you need to think carefully about variable scope and persistence.

AddHandler is useful for altering event handlers dynamically at runtime but take care to avoid ending up with spaghetti code.

_________________
Yes it's me in the avatar


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group