I am working on a chatting program using AHKsock and I have run into a problem I am hoping someone can shed some light on. My project is based off of example 3 - Messaging. After I have connected and sent a message, I want to disconnect and resume listening.
What I have tried with no success is posted below. I am using AHKsock_close() and AHKsock_Listen() functions to try to disconnect and then resume listening. The program disconnects but apparently does not resume listening, because it is unresponsive to attempts from the script running on other machines to connect.
btnSend:
sName = 172.29.112.98
MsgBox, % AHKsock_Connect(sName, 27015, "Peer")
Sleep, 1000
/*! This GUI event will be called everytime the user clicks on the Send button, or when he presses the Enter key after
typing text in the Edit control. The logic is rather straight-forward. We first send a string length frame to tell the
peer how long the string coming will be, and we then send the string. See the StreamProcessor function for more
information on frames.
*/
;Check if we're connected
If (iPeerSocket = -1)
Return
;Get the text to send
GuiControlGet, sText,, txtInput
;Make sure we even have something to send
If Not sText
Return
;Get text length
iTextLength := StrLen(sText) * 2
;First send a string length frame: high-word is 2, low-word is the length of the string
VarSetCapacity(iFrame, 4, 0), NumPut((2 << 16) + iTextLength, iFrame, 0, "UInt")
If (i := AHKsock_ForceSend(iPeerSocket, &iFrame, 4)) {
OutputDebug, % "AHKsock_ForceSend failed with return value = " i " and error code = " ErrorLevel " at line " A_LineNumber
ExitApp
}
;Send the actual string now, excluding the null terminator
If (i := AHKsock_ForceSend(iPeerSocket, &sText, iTextLength)) {
OutputDebug, % "AHKsock_ForceSend failed with return value = " i " and error code = " ErrorLevel " at line " A_LineNumber
ExitApp
}
;Data was sent. Add it to the dialog.
AddDialog(&sText)
;Clear the Edit control and give focus
GuiControl,, txtInput
GuiControl, Focus, txtInput
Sleep, 1000
AHKsock_Close()
Sleep, 1000
AHKsock_Listen(27015, "Peer")
Return