kreeper Posted April 6, 2011 Share Posted April 6, 2011 (edited) I have a server and a client: Server: expandcollapse popup$0 = 0 $00 = 0 $000 = 0 $Recv2 = "" $ip = InputBox ('IP=?','Please choose a funtioning IP for your server...',@IPAddress1) ;Makes the servers IP address... default is your @IPADDRESS1. $port = InputBox ('Port=?','Please Choose a port # that is not being used...', 6001) ;Makes the port # can be anything between 1 and 60000. ;(the maximum is a guess i don't know how many ports there are butits close). ;and ONLY if the port is not already being used. ;$max_connections = InputBox ('Maximim Connections=?', 'Please choose an amount of clients that would be able to connect to the server...(recommended that its minimal)', 20) $max_connections = 100 ;How many clients can be connected FUNCTIONABLY to the server... any more and it will not function correctly on the clients side. TCPStartup () $TCPListen = TCPListen ($ip, $port, $max_connections) If $TCPListen = -1 Then _Exit_ () ;Creates a listening Socket Dim $Socket_Data[$max_connections + 1][3] ;add more to the second array to add data. ;(ex) 2 would have another column for data. ;you could make Usernames or ClientIP's in it... anything you want. ;Socket_data EXAMPLE ;$Socket_Data[0][0] = AMOUNT OF CURRENT CLIENTS ;$Socket_Data[1][0] = client 1 ;$Socket_Data[2][0] = client 2 ;$Socket_Data[n][0] = client n ;ProgressOn ('Socket Data', 'Reseting Socket Data...') ;For $0 = 0 To $max_connections ;$Socket_Data[$0][0] = 0 ;To reset the Socket array. ;ProgressSet (Round (($0 / $max_connections) * 100),'','Reseting Socket Data...') ;Sleep (10) ;Next ;ProgressOff () While 1 _Accept_Connection () _Recv_From_Sockets_ () Sleep (100) ;$test = TCPSend($Socket_Data[0][0], "test") WEnd Func _Accept_Connection () If $Socket_Data[0][0] = $max_connections Then Return ;Makes sure no more Connections can be made. $Accept = TCPAccept ($TCPListen) ;Accepts incomming connections. If $Accept = -1 Then Return For $0 = 1 To $max_connections If $Socket_Data[$0][0] = 0 Then ;Find the first open socket. $Socket_Data[$0][0] = $Accept ;assigns that socket the incomming connection. Do $Recv = TCPRecv ($Accept, 1000000) Until $Recv <> '' $Recv = StringSplit ($Recv, '^') $Recv2 = $Recv[2] $Socket_Data[$0][1] = $Recv[1] $Socket_Data[$0][2] = $Recv[2] ;$Socket_Data[$0][n] = $Recv[n] DEPENDING O WHAT YOU DIMMED THE $SOCKET_DATA AS!!! $Socket_Data[0][0] += 1 ;adds one to the Socket list. ;$00 = 'New Client' $00 = $Recv2 _Broadcast_To_Sockets_ ($00) ;Broadcasts to all the clients. $0 = 0 $00 = 0 Return EndIf Next Return EndFunc Func _Broadcast_To_Sockets_ ($0) For $00 = 1 To $max_connections TCPSend ($Socket_Data[$00][0], $0) ;sends to every client. Next $0 = 0 $00 = 0 Return EndFunc Func _Recv_From_Sockets_ () For $0 = 1 To $max_connections $Recv = TCPRecv ($Socket_Data[$0][0],1000000) _Broadcast_To_Sockets_ ($Recv) Next EndFunc Func _Exit_ () For $0 = 1 To $max_connections TCPCloseSocket ($Socket_Data[$0][0]) Next TCPCloseSocket ($TCPListen) TCPShutdown () Exit EndFunc And Client: expandcollapse popup#include <GUICONSTANTS.AU3> #include <WINDOWSCONSTANTS.AU3> #include <STATICCONSTANTS.AU3> #include <EDITCONSTANTS.AU3> #include <MISC.AU3> #Include <GuiEdit.au3> #Include <WinAPI.au3> #Include <DATE.au3> Opt ('GUIoneventmode', 1) $GUIStyle = BitOR($WS_DLGFRAME,$WS_POPUP, $WS_VISIBLE) $GUIStyleEx = BitOR ($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE) $EditStyle = BitOR($ES_MULTILINE,$WS_VSCROLL) $EditStyleEx = BitOR($EditStyle, $ES_READONLY) $W = 0xFFFFFF $B = 0x0 $Titleb = 0x7F7F7F TCPStartup() $0 = 0 $00 = 0 $000 = 0 $ip = TCPNameToIP (InputBox('IP=?', 'Please choose a funtioning IP of your server...', @IPAddress1)) ;The servers IP address... default is your @IPADDRESS1. MUST BE THE SERVERS IP ADDRESS CANNOT BE YOUR CLIENTS!!! $port = InputBox('Port=?', 'Please Choose a port # that is specified by the server...', 6001) ;Makes the port # can be anything between 1 and 60000. ;(the maximum is a guess i don't know how many ports there are butits close). ;and ONLY if the port is not already being used. ;MUST BE SERVER OPENED PORT NOT ONE ON YOUR COMPUTER!!! $username = InputBox('Username=?', 'Please Chose a Username', "#User" & Random(1000, 9999, 1)) $start_gui = GUICreate("Connecting...", 250, 100) $try = 0 $try_msg = GUICtrlCreateLabel("", 10, 20, 280, 20) GUICtrlSetFont(-1, 10, 600) $connect_progress = GUICtrlCreateProgress(20, 60, 210, 20) GUISetState() GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit', $start_gui) For $0 = 0 To 10 GUICtrlSetData($try_msg, "Tryng to connect server...( " & $try & " )") $Socket = TCPConnect($ip, $port) $try = $try +1 ;Connects to an open socket on the server... If $Socket <> -1 Then ExitLoop TCPCloseSocket($Socket) Sleep(300) GUICtrlSetData($connect_progress, $try * 10) Next If $Socket = -1 Then MsgBox(0, "Client", "Unable to Contact Server!") _Exit () EndIf GUICtrlSetData($try_msg, "Connection completed. Loading...") GUICtrlSetData($connect_progress, 100) Sleep(2000) GUIDelete($start_gui) TCPSend ($Socket, '^New Client:' & " (" & $username & ") " & @UserName) ;EXAMPLE DATA ;TCPSend ($Socket, "blabla") ;EXAMPLE DATA $GUI = GUICreate (@ScriptName, 300, 200) $Console = GUICtrlCreateEdit ('',0,0,300,150,$EditStyleEx) $Send = GUICtrlCreateEdit ('',0,150,300,50,$EditStyle) GUICtrlSetLimit (-1, 250) GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit', $GUI) GUISetState () HotKeySet('{ENTER}', '_Send') While 1 _Recv_From_Server () Sleep(100) WEnd Func _Send () $0 = GUICtrlRead ($Send) If $0 = '' Then Return TCPSend($Socket, $username & ": " & $0) GUICtrlSetData ($Send,'') EndFunc ;==>_send_ Func _Recv_From_Server () $Recv = TCPRecv($Socket, 1000000) If $Recv = '' Then Return _GUICtrlEdit_AppendText ($Console,_NowTime () & ' -- ' & $Recv & @CRLF) EndFunc ;==>_Recv_From_Server_ Func _Minn () WinSetState ($GUI, '', @SW_MINIMIZE) EndFunc Func _Exit () TCPSend($Socket, "[System]: " & $username & " has left") GUIDelete($start_gui) TCPCloseSocket ($Socket) TCPShutdown() Exit EndFunc ;==>_Exit_ Work good but i have some problem: 1: i want to know when one of client is disconnected now when someone connect, make a soket slot, and if client left the slot don't become free... If $max_connections = 10, i can connect one client 10 time, and then server become busy. How can i know when a client is disconnected, and to free the slot? 2: How can i make a list of current user connected, to see on server and client? tnx Edited April 6, 2011 by kreeper Link to comment Share on other sites More sharing options...
bogQ Posted April 6, 2011 Share Posted April 6, 2011 (edited) 1) Make client to send every 10 seconds some msg that will mark if the client is alive Server side, _Timer_Init() on last msg recived from that client it _Timer_Diff() > 20 then... 2) Make array of users send to all the clients data in one string that contain list of all users, every time list change or new user connect or dcc, update all the connected clients with new list. Edited April 6, 2011 by bogQ TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost. Link to comment Share on other sites More sharing options...
kreeper Posted April 6, 2011 Author Share Posted April 6, 2011 (edited) I don't know how to add this.. And how to free up slot ? Edited April 6, 2011 by kreeper Link to comment Share on other sites More sharing options...
bogQ Posted April 6, 2011 Share Posted April 6, 2011 you have TCPCloseSocket example in help file that is showing how you close socket (dcc client) on server side $recv = TCPRecv($ConnectedSocket, 512) If $recv <> "" And $recv <> "~~bye" Then ; UPDATE EDIT CONTROL WITH DATA WE RECEIVED ;---------------------------------------------------------------- GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF) ElseIf @error Or $recv = "~~bye" Then ; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1 ;---------------------------------------------------------------- WinSetTitle($GOOEY, "", "my server - Client Disconnected") TCPCloseSocket($ConnectedSocket) $ConnectedSocket = -1 EndIf 'I don't know how to add this.' Well its easy (at least for me) you need ether to send new list or on every change to send what last happened on server side to all the clients so that clients can update their user lists. Or i did not understand you correctly? TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost. 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