Luigi Posted December 17, 2014 Share Posted December 17, 2014 (edited) I need an exemple from TCP client/server both running in "the same ip" (127.0.0.1). Of course, each one running in diferents ports. Is possible? Thanks, Detefon. I build this little code, but have return: TCPConnect @error=10061 server: expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Compression=4 #AutoIt3Wrapper_UseUpx=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include-once #include <GuiRichEdit.au3> #include <Array.au3> #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> OnAutoItExitRegister("on_exit") Opt("GUIOnEventMode", 1) Opt("GUIEventOptions", 1) Opt("MustDeclareVars", 1) ;~ Opt("TCPTimeout", 500) Global $LOCALHOST = "127.0.0.1" Global $aGuiSize[2] = [800, 600] Global $sGuiTitle = "GuiTitle" Global $hGui Global $hEdit Global $aMsg Global $SOCKET Global $PORT = 4334 If $CmdLine[0] == 1 Then $PORT = Number($CmdLine[1]) $hGui = GUICreate("port[ " & $PORT & " ]", $aGuiSize[0], $aGuiSize[1], 1960) GUISetOnEvent($GUI_EVENT_CLOSE, "_quit") $hEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 780, 580, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY)) GUISetState(@SW_SHOW, $hGui) Global $hTcp = TCPStartup() If @error Then _Console("TCPStartup() @error" & @LF) Global $MAIN = TCPListen($LOCALHOST, $PORT, 100) If @error Then _Console("TCPListen() @error" & @LF) While True check_tcp() WEnd Func check_tcp() $SOCKET = TCPAccept($MAIN) If @error Then Else Local $sReceived = TCPRecv($SOCKET, 1024) If $sReceived Then Local $iClientPort = StringLeft($sReceived, 4) _Console($sReceived) SendToServer(Number(Dec($iClientPort)), "do servidor " & $sReceived) EndIf ;~ TCPCloseSocket($SOCKET) EndIf EndFunc ;==>check_tcp Func SendToServer($iClientPort, $sMsg = "") _Console("SendToServer( $iPort=" & $iClientPort & ", $sMsg=" & $sMsg & ")") Local $SEND = TCPConnect($LOCALHOST, Number($iClientPort)) If @error Then _Console(@TAB & "TCPConnect @error=" & @error) Else TCPSend($SEND, $sMsg) If @error Then _Console(@TAB & "TCPSend @error=" & @error) Else _Console(@TAB & "TCPSend @OK") EndIf ;~ TCPCloseSocket($SEND) If @error Then _Console(@TAB & "TCPCloseSocket @error=" & @error) EndIf EndFunc ;==>SendToServer Func check_text() $aMsg = IniReadSection("config.ini", "client") If Not IsArray($aMsg) Then Return For $ii = 0 To UBound($aMsg, 1) - 1 If $aMsg[$ii][1] Then _Console("client[" & $aMsg[$ii][0] & "] msg[" & $aMsg[$ii][1] & "]") IniWrite("config.ini", "client", $aMsg[$ii][0], "") IniWrite("config.ini", $aMsg[$ii][0], "msg", $aMsg[$ii][1]) EndIf Next EndFunc ;==>check_text Func _quit() Exit EndFunc ;==>_quit Func on_exit() AdlibUnRegister("check_text") If $hTcp Then TCPCloseSocket($MAIN) TCPShutdown() EndIf _GUICtrlRichEdit_Destroy($hEdit) GUIDelete($hGui) EndFunc ;==>on_exit Func _Console($sText, $sAttrib = "", $iColor = "") ; http://www.autoitscript.com/forum/topic/121728-richedit-not-working/ Local $sLenght = StringLen($sText), $iStep = 75, $sTemp = "" StringReplace(_GUICtrlRichEdit_GetText($hEdit, True), @CRLF, "") Local $iLines = @extended Local $iEndPoint = _GUICtrlRichEdit_GetTextLength($hEdit, True, True) - $iLines If $sLenght > $iStep Then For $xx = 1 To $sLenght Step $iStep If $xx = 1 Then $sTemp &= "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " Else $sTemp &= " " EndIf $sTemp &= StringMid($sText, $xx, $iStep) & @CRLF Next _GUICtrlRichEdit_AppendText($hEdit, $sTemp & StringMid($sText, $xx, $iStep)) Else _GUICtrlRichEdit_AppendText($hEdit, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $sText & @CRLF) EndIf _GUICtrlRichEdit_SetSel($hEdit, $iEndPoint, -1) $iColor = Hex($iColor, 6) $iColor = "0x" & StringMid($iColor, 5, 2) & StringMid($iColor, 3, 2) & StringMid($iColor, 1, 2) If $iColor = "" Then _GUICtrlRichEdit_SetCharColor($hEdit, 0x000000) Else _GUICtrlRichEdit_SetCharColor($hEdit, $iColor) EndIf If Not ($sAttrib == "") Then _GUICtrlRichEdit_SetCharAttributes($hEdit, $sAttrib) _GUICtrlRichEdit_SetFont($hEdit, 8, "Courier New") _GUICtrlRichEdit_Deselect($hEdit) EndFunc ;==>_Console client expandcollapse popup#include-once #include <Array.au3> Global $PORT_SERVER = 4333 ;~ If Not ProcessExists("d_server.exe") Then ShellExecute("d_server.exe", $PORT_SERVER) OnAutoItExitRegister("on_exit") Global $LOCALHOST = "127.0.0.1" Global $SOCKET Global $hTcp = TCPStartup() If @error Then ConsoleWrite("TCPStartup @error" & @LF) Global $SOCKET_SERVER; = TCPConnect($LOCALHOST, $PORT_SERVER) ;~ If @error Then ConsoleWrite("TCPConnect @error" & @LF) Global $PORT_CLIENT = $PORT_SERVER + 1 ; TCPPortFree(4400) Global $SOCKET_CLIENT = TCPListen($LOCALHOST, $PORT_CLIENT, 100) If @error Then ConsoleWrite("TCPListen(" & $PORT_CLIENT & ") @error" & @LF) Func SendToServer($sMsg = "") $SOCKET_SERVER = TCPConnect($LOCALHOST, $PORT_SERVER) If @error Then If @error Then ConsoleWrite("TCPConnect @error" & @LF) Else TCPSend($SOCKET_SERVER, $sMsg) If @error Then If @error Then ConsoleWrite("TCPSend @error" & @LF) Else If @error Then ConsoleWrite("TCPSend @OK" & @LF) EndIf TCPCloseSocket($SOCKET_SERVER) If @error Then ConsoleWrite("TCPCloseSocket @error" & @LF) EndIf EndFunc ;==>SendToServer AdlibRegister("sendmsg", 200) While True check_tcp() WEnd Func sendmsg() SendToServer(Hex($PORT_CLIENT, 4) & "|" & @HOUR & " = = " & @SEC & " = = " & @MSEC) AdlibUnRegister("sendmsg") Exit EndFunc ;==>sendmsg Func TCPPortFree($iBegin = 4300, $iEnd = 4400) Local $iPort = $iBegin Local $try Do $try = IsUsedPort($iPort) If @error Then $iPort += 1 Else Return $iPort EndIf Until Not $try EndFunc ;==>TCPPortFree Func IsUsedPort($iPort) Local $iSocket = TCPListen($LOCALHOST, $iPort) If @error Then Return SetError(1, 0, True) If $iSocket == -1 Then Return SetError(1, 0, True) Return False EndFunc ;==>IsUsedPort Func check_tcp() $SOCKET = TCPAccept($SOCKET_CLIENT) If @error Then ConsoleWrite("TCPAccept @error" & @LF) Else Local $sReceived = TCPRecv($SOCKET, 1024) If @error Then ConsoleWrite("TCPRecv @error" & @LF) If $sReceived Then ConsoleWrite("Received [" & $sReceived & " ]" & @LF) TCPCloseSocket($SOCKET) If @error Then ConsoleWrite("TCPCloseSocket @error" & @LF) EndIf EndFunc ;==>check_tcp Func _quit() Exit EndFunc ;==>_quit Func on_exit() If $hTcp Then TCPCloseSocket($SOCKET_SERVER) TCPShutdown() EndIf EndFunc ;==>on_exit Edited December 17, 2014 by Detefon Visit my repository Link to comment Share on other sites More sharing options...
Luigi Posted December 17, 2014 Author Share Posted December 17, 2014 I found a great example >here from SlowCoder74. Work one server with two or more clients, include I running a client in diferent account, cool! No config was necessary, just compile and use. Excelent! Visit my repository 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