psychopyro212 Posted May 7, 2012 Share Posted May 7, 2012 I've been screwing around with tcp functions for a few days just trying to expand my knowledge of autoit a little and I've hit a brick wall with this one its a login server/client its being used to send login to server server verifies the login and sends the client a accepted or denied I'm running into the issue where the client/server stop communicating AND where rather the server isn't sending the client the verified/denied or the client isn't receiving it so i wrote in some error reporting to a text file which I've pasted here :http://pastebin.com/GLfaZkt3 any help would be great I've hit a brick wall.P.S. Yes i know i use retarded variable names and yes i know its very crude code I'm still learning.Serverexpandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=server.exe #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <GUIConstantsEx.au3> Local $szIPADDRESS = @IPAddress1 Local $nPORT = 33891 Local $nport1 = 33892 Local $MainSocket, $edit, $ConnectedSocket, $szIP_Accepted, $sendverf, $enroleeip, $sendver Local $msg, $recv, $login, $password, $recvd, $error1 GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200, 100, 100) $edit = GUICtrlCreateEdit("", 10, 10, 280, 180) GUISetState() Server() Func Server() TCPStartup() $MainSocket = TCPListen($szIPADDRESS, $nPORT) If $MainSocket = -1 Then Exit $ConnectedSocket = -1 Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 $szIP_Accepted = SocketToIP($ConnectedSocket) While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop $recv = TCPRecv($ConnectedSocket, 2048) If @error Then restart() $recv = BinaryToString($recv, 4) If $recv <> "" Then GUICtrlSetData($edit, _ @HOUR & ":" & @MIN & ":" & @SEC & " " & $szIP_Accepted & " > Login : " & $recv & @CRLF & GUICtrlRead($edit)) verify() WEnd EndFunc ;==>Server Func verify() Local $ConnectedSocket1, $szData1 Local $CLIENTIP = $szIP_Accepted Local $nport1 = 33892 $ConnectedSocket1 = -1 $ConnectedSocket1 = TCPConnect($CLIENTIP, $nport1) If @error Then FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " & "Failed to connect to client---$ConnectedSocket1 = TCPConnect($CLIENTIP, $nport1 line 40 server") If $recv <> "" Then $recvd = StringSplit($recv, "\", 1) $login = $recvd[1] $password = $recvd[2] $login = IniRead("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\Data.ini", "Users", $login, "error") EndIf If @error Then $sendverf = 0 EndIf If $login = $password Then $sendverf = "Accepted" GUICtrlSetData($edit, _ @HOUR & ":" & @MIN & ":" & @SEC & " " & $szIP_Accepted & " > Login Accepted! " & @CRLF & GUICtrlRead($edit)) FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " & $szIP_Accepted & " > Login Accepted! ") $sendver = StringToBinary($sendverf, 4) Else $sendverf = "Denied" GUICtrlSetData($edit, _ @HOUR & ":" & @MIN & ":" & @SEC & " " & $szIP_Accepted & " > Login Denied! " & @CRLF & GUICtrlRead($edit)) FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " & $szIP_Accepted & " > Login Denied! ") $sendver = StringToBinary($sendverf, 4) EndIf TCPSend($ConnectedSocket1, $sendverf) If @error Then FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " & "Failed to send string to client---TCPSend($ConnectedSocket1, $sendverf) line 62 server") restart() EndFunc ;==>verify Func restart() TCPShutdown() server() EndFunc ;==>restart Func SocketToIP($SHOCKET) Local $sockaddr, $aRet $sockaddr = DllStructCreate("short;ushort;uint;char[8]") $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _ "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr)) If Not @error And $aRet[0] = 0 Then $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3)) If Not @error Then $aRet = $aRet[0] Else $aRet = 0 EndIf $sockaddr = 0 Return $aRet EndFunc ;==>SocketToIPClientexpandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=client.exe #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 252, 52, 192, 124) $Input1 = GUICtrlCreateInput("account", 0, 0, 177, 21) $Input2 = GUICtrlCreateInput("password", 0, 24, 177, 21) $Button1 = GUICtrlCreateButton("Login", 184, 8, 57, 33) GUISetState(@SW_SHOW) #endregion ### END Koda GUI section ### Global $login, $pass, $send, $string, $verf, $recver, $reccon, $error1 Global $ConnectedSocket, $szData, $ConnectedSocket1, $MainSocket1 Global $szIPADDRESS = @IPAddress1 Global $nPORT = 33891 Local $nport1 = 33892 Global $ConnectedSocket = -1 $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT) main() Func main() ;While 1 ; $nMsg = GUIGetMsg() ;Switch $nMsg ; Case $GUI_EVENT_CLOSE ; Exit ;Case $Button1 $login = GUICtrlRead($Input1) $pass = GUICtrlRead($Input2) Sleep(1000) Sendlogin() ;EndSwitch ;WEnd EndFunc ;==>main Func Sendlogin() $send = String($login & "\" & $pass) TCPStartup() $ConnectedSocket = -1 $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT) If @error Then FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " &"Failed to connect to server --- $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT) line 40 client") TCPSend($ConnectedSocket, StringToBinary($send, 4)) If @error Then FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " & "Failed to send string to server --- TCPSend($ConnectedSocket, StringToBinary($send, 4)) line 45 client") receiveverf() EndFunc ;==>Sendlogin Func receiveverf() $MainSocket1 = TCPListen($szIPADDRESS, $nPORT1) If $MainSocket1= -1 Then FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " &"client or server dropped connection --- $MainSocket1 = TCPListen($szIPADDRESS, $nPORT1) _ If $MainSocket1= -1 line 56 client") $ConnectedSocket1 = -1 Do $ConnectedSocket1 = TCPAccept($MainSocket1) Until $ConnectedSocket1 <> -1 $szIP_Accepted = SocketToIP($ConnectedSocket1) While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop $recv = TCPRecv($ConnectedSocket1, 2048) $recv = BinaryToString($recv, 4) If $recv <> "" Then FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " &"Client Received Response from Server!");MsgBox("","",$recv) restart() WEnd EndFunc ;==>receiveverf Func restart() $recv = "" TCPShutdown() main() EndFunc ;==>restart Func SocketToIP($SHOCKET) Local $sockaddr, $aRet $sockaddr = DllStructCreate("short;ushort;uint;char[8]") $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _ "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr)) If Not @error And $aRet[0] = 0 Then $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3)) If Not @error Then $aRet = $aRet[0] Else $aRet = 0 EndIf $sockaddr = 0 Return $aRet EndFunc ;==>SocketToIP Func errorwrite() FileWrite("C:\Documents and Settings\Administrator\Desktop\Projects\Login\Secure\log.txt", @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & " " & $error1) main() EndFunc Link to comment Share on other sites More sharing options...
tsolrm Posted May 7, 2012 Share Posted May 7, 2012 I didn't look at the code properly but i've noticed that you have TCPAccept in both client and server codes. You only need to accept the connection once and then the client and the server can communicate as long as the connection exists. Link to comment Share on other sites More sharing options...
tsolrm Posted May 7, 2012 Share Posted May 7, 2012 If you want multiple connections (this is the proper way to do it) what you need to do is this. Global $Socket[1] (this is the array with all the connections) ==Server== TCPStartup() $TCPListen=TCPListen (ip, port) While 1 $TCPAccept = TCPAccept ($TCPListen) If $TCPAccept <> -1 Then _ArrayAdd ($Socket, $TCPAccept ) --- if a NEW connection is made, it's added to the list. EndIf Now you need to recieve messages in a loop from all sockets For $i=1 to Ubound ($Socket)-1 $Message = TCPRecv ($Socket [$i], 100000) *here comes the code about what you want to do with the message* Next Wend ==CLIENT== TCPStartup() TCPConnect (ip,port) TCPSend (tcpconnect, message) Link to comment Share on other sites More sharing options...
psychopyro212 Posted May 7, 2012 Author Share Posted May 7, 2012 (edited) expandcollapse popup#include <array.au3> Local $ip = @IPAddress1 Local $port = 33891 Local $TCPListen, $TCPAccept, $Approve Global $Socket[1] starttcp() Func starttcp() TCPShutdown() TCPStartup() $TCPListen = TCPListen($ip, $port) While 1 $TCPAccept = TCPAccept($TCPListen) If $TCPAccept <> -1 Then _ArrayAdd($Socket, $TCPAccept) receivetcp() EndIf WEnd EndFunc ;==>starttcp Func receivetcp() While 1 For $i = 1 To UBound($Socket) - 1 $Message = TCPRecv($Socket[$i], 100000) If $Message <> "" Then _ MsgBox("", "", $Message) sendtcp() Next WEnd EndFunc ;==>receivetcp Func sendtcp() $Approve = "HEARD YOU!" TCPSend($TCPAccept, $Approve) receivetcp() EndFunc ;==>sendtcp The problem im running into is turning around and talking back to the client i appreciate the help you have both given me its helped tidy my code up quite a bit. The question i have is will the code i posted above be capable or turning around and sending a msg back to the client? Edited May 7, 2012 by psychopyro212 Link to comment Share on other sites More sharing options...
tsolrm Posted May 7, 2012 Share Posted May 7, 2012 (edited) You can talk back to the client at any point TCPSend (Socket[$i], Message) Edited May 7, 2012 by tsolrm Link to comment Share on other sites More sharing options...
tsolrm Posted May 7, 2012 Share Posted May 7, 2012 So lets sayFor $i=1 to Ubound ($Socket)-1$Message = TCPRecv ($Socket [$i], 100000)If $Message = "Hello" Then TCPSend ($Socket[$i], "Hello back")Next Link to comment Share on other sites More sharing options...
psychopyro212 Posted May 7, 2012 Author Share Posted May 7, 2012 (edited) ERROR: syntax errorTCPSend(Socket[$i], $Approve)had a moment ignore me lol thank you Edited May 7, 2012 by psychopyro212 Link to comment Share on other sites More sharing options...
tsolrm Posted May 7, 2012 Share Posted May 7, 2012 You are welcome. when i asked about this no one helped me so it took me many hours (even days) to realise how to create a proper chat using TCP in Autoit. I hope this will clarify things for you/ Link to comment Share on other sites More sharing options...
psychopyro212 Posted May 7, 2012 Author Share Posted May 7, 2012 (edited) Local $ip = @IPAddress1 Local $port = 33891 Local $message, $tcpconnect, $message1 Global $Socket[1] Sendtcp() Func Sendtcp() TCPStartup() $tcponnect = TCPConnect($ip, $port) $message = "test" TCPSend($tcponnect,$message) While 1 For $i = 1 To UBound($Socket) - 1 $message1 = TCPRecv($Socket[$i], 100000) If $message1 <> "" Then _ MsgBox("", "", $message1) Next WEnd EndFunc ;==>Sendtcp Ok using what youve shown me ive come up with this for the client im still scratching my head.... Edit : forgot _ArrayAdd($Socket, $tcponnect) Edited May 7, 2012 by psychopyro212 Link to comment Share on other sites More sharing options...
tsolrm Posted May 7, 2012 Share Posted May 7, 2012 (edited) $Socket is the array that holds the connections to all the clients. The server is connected to many clients (for example) but a client is only connected to the server So for the client you will need $Message = TCPRec ($TCPConnect, 1000000) Edited May 7, 2012 by tsolrm Link to comment Share on other sites More sharing options...
tsolrm Posted May 7, 2012 Share Posted May 7, 2012 In case you don't understand what For $i = 1 to Ubound (Socket)-1 does, its a loop which will run the code within it for every instance of $i defined. You don't need the loop for the client because you only have 1 connection which is to the server Link to comment Share on other sites More sharing options...
guinness Posted May 8, 2012 Share Posted May 8, 2012 psychopyro212, Be careful of recursion errors. If you both want an example of using multiple connections then let me know. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now