Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP/IP server and client
#1
Macro TcpSocket help
Code:
Copy      Help
;TCP server/client functions.
;Can be used to create TCP server (listen to incoming connections) or client (connect to a server).
;Server calls ServerStart. Client calls ClientConnect. Other functions are common.

;EXAMPLES: in "test" folder


Attached Files
.qml   TcpSocket.qml (Size: 10.77 KB / Downloads: 965)
#2
Is there a way for the server to output the content that is send from the client.
To be more specific, if the CLIENT sends the string "test" to the SERVER, then let the SERVER render a messagebox with contents "test" if the received string is "test".

SERVER CODE
Function dialog_TcpSocket_server
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

#compile "__TcpSocket"

str controls = "5 15"
str e5por e15res
e5por=5033
e15res="from server"
if(!ShowDialog("dialog_TcpSocket_server" &dialog_TcpSocket_server &controls win("" "QM_Editor"))) ret

;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 231 64 "TCP server"
;4 Static 0x54000000 0x0 170 8 22 12 "Port"
;5 Edit 0x54032000 0x200 196 6 32 14 "port"
;6 Button 0x54032000 0x0 6 6 48 14 "Start"
;7 Button 0x5C032000 0x0 58 6 48 14 "Stop"
;14 Static 0x54000000 0x0 6 30 36 12 "Response"
;15 Edit 0x54231044 0x200 46 28 182 30 "res"
;END DIALOG
;DIALOG EDITOR: "" 0x2030300 "*" "" ""

ret
;messages
TcpSocket- t_server
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,t_server.Close
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 6 ;;Start
,t_server.ServerStart(GetDlgItemInt(hDlg 5 0 0) &test_TcpSocket_func2 hDlg 1)
,EnableWindow lParam 0
,EnableWindow id(7 hDlg) 1
,
,case 7 ;;Stop
,t_server.Close
,EnableWindow lParam 0
,EnableWindow id(6 hDlg) 1
ret 1

err+ out _error.description


CLIENT CODE
Function dialog_TcpSocket_client
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

#compile "__TcpSocket"

str controls = "5 10 12"
str e5por e10ser e12dat
e5por=5033
e10ser="localhost"
e12dat="from client"
if(!ShowDialog("dialog_TcpSocket_client" &dialog_TcpSocket_client &controls win("" "QM_Editor"))) ret

;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 231 74 "TCP client"
;4 Static 0x54000000 0x0 170 8 22 12 "Port"
;5 Edit 0x54032000 0x200 196 6 32 14 "port"
;9 Static 0x54000000 0x0 4 6 26 13 "Server"
;10 Edit 0x54030080 0x200 32 4 124 14 "server"
;11 Static 0x54000000 0x0 4 26 26 13 "Data"
;12 Edit 0x54231044 0x200 32 22 196 30 "data"
;13 Button 0x54032000 0x0 4 56 48 14 "Send"
;END DIALOG
;DIALOG EDITOR: "" 0x2030300 "*" "" ""

ret
;messages
TcpSocket- t_server
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 13 ;;Send
,TcpSocket x.ClientConnect(_s.getwintext(id(10 hDlg)) GetDlgItemInt(hDlg 5 0 0))
,x.Send(_s.getwintext(id(12 hDlg)))
,if(x.Receive(_s 1000 1000)=2 and !_s.len) _s="<timeout>"
,out F"client: {_s}"
ret 1

err+ out _error.description
#3
In the example test_TcpSocket_func2 is used out. Does it not work when out replaced with mes?
#4
I am sorry, you are right the function is called serverside, it works.

But now I want to test it from outside by using an android smartphone, I used tasker to create a simple HTTP-post.
I think you can't give much feedback on this because it's about sending an http-post from outside QM's scope (and it involves android).
But any tip you can give is very welcome. (If someone else has experience with this, your feedback is very welcome!)

But I get the below output (and below error in tasker (android app)):

   

I needed to send some string, so I send "%BLUE" which is the blue-tooth status ("on" or "off")

   

EDIT:

SORRY, forgot to mention the following error which happens when I send the above HTTP-post in tasker (see above android screenshot)

Quote:17.34.40/Variables doreplresult: |%BLUE| -> |off|
17.34.40/E prot: http:// serverport: 192.168.0.189:5032 contenttype: application/x-www-form-urlencoded
17.34.40/E get/head or null
17.34.40/E bad attribute: no =: off
17.34.40/E method: POST url: http://192.168.0.189:5032 timout: 10000 dataisfile false save null
17.34.40/WakeLockManager setClearAlarm: not setting, last set 9 ago
17.34.40/WakeLockManager setClearAlarm: not setting, last set 13 ago
17.34.40/E body isfile: false cont: null
17.34.50/E Input/Output error for http://192.168.0.189:5032: java.net.SocketTimeoutException. <============ ERROR


EDIT2:
When I set the content type to "text", I now get the correct response in QM (output = "off").
But I still get the error:
/E Input/Output error for http://192.168.0.189:5032: java.net.SocketTimeoutException

Maybe just accept it as it is (ignore it?).
Any feedback (if possible) is still welcome!
#5
Here the same. With Content Type text/plain my QM server receives the text.
But Tasker always shows that error.
Maybe server's response is incorrect.
Tried this response, does not work. Don't know what it should be.

e15res="HTTP/1.1 200 OK[]Connection: close[]"
#6
Solved. The response must end with two line breaks.

e15res="HTTP/1.1 200 OK[][]"
#7
Example response for GET request:

e15res="HTTP/1.1 200 OK[]Content-Type: text/plain[]Content-Length: 4[][]DATA"

Tasker will save DATA in the specified file.
#8
Full server code and instructions.

Sending commands from Android+Tasker to QM
#9
THANK YOU!!!


Forum Jump:


Users browsing this thread: 3 Guest(s)