CodyBarrett Posted July 23, 2009 Share Posted July 23, 2009 (edited) recently i got a PM asking how to make a TCP server....so here i wrote an example server & client this morning...(client you can start first.. provided that the server starts and connects before 3 seconds is upSERVER!!!!!!expandcollapse popup$0 = 0 $00 = 0 $000 = 0 $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...',Random (100, 10000,1)) ;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)',Random (1,1000, 1)) ;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) 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, '^') $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' _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 EndFuncCLIENT!!!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...', Random(100, 10000, 1)) ;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!!! For $0 = 0 To 10 $Socket = TCPConnect($ip, $port) ;Connects to an open socket on the server... If $Socket <> -1 Then ExitLoop TCPCloseSocket($Socket) Sleep(300) Next If $Socket = -1 Then _Exit () TCPSend ($Socket, @UserName & '^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, $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 () TCPCloseSocket ($Socket) TCPShutdown() Exit EndFunc ;==>_Exit_you can replace the Tooltips with a GUI and controls... but for now i'll leave it at that... BTCP in my sig is also a working TCP multiclient Chat roomBTW!i don't know how to configure it over a router\highspeed for i have dialup and a VPNhave fun learning!EDITi made the client use GUI controls.. it just is more simpler that way Edited October 11, 2009 by CodyBarrett [size="1"][font="Tahoma"][COMPLETED]-----[FAILED]-----[ONGOING]VolumeControl|Binary Converter|CPU Usage| Mouse Wrap |WinHide|Word Scrammbler|LOCKER|SCREEN FREEZE|Decisions Decisions|Version UDF|Recast Desktop Mask|TCP Multiclient EXAMPLE|BTCP|LANCR|UDP serverless|AIOCR|OECR|Recast Messenger|AU3C|Tik-Tak-Toe|Snakes & Ladders|BattleShips|TRON|SNAKE_____________________[u]I love the Helpfile it is my best friend.[/u][/font][/size] Link to comment Share on other sites More sharing options...
petenyc1 Posted October 29, 2011 Share Posted October 29, 2011 This is awesome!! Just a couple of questions please. I know that this was a quick example (amazing by the way that you just through it together). 1. How would you recommend recovery from the server going down and then restarting? 2. I see that you go through and find the first available socket on the server. What about when a client exits gracefully and ungracefully? Thanks this was a HUGE help!!! Peter Link to comment Share on other sites More sharing options...
darippaxp Posted December 16, 2011 Share Posted December 16, 2011 thanks for the examples, im gonna try to work with em 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