Jump to content

Recommended Posts

Posted

I checked out the Client / Server Communication thread and found it quite cool. So I decided to try something but it doesn't seem to be working. Is it possible to have multiple connections on a socket?

Posted (edited)

How would I go about doing that? I took a try at it but I dout it will work.

Dim $server=@IPAddress1
Dim $port=33891
Dim $CS[50]=-1

TCPStartUp()

$MS=TCPListen($server,$port)
If $MS=-1 Then Exit

While 1
for $i=1 to UBound($CS)
if $CS[$i] < 0 then $CS[$i]=TCPAccept( $MS)
next
Wend
Edited by Hellfire
Posted

Well here is my horrible code, it isn't working.

This is the server script. The client script is working because I tested it on an already working server script.

I'm trying to make this support multiple connections, I have it set at 25 connections max, or atleast trying to.

This was working before I tried to make it able to support more than 1 connection.

Dim $server = @IPAddress1
Dim $port = 33891
Dim $data=""
Dim $CS[25]
Dim $ip[25]
Dim $sock, $n
Dim $i=1

;------Start TCP Services
TCPStartUp()
;------

;------Listening Socket
$MS = TCPListen($server,$port)
;------

$n=0
While 1

;------Look for a client connection
    while 1
For $i = 1 to UBound($CS)
    $sock = TCPAccept($MS)
    If $sock <> -1 Then
        
        If $ip[$i] Not = SocketToIP($sock) then
        $CS[$i] = $sock
        $ip[$i]=SocketToIP($CS[$i])
                $n += 1
endif
    EndIf
Next
if $n >=1 then exitloop
wend
;------


;------Recieve Data
For $i=1 to $n
 $data = TCPRecv( $CS[$i], 2048)
 if $data <> "" then
     msgbox(0,"",$data)
         $d=StringSplit($data," ")
Select
    Case $d[1] = "NA"
        AccountCreate($d[2],$d[3],$d[4],$d[5],$i)
        
        
    Case $d[1] = "LI"
        Login($d[2],$d[3],$i)
        
    Case $d[1] = "RE"
        Refresh($i)
         
Endselect
endif
next
Wend
;------


Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int_ptr",DLLStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc

Func AccountCreate($username,$password,$email,$ipa,$usernumber)
    $file= @scriptdir & "\Accounts\" & $username & ".txt"
if FileExists($file) then
    else
_filecreate($file)
    $f=FileOpen($file,1)
FileWrite($f, $password & @CRLF & $email & @CRLF & $ipa)
FileClose($f)
endif
endfunc

Func Login($username,$password,$usernumber)
    $file= @SCRIPTDIR & "\Accounts\" & $username & ".txt"
    if FileExists($file) Then
        if FileReadLine($file,1)=$password then 
            
        EndIf
    Else

        endif
    endfunc
    
Func Refresh($usernumber)
    $z=1
    $x=1
    $a=DirGetSize(@scriptdir & "\Items\"
    For $z = 1 to $a
        For $x=1 to 3
    ;TCPSEND($CS[$usernumber],FileReadLine(@scriptdir & "\Items\" & $z,$x)
    Next
    next
    Endfunc
Posted (edited)

You could check out my crappy UDFs which can be found http://www.autoitscript.com/fileman/users/Helge/udfs.htm...

edit : stupid typo

Edited by Helge
Posted

no, you can't have multiple connections on 1 socket.. you have to check on multiple sockets which is possible by using the sockets in a array, you can do this by doing something like this:

TcpStartup()

$mainsocket = TCPListen(blabla)

While 1
  $Sockets[$emptysocket] = TCP Accept($mainsocket)
  If Not $sockets[$emptysocket] = -1 then
      $emptysocket + 1
  endif
Wend

I hope you're good to go after this

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...