Sign in to follow this  
Followers 0
Alexxander

TCP listen

6 posts in this topic

#1 ·  Posted (edited)

Hello folks 

i want to make a simple server that show data that is send it a sissified port

this code can receive only the first text string that is being send then the sever stop

TCPStartup()
$listen = TCPListen(@IPAddress1,403)

Do
$accept = TCPAccept($listen)
Until $accept <> -1

Do
    $recive = TCPRecv($accept,10000000)
Until $recive <> ""
MsgBox(0,0,$recive)

i want to make it always running without stopping when any text recived 

i tried 

TCPStartup()
$listen = TCPListen(@IPAddress1,403)
while 1
Do
$accept = TCPAccept($listen)
Until $accept <> -1

Do
    $recive = TCPRecv($accept,10000000)
Until $recive <> ""
MsgBox(0,0,$recive)
wend

and this

TCPStartup()
$listen = TCPListen(@IPAddress1,403)

while 1
$accept = TCPAccept($listen)
$recive = TCPRecv($accept,10000000)
if $recive <> "" Then MsgBox(0,0,$recive)
ConsoleWrite($recive)
WEnd

both are not functioning as i want any ideas ?

Edited by Alexxander

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

ok finally i was able to solve the problem this is the working code

TCPStartup()
$TCPL = TCPListen(@IPAddress1, 403)

Do
    $TCPA = TCPAccept($TCPL)
Until $TCPA <> -1

While 1
    $TCPR = TCPRecv($TCPA, 9999999)
    If $TCPR <> "" Then
        ConsoleWrite($TCPR)
    EndIf
WEnd

is it possible to make it listen to more than one client at the same time ?

Edited by Alexxander

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

try this 

 
TCPStartup()

Global $TCPL = TCPListen(@IPAddress1, 403)

Global $arrayclien[1]

Func _TCPAccept()
    Local $TCPA = TCPAccept($TCPL)
    If @error Then Return
    ReDim $arrayclien[$arrayclien[0] + 2]
    $arrayclien[0] += 1
    $arrayclien[$arrayclien[0]] = $TCPA
EndFunc   ;==>_TCPAccept

Local $TCPR
While 1
    _TCPAccept()
    For $oi = 1 To $arrayclien[0]
        $TCPR = TCPRecv($arrayclien[$oi], 9999999)
        If @error = -1 Then _Dellclien($arrayclien[$oi])
        If $TCPR <> "" Then
            ConsoleWrite($TCPR)
        EndIf
    Next
WEnd
Func _Dellclien($Soket)
    Dim $Arryctmp[$arrayclien[0]]
    For $oi = 1 To $arrayclien[0]
        If $Soket = $arrayclien[$oi] Then ContinueLoop
        $i = +1
        $Arryctmp[$i] = $arrayclien[$oi]
    Next
    $Arryctmp[0] = $arrayclien[0] - 1
    $arrayclien = $Arryctmp
EndFunc   ;==>_Dellclien
Edited by FinishHim

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

;)  Good luck

Mini chat Multi client

Server

Opt("GUIONEVENTMODE", 1)
GUICreate("Form1", 475, 304)
GUISetOnEvent(-3, "_EXITdb")
Global $Edit1 = GUICtrlCreateEdit("", 8, 8, 329, 241)
Global $Input1 = GUICtrlCreateInput("", 24, 264, 225, 21)
GUICtrlCreateButton("Send", 256, 264, 67, 25)
GUICtrlSetOnEvent(-1, "_Senddata")

GUICtrlCreateLabel("Select Sokey ", 360, 8, 70, 17)
Global $List1 = GUICtrlCreateListView("Soket", 352, 32, 113, 253)

GUISetState(@SW_SHOW)

TCPStartup()

Global $TCPL = TCPListen(@IPAddress1, 403)
If @error Then Exit

Global $arrayclien[1][2]

Func _TCPAccept()
    Local $TCPA = TCPAccept($TCPL)
    If $TCPA = -1 Then Return
    ReDim $arrayclien[$arrayclien[0][0] + 2][2]
    $arrayclien[0][0] += 1
    $arrayclien[$arrayclien[0][0]][0] = $TCPA
    $arrayclien[$arrayclien[0][0]][1] = GUICtrlCreateListViewItem($TCPA, $List1)
EndFunc   ;==>_TCPAccept

Local $Resv, $TCPR, $TCPRtmp
While 1
    Sleep(10)
    _TCPAccept()
    For $oi = 1 To $arrayclien[0][0]
        
        If $oi > $arrayclien[0][0] Then ExitLoop ;;;
        $Resv = _TCPRecv($arrayclien[$oi][0], 20)
        If $Resv <> "" Then _Consolwirite($arrayclien[$oi][0] & " : " & $Resv)
        
    Next
WEnd

Func _Consolwirite($txt)
    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & $txt & @CRLF)
EndFunc   ;==>_Consolwirite
Func _TCPRecv($Soker, $Len)
    $TCPR = TCPRecv($Soker, $Len)
    If @error Then _Dellclien($Soker)
    
    $TCPRtmp = $TCPR
    If $TCPR = "" Then Return $TCPRtmp

    Do
        $TCPR = TCPRecv($Soker, $Len)
        If $TCPR = "" Then ExitLoop
        $TCPRtmp &= $TCPR
    Until $TCPR = ""

    Return $TCPRtmp
EndFunc   ;==>_TCPRecv
Func _TCPSend($Soker, $mdata)
    TCPSend($Soker, $mdata)
EndFunc   ;==>_TCPSend
Func _Senddata()
    If GUICtrlRead(GUICtrlRead($List1)) = "" Then Return
    If GUICtrlRead($Input1) = "" Then Return
    _TCPSend(GUICtrlRead(GUICtrlRead($List1)), GUICtrlRead($Input1))
EndFunc   ;==>_Senddata
Func _EXITdb()
    TCPShutdown()
    Exit
EndFunc   ;==>_EXITdb
Func _Dellclien($Soket)
    Dim $Arryctmp[$arrayclien[0][0]][2], $i
    For $oi = 1 To $arrayclien[0][0]
        If $Soket = $arrayclien[$oi][0] Then
            GUICtrlDelete($arrayclien[$oi][1])
            ContinueLoop
        EndIf
        $i += 1
        $Arryctmp[$i][0] = $arrayclien[$oi][0]
        $Arryctmp[$i][1] = $arrayclien[$oi][1]
    Next
    $Arryctmp[0][0] = $arrayclien[0][0] - 1
    $arrayclien = $Arryctmp
EndFunc   ;==>_Dellclien

Clien

Opt("GUIONEVENTMODE", 1)
GUICreate("Form1", 345, 304, 192, 124)
GUISetOnEvent(-3, "_EXITdb")
Global $Edit1 = GUICtrlCreateEdit("", 8, 8, 329, 241)
Global $Input1 = GUICtrlCreateInput("", 24, 264, 225, 21)
GUICtrlCreateButton("Send", 256, 264, 67, 25)
GUICtrlSetOnEvent(-1, "_Senddata")
GUISetState(@SW_SHOW)
TCPStartup()

Global $TCPL

_TCPConnect()

Func _TCPConnect()
    While 1
        $TCPL = TCPConnect(@IPAddress1, 403)
        If $TCPL <> -1 Then Return
    WEnd
EndFunc   ;==>_TCPConnect

Local $Resv, $TCPR, $TCPRtmp
While 1
    Sleep(10)
    $Resv = _TCPRecv($TCPL, 20)
    If $Resv <> "" Then _Consolwirite($TCPL & " : " & $Resv)
WEnd

Func _Consolwirite($txt)
    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & $txt & @CRLF)
EndFunc   ;==>_Consolwirite
Func _TCPRecv($Soker, $Len)
    $TCPR = TCPRecv($Soker, $Len)
    If @error Then _TCPConnect()

    $TCPRtmp = $TCPR
    If $TCPR = "" Then Return $TCPRtmp

    Do
        $TCPR = TCPRecv($Soker, $Len)
        If $TCPR = "" Then ExitLoop
        $TCPRtmp &= $TCPR
    Until $TCPR = ""

    Return $TCPRtmp
EndFunc   ;==>_TCPRecv
Func _TCPSend($Soker, $mdata)
    TCPSend($Soker, $mdata)
EndFunc   ;==>_TCPSend

Func _Senddata()
    If GUICtrlRead($Input1) = "" Then Return
    _TCPSend($TCPL, GUICtrlRead($Input1))
EndFunc   ;==>_Senddata
Func _EXITdb()
    TCPShutdown()
    Exit
EndFunc   ;==>_EXITdb
Edited by FinishHim

Share this post


Link to post
Share on other sites

seems very good , i will try them and study the code as seen as possible .. thanks a lot bro


Share this post


Link to post
Share on other sites

:D *Update  :support multi connection on client

Server 

Opt("GUIONEVENTMODE", 1)
GUICreate("Form1", 475, 304)
GUISetOnEvent(-3, "_EXITdb")
Global $Edit1 = GUICtrlCreateEdit("", 8, 8, 329, 241)
Global $Input1 = GUICtrlCreateInput("", 24, 264, 180, 21)
GUICtrlCreateButton("Send", 208, 264, 47, 25)
GUICtrlSetOnEvent(-1, "_Senddata")

GUICtrlCreateLabel("Select socket ", 360, 8, 70, 17)
Global $List1 = GUICtrlCreateListView("Socket list", 352, 32, 113, 253)

GUISetState(@SW_SHOW)

TCPStartup()

_Consolwrite("-> listener started on port : " & 403 & " <-")
Global $TCPL = TCPListen(@IPAddress1, 403)
If @error Then Exit

Global $arrayclien[1][3]
Global $Packmsg = "[Msgpack]"
Global $Packend = "[Endpack]"
Global $PackNewc = "[Newcpack]"
Global $PackSplinfo = "[Splinfo]"
Global $PackDelc = "[Delcpack]"

Func _TCPAccept()
    Local $TCPA = TCPAccept($TCPL)
    If $TCPA = -1 Then Return
    Do
        $res = TCPRecv($TCPA, 50)
    Until $res <> ""
    _Sptpack($res, $TCPA)
    ReDim $arrayclien[$arrayclien[0][0] + 2][3]
    $arrayclien[0][0] += 1
    $arrayclien[$arrayclien[0][0]][0] = $TCPA
    $arrayclien[$arrayclien[0][0]][1] = GUICtrlCreateListViewItem($TCPA, $List1)
    _Consolwrite("-> New connection socket : " & $TCPA & " <-")
    For $m = 1 To $arrayclien[0][0] - 1
        _TCPSend($TCPA, $PackNewc & $arrayclien[$m][0] & $Packend)
    Next
    For $m = 1 To $arrayclien[0][0] - 1
        _TCPSend($arrayclien[$m][0], $PackNewc & $TCPA & $Packend)
    Next
EndFunc   ;==>_TCPAccept

Local $Resv, $TCPR, $TCPRtmp
While 1
    Sleep(10);;
    _TCPAccept()
    For $oi = 1 To $arrayclien[0][0]
        If $oi > $arrayclien[0][0] Then ExitLoop ;;;
        $Resv = _TCPRecv($arrayclien[$oi][0], 20)
    Next
WEnd

Func _Consolwrite($txt)
    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & $txt & @CRLF)
EndFunc   ;==>_Consolwrite
Func _TCPRecv($Soker, $Len)
    $TCPR = TCPRecv($Soker, $Len)
    If @error Then
        _Consolwrite("-> Client has been disconnected : " & $Soker & " <-")
        _Dellclien($Soker)
        For $m = 1 To $arrayclien[0][0]
            _TCPSend($arrayclien[$m][0], $PackDelc & $Soker & $Packend)
        Next
    EndIf
    
    $TCPRtmp = $TCPR
    If $TCPR = "" Then Return $TCPRtmp

    Do
        $TCPR = TCPRecv($Soker, $Len)
        If $TCPR = "" Then ExitLoop
        $TCPRtmp &= $TCPR
    Until $TCPR = ""

    _Sptpack($TCPRtmp, $Soker)
EndFunc   ;==>_TCPRecv
Func _Sptpack($mPack, $Soker)
    Local $Splitpack = StringSplit($mPack, $Packend, 1)
    If @error Then Return
    For $ii = 0 To $Splitpack[0]
        _Getpacktyp($Splitpack[$ii], $Soker)
    Next
EndFunc   ;==>_Sptpack
Func _Getpacktyp($mPack, $Msoket)
    Select
        Case StringInStr($mPack, $Packmsg) > 0
            $Splitinfo = StringSplit(StringTrimLeft($mPack, StringInStr($mPack, $Packmsg) + StringLen($Packmsg) - 1), $PackSplinfo, 1)
            If @error Then Return
            If $Splitinfo[1] = "*Server*" Then
                _Consolwrite($Msoket & " Say : " & $Splitinfo[2])
            Else
                _TCPSend($Splitinfo[1], $Packmsg & $Msoket & $PackSplinfo & $Splitinfo[2] & $Packend)
            EndIf
        Case StringInStr($mPack, $PackNewc) > 0
            
    EndSelect
EndFunc   ;==>_Getpacktyp
Func _TCPSend($Soker, $mdata)
    TCPSend($Soker, $mdata)
EndFunc   ;==>_TCPSend
Func _Senddata()
    Local $Redctrlv = GUICtrlRead(GUICtrlRead($List1))
    If $Redctrlv = "" Then
        _Consolwrite("-> Please select a socket first <-")
        Return
    EndIf
    Local $Getsoket = StringSplit($Redctrlv, "|", 1)
    If @error Then Return

    If GUICtrlRead($Input1) = "" Then Return
    _TCPSend($Getsoket[1], $Packmsg & "*Server*" & $PackSplinfo & GUICtrlRead($Input1) & $Packend)
EndFunc   ;==>_Senddata
Func _Dellclien($Soket)
    Dim $Arryctmp[$arrayclien[0][0]][3], $i
    For $oi = 1 To $arrayclien[0][0]
        If $Soket = $arrayclien[$oi][0] Then
            GUICtrlDelete($arrayclien[$oi][1])
            ContinueLoop
        EndIf
        $i += 1
        For $m = 0 To 2
            $Arryctmp[$i][$m] = $arrayclien[$oi][$m]
        Next
    Next
    $Arryctmp[0][0] = $arrayclien[0][0] - 1
    $arrayclien = $Arryctmp
EndFunc   ;==>_Dellclien
Func _EXITdb()
    TCPShutdown()
    Exit
EndFunc   ;==>_EXITdb

Clien

Opt("GUIONEVENTMODE", 1)
GUICreate("Form1", 475, 304)
GUISetOnEvent(-3, "_EXITdb")
Global $Edit1 = GUICtrlCreateEdit("", 8, 8, 329, 241)
Global $Input1 = GUICtrlCreateInput("", 24, 264, 225, 21)
GUICtrlCreateButton("Send", 268, 264, 47, 25)
GUICtrlSetOnEvent(-1, "_Senddata")

GUICtrlCreateLabel("Select socket ", 360, 8, 70, 17)
Global $List1 = GUICtrlCreateListView("Socket list", 352, 32, 113, 253)

GUISetState(@SW_SHOW)
TCPStartup()

Global $TCPL
Global $arrayclien[1][2]

Global $Packmsg = "[Msgpack]"
Global $PackNewc = "[Newcpack]"
Global $PackDelc = "[Delcpack]"
Global $PackSplinfo = "[Splinfo]"
Global $Packend = "[Endpack]"

_TCPConnect()

Func _TCPConnect()
    _Consolwrite("-> connection was started wait for response <-")
    While 1
        $TCPL = TCPConnect(@IPAddress1, 403)
        If $TCPL <> -1 Then ExitLoop
    WEnd
    _Consolwrite("-> A connection was successfully established with the server <-")
    _TCPSend($TCPL, $Packmsg & "Hallo :)" & $Packend)
    ReDim $arrayclien[$arrayclien[0][0] + 2][2]
    $arrayclien[0][0] += 1
    $arrayclien[$arrayclien[0][0]][0] = "*Server*"
    $arrayclien[$arrayclien[0][0]][1] = GUICtrlCreateListViewItem("*Server*", $List1)
EndFunc   ;==>_TCPConnect

Local $Resv, $TCPR, $TCPRtmp
While 1
    Sleep(10)
    _TCPRecv($TCPL, 20)
WEnd

Func _Consolwrite($txt)
    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & $txt & @CRLF)
EndFunc   ;==>_Consolwrite
Func _TCPRecv($Soker, $Len)
    $TCPR = TCPRecv($Soker, $Len)
    If @error Then
        _Consolwrite("-> Server has been shut down <-")
        _TCPConnect()
    EndIf

    $TCPRtmp = $TCPR
    If $TCPR = "" Then Return $TCPRtmp

    Do
        $TCPR = TCPRecv($Soker, $Len)
        If $TCPR = "" Then ExitLoop
        $TCPRtmp &= $TCPR
    Until $TCPR = ""
    
    _Sptpack($TCPRtmp)
EndFunc   ;==>_TCPRecv
Func _Sptpack($mPack)
    Local $Splitpack = StringSplit($mPack, $Packend, 1)
    If @error Then Return
    For $ii = 0 To $Splitpack[0]
        _Getpacktyp($Splitpack[$ii])
    Next
EndFunc   ;==>_Sptpack
Func _Getpacktyp($mPack)
    Select
        Case StringInStr($mPack, $Packmsg) > 0
            $Splitinfo = StringSplit(StringTrimLeft($mPack, StringInStr($mPack, $Packmsg) + StringLen($Packmsg) - 1), $PackSplinfo, 1)
            If @error Then Return
            _Consolwrite($Splitinfo[1] & " Say : " & $Splitinfo[2])
        Case StringInStr($mPack, $PackNewc) > 0
            ReDim $arrayclien[$arrayclien[0][0] + 2][2]
            $arrayclien[0][0] += 1
            $arrayclien[$arrayclien[0][0]][0] = StringTrimLeft($mPack, StringInStr($mPack, $PackNewc) + StringLen($PackNewc) - 1)
            $arrayclien[$arrayclien[0][0]][1] = GUICtrlCreateListViewItem($arrayclien[$arrayclien[0][0]][0], $List1)
        Case StringInStr($mPack, $PackDelc) > 0
            $Mclien = StringTrimLeft($mPack, StringInStr($mPack, $PackDelc) + StringLen($PackDelc) - 1)
            _Consolwrite("-> Client has been disconnected : " & $Mclien & " <-")
            _Dellclien($Mclien)
    EndSelect
EndFunc   ;==>_Getpacktyp
Func _Dellclien($Soket)
    Dim $Arryctmp[$arrayclien[0][0]][2], $i
    For $oi = 1 To $arrayclien[0][0]
        If $Soket = $arrayclien[$oi][0] Then
            GUICtrlDelete($arrayclien[$oi][1])
            ContinueLoop
        EndIf
        $i += 1
        For $m = 0 To 1
            $Arryctmp[$i][$m] = $arrayclien[$oi][$m]
        Next
    Next
    $Arryctmp[0][0] = $arrayclien[0][0] - 1
    $arrayclien = $Arryctmp
EndFunc   ;==>_Dellclien
Func _TCPSend($Soker, $mdata)
    TCPSend($Soker, $mdata)
EndFunc   ;==>_TCPSend
Func _Senddata()
    Local $Redctrlv = GUICtrlRead(GUICtrlRead($List1))
    If $Redctrlv = "" Then
        _Consolwrite("-> Please select a socket first <-")
        Return
    EndIf
    Local $Getsoket = StringSplit($Redctrlv, "|", 1)
    If @error Then Return

    If GUICtrlRead($Input1) = "" Then Return
    _TCPSend($TCPL, $Packmsg & $Getsoket[1] & $PackSplinfo & GUICtrlRead($Input1) & $Packend)
EndFunc   ;==>_Senddata
Func _EXITdb()
    TCPShutdown()
    Exit
EndFunc   ;==>_EXITdb

246525Sanstitre2.jpg

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  
Followers 0