TCP/IP server and client

Functions, classes, components, etc. Only high quality content.

TCP/IP server and client

Postby Gintaras on Mon Oct 10, 2011 7:49 pm

Macro TcpSocket 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

Attachments
TcpSocket.qml
(10.77 KiB) Downloaded 438 times
Gintaras
Site Admin
 
Posts: 10370
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: TCP/IP server and client

Postby r0n on Sun Oct 08, 2017 1:12 am

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     ?
\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     ?
\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

r0n
Active Member
 
Posts: 495
Joined: Tue Jul 24, 2012 5:47 am

Re: TCP/IP server and client

Postby Gintaras on Sun Oct 08, 2017 6:05 am

In the example test_TcpSocket_func2 is used out. Does it not work when out replaced with mes?
Gintaras
Site Admin
 
Posts: 10370
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: TCP/IP server and client

Postby r0n on Sun Oct 08, 2017 4:37 pm

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)):

2.jpg
When I send the below http-post (below android screenshot) I get this output
2.jpg (26.76 KiB) Viewed 274 times


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

1.jpg
This is how the http-post is made in the android device.
1.jpg (50.98 KiB) Viewed 272 times


EDIT:

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

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!
r0n
Active Member
 
Posts: 495
Joined: Tue Jul 24, 2012 5:47 am

Re: TCP/IP server and client

Postby Gintaras on Sun Oct 08, 2017 7:53 pm

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[]"
Gintaras
Site Admin
 
Posts: 10370
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: TCP/IP server and client

Postby Gintaras on Sun Oct 08, 2017 8:09 pm

Solved. The response must end with two line breaks.

e15res="HTTP/1.1 200 OK[][]"
Gintaras
Site Admin
 
Posts: 10370
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: TCP/IP server and client

Postby Gintaras on Sun Oct 08, 2017 9:15 pm

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.
Gintaras
Site Admin
 
Posts: 10370
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: TCP/IP server and client

Postby Gintaras on Sun Oct 08, 2017 9:17 pm

Full server code and instructions.

viewtopic.php?f=11&t=7214&p=33286#p33286
Gintaras
Site Admin
 
Posts: 10370
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: TCP/IP server and client

Postby r0n on Sun Oct 08, 2017 11:05 pm

THANK YOU!!!
r0n
Active Member
 
Posts: 495
Joined: Tue Jul 24, 2012 5:47 am


Return to QM Extensions

Who is online

Users browsing this forum: No registered users and 2 guests