TCP aide
Posté : mar. 28 nov. 2017 08:25
Bonjour, alors voilà j'utilise les udf tcp client et serveurs suivants
https://github.com/jesobreira/tcpclient-udf
https://github.com/jesobreira/TCPServerUDF
Le serveur :
Le client :
J'explique le problème, quand je lance un client, il ne relie pas toujours le serveur au même socket, des fois le client détecte que le serveur est au socket 508 et des fois 504, je ne comprends pas pourquoi, c'est vraiment étrange, surtout que c'est vraiment aléatoire, des fois je lance 10 clients et les 10 detectent le serveur au 508 et des fois certains l'identifient au socket 504 ...
https://github.com/jesobreira/tcpclient-udf
https://github.com/jesobreira/TCPServerUDF
Le serveur :
Code : Tout sélectionner
#NoTrayIcon
#include "TCPServer.au3"
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
#include <Array.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$frmList = GUICreate("List clients", 306, 114, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_EXIT")
$gList = GUICtrlCreateListView("IP|Socket", 0, 0, 305, 113)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 200)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
HotKeySet("{ESC}", "_EXIT")
_TCPServer_OnConnect("OnConnect")
_TCPServer_OnDisconnect("OnDisconnect")
_TCPServer_OnReceive("receive")
_TCPServer_DebugMode(True)
_TCPServer_SetMaxClients(30)
_TCPServer_Start(8080, @IPAddress1)
While 1
Sleep(100)
WEnd
Func OnConnect($iSocket, $sIP)
Local $LIST = _TCPServer_ListClients()
_TCPServer_Broadcast("Connected: " & $iSocket & "|" & $sIP & @CRLF)
Sleep(1000)
_TCPServer_Send($iSocket, "IMSERVER" & @CRLF)
GUICtrlCreateListViewItem($sIP & "|" & $iSocket, $gList)
EndFunc ;==>OnConnect
Func OnDisconnect($iSocket, $sIP)
Local $LIST = _TCPServer_ListClients()
_GUICtrlListView_DeleteAllItems($gList)
$aActive = _TCPServer_ListClients()
For $i = 1 To _TCPServer_GetMaxClients()
$sIP = _TCPServer_SocketToIP($aActive[$i])
If $sIP <> 0 Then
GUICtrlCreateListViewItem($sIP & "|" & $aActive[$i], $gList)
EndIf
Next
_TCPServer_Broadcast("Disconnected: " & $iSocket & @CRLF)
EndFunc ;==>OnDisconnect
Func receive($iSocket, $sIP, $sData, $mPar)
Local $LIST = _TCPServer_ListClients()
_LOG(@CRLF & "Clients : " & $LIST[0])
EndFunc ;==>receive
Func _EXIT()
_TCPServer_Broadcast("EXIT")
_TCPServer_Stop()
Exit
EndFunc ;==>_EXIT
Func _LOG($TEXT)
ConsoleWrite($TEXT & @CRLF)
EndFunc ;==>_LOG
Code : Tout sélectionner
#NoTrayIcon
#include "TCPClient.au3"
Global $SERVERID
_TCPClient_OnReceive("receive")
_TCPClient_OnDisconnect("disconnect")
_TCPClient_DebugMode()
Func receive($iSocket, $sIP, $sData, $mPar)
If $sData == "EXIT" Then
Exit
ElseIf $sData == "IMSERVER" Then
$SERVERID = $iSocket
EndIf
EndFunc ;==>receive
Func disconnect($iSocket, $sIP)
EndFunc ;==>disconnect
$conn = _TCPClient_Connect(@IPAddress1, '8080')
If @error Then
Exit
EndIf
While 1
Sleep(1000)
If $SERVERID <> "" Then _TCPClient_Send($SERVERID, "PING" & @CRLF)
WEnd
Func _LOG($TEXT)
Local $DATE_LONG = "(" & @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s" & @MSEC & "ms)"
ConsoleWrite($TEXT & @CRLF)
EndFunc ;==>_LOG