Drew Posted May 1, 2008 Share Posted May 1, 2008 How do you make it so that you can connect more than 1 client? SERVER SIDE- Dim $iMainSocket = 0 Dim $iConnectedSocket = -1 Dim $sData = "" TCPStartup() $iMainSocket = TCPListen(@IPAddress1,7000) $Log = FileExists( @DesktopDir&"\AntiHck\Log.ini") ;~ If $Log = 0 Then ;~ IniWrite( "Log.ini", "Logged-In Ip Addresses", ""&$Ip, "User") ;~ EndIf While 1 If $iConnectedSocket >= 0 Then $sData = TCPRecv($iConnectedSocket,1024) If @error Then $iConnectedSocket = -1 ConsoleWrite("Client has disconnected" & @CRLF) ElseIf $sData Then MsgBox(0, "Message Received", $sData) EndIf Else $iConnectedSocket = TCPAccept($iMainSocket) If $iConnectedSocket >= 0 Then ConsoleWrite("Connection established" & @CRLF) EndIf EndIf Wend CLIENT CODE- Dim $iMainSocket = 0 Dim $iConnectedSocket = -1 Dim $sData = "" TCPStartup() $iMainSocket = TCPListen(@IPAddress1,7000) $Log = FileExists( @DesktopDir&"\AntiHck\Log.ini") ;~ If $Log = 0 Then ;~ IniWrite( "Log.ini", "Logged-In Ip Addresses", ""&$Ip, "User") ;~ EndIf While 1 If $iConnectedSocket >= 0 Then $sData = TCPRecv($iConnectedSocket,1024) If @error Then $iConnectedSocket = -1 ConsoleWrite("Client has disconnected" & @CRLF) ElseIf $sData Then MsgBox(0, "Message Received", $sData) EndIf Else $iConnectedSocket = TCPAccept($iMainSocket) If $iConnectedSocket >= 0 Then ConsoleWrite("Connection established" & @CRLF) EndIf EndIf Wend Link to comment Share on other sites More sharing options...
evilertoaster Posted May 1, 2008 Share Posted May 1, 2008 You must do a TCPAccept() for each client. They will return thier own socket identifier which you can use to communicate with them. Link to comment Share on other sites More sharing options...
Drew Posted May 2, 2008 Author Share Posted May 2, 2008 Can you show me an example of how to do this? I'm trying to make an array to do it , because i don't know how many sockets i need... but i can't figure it out... Link to comment Share on other sites More sharing options...
evilertoaster Posted May 2, 2008 Share Posted May 2, 2008 K, so for exmaple, Here's a server- expandcollapse popupDim $iMainSocket = 0 Dim $iConnectedSocket = -1 Dim $sData = "" Dim $ClientArray[1] TCPStartup() $iMainSocket = TCPListen(@IPAddress1,7000) While 1 $iConnectedSocket = TCPAccept($iMainSocket) if $iConnectedSocket > 0 then _AddSocket($iConnectedSocket) ConsoleWrite("Connection established - Client "&UBound($ClientArray)-1 & @CRLF) EndIf _PollClients() Wend func _AddSocket($inSock) ReDim $ClientArray[UBound($ClientArray)+1] $ClientArray[UBound($ClientArray)-1]=$inSock EndFunc func _PollClients() $i=1 while $i<=UBound($ClientArray)-1 $sData=TCPRecv($ClientArray[$i],1014) If @error Then ConsoleWrite("Client "&$i&" - "&@error & @CRLF) Elseif $sData then MsgBox(0, "Message Received - Client "&$i, $sData) EndIf $i+=1 Wend EndFuncoÝ÷ Ù©ÝêÞËayÉbz{ZºÚ"µÍÔÝ HÂIÌÍÜÛØÚÏUÔÛÛXÝ TYÜÌK Ì B[[ ÌÍÜÛØÚÉÉÝËLBÚ[HBHÔÙ[ ÌÍÜÛØÚË ][ÝÔÛÛYH]I][ÝÊBHYÜ[ÙÐÞ ][ÝÙÜ][ÝËÜBHÛY L BÑ[ So you can see, one server can handle multiple clients connecting to it. 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