Jump to content
Sign in to follow this  
Drew

Network Commands

Recommended Posts

Drew

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

Share this post


Link to post
Share on other sites
evilertoaster

You must do a TCPAccept() for each client. They will return thier own socket identifier which you can use to communicate with them.

Share this post


Link to post
Share on other sites
Drew

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...

Share this post


Link to post
Share on other sites
evilertoaster

K, so for exmaple, Here's a server-

Dim $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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×