Jump to content
Sign in to follow this  
juniqofficial

TCP can't reconnect please help me - (Moved)

Recommended Posts

Hey I'm building a home automation system I mean it's all done except the software network part.

It shall run via TCP and send commands to a little server pc which then controls all my stuff. works like a charm, I use an android app called 'TCP Telnet Terminal' for testing and development. Later I wanna make my own one. The problem is, if I close the app or go backwards it disconnects the TCP connection which itself is not a problem BUT after disconnecting I can't reconnect. I mean I can reconnect successfully but then nothing works anymore. it's not sending / receiving anymore. anyone help me out?

 

here's the code:

TCPStartup()

_SP_PortOpen("COM8")

Local $iListenSocket = TCPListen('123.456.7.890', '65432', 100)
Do
   $iSocket = TCPAccept($iListenSocket)
Until $iSocket <> -1

While 1
   Local $sReceived = TCPRecv($iSocket, 4)
   If StringLen($sReceived) > 0 Then
      If $sReceived = "led1" Then
         _SP_SetOutput("txd", 1)
      ElseIf $sReceived = "led0" Then
         _SP_SetOutput("txd", 0)
      EndIf
   EndIf
WEnd

TCPShutdown()
_SP_PortClose()

Func _SP_PortOpen($port)
   $port_open = DllCall("kernel32.dll", "hwnd", "CreateFile", "str", "\\.\" & $port, "int", 0xC0000000, "int", 0, "ptr", 0, "int", 3, "int", 0, "int", 0)
   Global $PORT_OPEN_FC=$port_open
   DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 6)
   DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 4)
   DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 9)
EndFunc

Func _SP_PortClose()
   DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $PORT_OPEN_FC[0])
EndFunc

Func _SP_SetOutput($pin, $state)
   If $pin="dtr" Then
      If $state=1 Then
         DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 5) ;DTR
      ElseIf $state=0 Then
         DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 6) ;DTR
      Else
         Beep (2000, 400)
         MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
         Exit
      EndIf
   ElseIf $pin="rts" Then
      If $state=1 Then
         DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 3) ;RTS
         ElseIf $state=0 Then
         DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 4) ;RTS
      Else
         Beep (2000, 400)
         MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
         Exit
      EndIf
   ElseIf $pin="txd" Then
      If $state=1 Then
         DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 8) ;TXD
      ElseIf $state=0 Then
         DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 9) ;TXD
      Else
         Beep (2000, 400)
         MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
         Exit
      EndIf
   Else
      Beep (2000, 400)
      MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
      Exit
   EndIf
EndFunc

Func _SP_ReadInput($pin)
   Local $port_stat = DllStructCreate("DWORD")
   DllCall("kernel32.dll", "long", "GetCommModemStatus", "HWND", $PORT_OPEN_FC[0], "Ptr", DllStructGetPtr($port_stat))
   If $pin="cts" Then
      If BitAND(DllStructGetData($port_stat, 1), 0x10) Then ;CTS
         Return 1
      Else
         Return 0
      EndIf
   ElseIf $pin="dsr" Then
      If BitAND(DllStructGetData($port_stat, 1), 0x20) Then ;DSR
         Return 1
      Else
         Return 0
      EndIf
   ElseIf $pin="ri" Then
      If BitAND(DllStructGetData($port_stat, 1), 0x40) Then ;RI
         Return 1
      Else
         Return 0
      EndIf
   ElseIf $pin="dcd" Then
      If BitAND(DllStructGetData($port_stat, 1), 0x80) Then ;DCD
         Return 1
      Else
         Return 0
      EndIf
   Else
      Beep (2000, 400)
      MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
      Exit
   EndIf
EndFunc

 

image.png

Share this post


Link to post
Share on other sites

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
6 minutes ago, Jos said:

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Moderation Team

LOL?? I posted this in autoit general help and support what the??  this website sucks.

Share this post


Link to post
Share on other sites
12 minutes ago, Jos said:

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Moderation Team

and MOVED where? there's not even a link to follow ur post...

Share this post


Link to post
Share on other sites

juniqofficial,

Quote

I posted this in autoit general help and support

As the Moderation team had to move it, you obviously did not - and no-one else has ever complained about the site itself changing the destination. I suggest that it was the keyboard-chair interface at fault in this case.

If you look in the Developer General Discussion forum you will see the link. Your thread has been moved - and you with it.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

What about you kick in the brain first before firing multiple posts? 

You are posting in this thread so you can see which forum it is in.

...and telling us this forum sucks won't help you much. :idiot:

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Ohhkay.. so as this obviously as a moderator said is nat geaky enough to fit into scripting and development I post it here :)

Let's see if it really is "not geaky enough" it's funny cz I actually couldn't found any help to this problem in no forum. maybe there are some smart guys in here ;)

 

I have a TCP connection. an android phone (mine) connects to a local server mini pc. it's for a home automation system.

I can connect successfully. BUT when I close the app and open again it's not sending / receiving anymore.

 

I HAVE TESTED all with a second script as client. it works perfect. If the script is closed my server detects that and waits for the client to connect again and then it works just fine.

 

BUT why does that not work on my phone? I have tested like ALL TCP apps on Google Play Store. All the same so I'm pretty sure it's not an app problem. I'm in the same wifi net of course as the connection works the first time and data transfer too. but then closing and opening the app again and reconnecting leads to a non receiving server.

 

I can't anymore guys.. my fingers are bleeding from Googling.. that can't be that hard is it? Is it that hard to detect that a phone disconnected a simple TCP connection? like we're in 2018 right?

 

As you can see in the code there's an if statement to check if the received message contains the string 'Nocommand' I made this for testing.

When I send 'Nocommand' with my phones app the server successfully restarts (exits the loop) which shall be done when the connection aborts too.

Now I guess there must be AANYTHING that tells the server "hey dude I broke the connection" in my client script on my pc which works fine.

but WHAT? what is it telling? is it sending something special? How does "TCPRecv's @error" know that the connection breaks`? And when I break the connection with my phone why doesn't it recognize that? I even tested turning off WiFi on my phone. nothing. server doesn't notice that.

 

the c

TCPStartup()
$ConnectedSocket = -1
$MainSocket = TCPListen('127.0.0.1', 1337)
;~ $MainSocket = TCPListen('192.168.2.103', 65432)
If $MainSocket = -1 Then Exit
While 1
   ConsoleWrite("waiting for client...")
   Do
      $ConnectedSocket = TCPAccept($MainSocket)
   Until $ConnectedSocket <> -1
   SendSciTE_Command("menucommand:420")
   While 1
      $recv = TCPRecv($ConnectedSocket, 2048)
      If @error Then
         SendSciTE_Command("menucommand:420")
         ExitLoop
      EndIf
      If $recv <> "" Then
         ConsoleWrite($recv & @CRLF)
      EndIf
      If StringInStr($recv, "Nocommand") Then
         ExitLoop
      EndIf
   WEnd
   If $ConnectedSocket <> -1 Then
      TCPCloseSocket($ConnectedSocket)
   EndIf
   $ConnectedSocket = -1
WEnd

TCPCloseSocket($MainSocket)
TCPShutdown()
Exit

; ONLY FOR DEVELOPMENT PURPOSES TO CLEAR THE CONSOLE
Func SendSciTE_Command($sCmd)
   Local $Scite_hwnd = WinGetHandle("DirectorExtension")
   Local $WM_COPYDATA = 74
   Local $CmdStruct = DllStructCreate('Char[' & StringLen($sCmd) + 1 & ']')
   DllStructSetData($CmdStruct, 1, $sCmd)
   Local $COPYDATA = DllStructCreate('Ptr;DWord;Ptr')
   DllStructSetData($COPYDATA, 1, 1)
   DllStructSetData($COPYDATA, 2, StringLen($sCmd) + 1)
   DllStructSetData($COPYDATA, 3, DllStructGetPtr($CmdStruct))
   DllCall('User32.dll', 'None', 'SendMessage', 'HWnd', $Scite_hwnd, _
   'Int', $WM_COPYDATA, 'HWnd', 0, _
   'Ptr', DllStructGetPtr($COPYDATA))
EndFunc
; END

ode:

Edited by juniqofficial
forgot code

Share this post


Link to post
Share on other sites

juniqofficial,

Please stick to just the one thread at a time - although you did post the new one in the correct section this time. ;)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
1 hour ago, Jos said:

What about you kick in the brain first before firing multiple posts? 

You are posting in this thread so you can see which forum it is in.

...and telling us this forum sucks won't help you much. :idiot:

Jos

and playing the brave moderator doesn't help me much too. cz ppl see there are answers on this question already and most ppl search for questions with no answers....thx

Share this post


Link to post
Share on other sites

Yea Julian..   that's what we do...  Just be brave. 
I am sure this all will not really help you to get the focus back to the question on hand and get an answer, but you can imagine you won't get much cooperation from the team for a while.

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
1 minute ago, Jos said:

Yea Julian..   that's what we do...  Just be brave. 
I am sure this all will not really help you to get the focus back to the question on hand and get an answer, but you can imagine you won't get much cooperation from the team for a while.

Jos

what a team. fuck this place it's worse than gf.net  tf.. just ban me I'll go for a better site with moderators not fuckin bout everything and giving answers atleast.

Share this post


Link to post
Share on other sites

juniqofficial,

Quote

just ban me

My pleasure.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Yup,

One of the faster ones. :D

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...