Jump to content
Sign in to follow this  
Andreik

TCP - Free socket

Recommended Posts

Andreik

Hi all. I write this script but don't work like I want. I want to create a server that accept 4 connections, and everytime look for new connection if a socket is free. Can somebosy tell me what is wrong in this code.

Global $SERVER
Global $MAX = 4
Global $SOCKET[$MAX]
Global $CURRENT = 0

TCPStartup()
$SERVER = TCPListen(@IPAddress1,7711,$MAX)
If @error Then
    MsgBox(0,"ERROR",@error)
    Exit
EndIf

While True
    If IsNumber($CURRENT) Then
        $SOCKET[$CURRENT] = TCPAccept($SERVER)
        $CURRENT = GetFreeSocket()
    Else
        $CURRENT = GetFreeSocket()
    EndIf
    For $INDEX = 0 To $MAX-1
        If $SOCKET[$INDEX] <> -1 And $SOCKET[$INDEX] <> "" Then
            $RECV = TCPRecv($SOCKET[$INDEX],512)
            If $RECV <> "" Then MsgBox(0,"",$RECV)
        EndIf
    Next
    Sleep(20)
WEnd

Func GetFreeSocket()
    Local $INDEX,$FULL = "SERVER_FULL"
    For $INDEX = 0 To $MAX-1
        $TEST = TCPRecv($SOCKET[$INDEX],32)
        If @error Then Return $INDEX
    Next
    Return $FULL
EndFunc
Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites
Andreik

I found a solution, not the best but work:

Global $SERVER
Global $MAX = 4
Global $SOCKET[$MAX]
Global $CURRENT = 0

HotKeySet("{PAUSE}","Quit")

TCPStartup()
$SERVER = TCPListen(@IPAddress1,7711,$MAX)
If @error Then
    MsgBox(0,"ERROR",@error)
    Exit
EndIf

While True
    If CheckFreeSocket($CURRENT) Then $SOCKET[$CURRENT] = TCPAccept($SERVER)
    If $SOCKET[$CURRENT] <> -1 Then
        $CURRENT += 1
        If $CURRENT = $MAX Then $CURRENT = 0
    EndIf
    For $INDEX = 0 To $MAX-1
        If $SOCKET[$INDEX] <> -1 And $SOCKET[$INDEX] <> "" Then
            $RECV = TCPRecv($SOCKET[$INDEX],512)
            If $RECV <> "" Then
                If $RECV = "GET_INDEX" Then
                    TCPSend($SOCKET[$INDEX],$INDEX)
                Else
                    MsgBox(0,"RECEIVED DATA",$RECV)
                EndIf
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func CheckFreeSocket($CURRENT)
    $TEST = TCPRecv($SOCKET[$CURRENT],16)
    If @error Then
        Return True
    Else
        Return False
    EndIf
EndFunc

Func Quit()
    TCPCloseSocket($SERVER)
    TCPShutdown()
    Exit
EndFunc

When the words fail... music speaks

Share this post


Link to post
Share on other sites
Xand3r

Global $SERVER
Global $MAX = 4
Global $SOCKET[$MAX]
Global $CURRENT = 0
Global $SERVER_FULL="The Server is full"
Global $RECV_LEN=4096
For $i=0 To $MAX-1
    $SOCKET[$i]=""
Next
TCPStartup()
$SERVER = TCPListen(@IPAddress1,7711,$MAX)
If @error Then
    MsgBox(0,"ERROR",@error)
    Exit
EndIf

While True
    $New_TCP_Socket=TCPAccept($SERVER)
    If $New_TCP_Socket<>-1 Then TCP_Accept($New_TCP_Socket)
    $recv=TCP_Recv(-1)
    If Not @error Then
        For $i=0 To UBound($recv)-1
            ConsoleWrite("Data from "&$i&": "&$recv[$i] & @CRLF)
        Next
    EndIf
    Sleep(10)
WEnd

Func TCP_Accept($sock)
    ConsoleWrite("New Sock!" & @CRLF)
    For $i=0 To $MAX-1
        If $socket[$i]="" Then
            $socket[$i]=$sock
            ConsoleWrite("!Added sock:" & $i & @CRLF)
            Return $i
        EndIf
    Next
    ConsoleWrite("Server was full!" & @CRLF)
    Return 0*TCPSend($sock,$SERVER_FULL)*TCPCloseSocket($sock)
EndFunc
Func Close_Socket($index)
    TCPCloseSocket($SOCKET[$index])
    $SOCKET[$index]=""
    ConsoleWrite("!Closed sock:" & $index & @CRLF)
EndFunc

Func TCP_Recv($sock)
    If $sock<>-1 Then
        Dim $return[1]
        $return[0]=TCPRecv($SOCKET[$sock],$RECV_LEN)
        If @error Then
            $return[0]=0*Close_Socket($sock)
            Return SetError(1)
        EndIf
    EndIf
    Dim $return[$MAX]
    Local $has_data=0
    For $i=0 To $MAX-1
        If $SOCKET[$i]<>"" Then
            $return[$i]=TCPRecv($socket[$i],$RECV_LEN)
            If $return[$i]<>"" Then $has_data=1
            If @error Then
                $return[$i]=-1
                Close_Socket($i)
            EndIf
        EndIf
    Next
    If Not $has_data Then
        ReDim $return[1]
        $return[0]=0
        SetError(1,0,$return)
    EndIf
    Return $return
EndFunc


Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites
Andreik

Yes, it's a more good version. Thanks.


When the words fail... music speaks

Share this post


Link to post
Share on other sites
Xand3r

np ... just let me know if you need more help with it


Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

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  

×