Sign in to follow this  
Followers 0
Cater

Two socket tcp server

7 posts in this topic

#1 ·  Posted (edited)

Can someone explain how to create two socket in same time, so two clients can connect to server(TCP)? i cant find it on the forum.

Edited by Cater

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Can someone explain how to create two socket in same time, so two clients can connect to server(TCP)? i cant find it on the forum.

Is no necessary to create two sockets. You can use this old code from autoit forum:

Global $iCurrentSocket = 0, $oListenSocket, $oConnectedSocket[25]
TCPStartup()
$oListenSocket = TCPListen(@IPAddress1, 16802, 25)
If $oListenSocket = -1 Then
        MsgBox(0, "Error", "Unable to start listening on port 16802")
        Exit
EndIf
While 1
    $oConnectedSocket[$iCurrentSocket] = TCPAccept($oListenSocket)
    ConsoleWrite($oConnectedSocket[$iCurrentSocket]&@CR)
    If $oConnectedSocket[$iCurrentSocket] <> -1 Then
        $iCurrentSocket = $iCurrentSocket + 1
    EndIf
    For $i = 0 To 24
        If $oConnectedSocket[$i] <> -1 Or $oConnectedSocket[$i] <> "" Then
            $sResv = TCPRecv($oConnectedSocket[$i], 512)
            If $sResv <> "" Then
            ...
            EndIf
    EndIf
    Next
    Sleep(20)
WEnd

You can connect 25 clients on this server.

Hope that helps!

Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Server:

Global $iCurrentSocket = 0, $oListenSocket, $oConnectedSocket[25]
TCPStartup()
$oListenSocket = TCPListen(@IPAddress1, 16802, 25)
If $oListenSocket = -1 Then
        MsgBox(0, "Error", "Unable to start listening on port 16802")
        Exit
EndIf
While 1
    $oConnectedSocket[$iCurrentSocket] = TCPAccept($oListenSocket)
    ConsoleWrite($oConnectedSocket[$iCurrentSocket]&@CR)
    If $oConnectedSocket[$iCurrentSocket] <> -1 Then
        $iCurrentSocket = $iCurrentSocket + 1
    EndIf
    For $i = 0 To 24
        If $oConnectedSocket[$i] <> -1 Or $oConnectedSocket[$i] <> "" Then
            $sResv = TCPRecv($oConnectedSocket[$i], 512)
            If $sResv <> "" Then
            For $c = 0 to 24
                Local $send = "1"
                TCPSend($oConnectedSocket[$c], "Socket[" & $i & "]: " & $sResv)
            Next
            ElseIf @error Then
                TCPCloseSocket($oConnectedSocket[$i])
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Why if one of socket is disconnected and new client connect to disconnected socket then this client get same message two times? :<

Edited by Cater

Share this post


Link to post
Share on other sites

Global $iCurrentSocket = 0, $oListenSocket, $oConnectedSocket[25]
TCPStartup()
$oListenSocket = TCPListen(@IPAddress1, 16802, 25)
If $oListenSocket = -1 Then
        MsgBox(0, "Error", "Unable to start listening on port 16802")
        Exit
EndIf
$n=-1
While 1
    $newConnection = TCPAccept($oListenSocket)
    If $newConnection <> -1 Then
        $n=$n+1
        $oConnectedSocket[$n]=$newConnection
    EndIf
    For $i = 0 To $n
        $recv = TCPRecv($oConnectedSocket[$i], 2048)
        If @error Then
            For $j=$i To $n-1
                $oConnectedSocket[$j]=$oConnectedSocket[$j+1]
            Next
        $n=$n-1
        Else
            For $j=0 to $n
                TCPSend($oConnectedSocket[$j], $recv)
            Next
        EndIf   
    Next
    Sleep(20)
WEnd

This should work. :)

Share this post


Link to post
Share on other sites

But every new connection have brand new socket, I want to connect new connection to socket which was disconnected with out getting multiply msg.

server:

Global $iCurrentSocket = 0, $oListenSocket, $oConnectedSocket[25]
TCPStartup()
$oListenSocket = TCPListen(@IPAddress1, 16802, 25)
If $oListenSocket = -1 Then
        MsgBox(0, "Error", "Unable to start listening on port 16802")
        Exit
EndIf
While 1
    $oConnectedSocket[$iCurrentSocket] = TCPAccept($oListenSocket)
    ConsoleWrite($oConnectedSocket[$iCurrentSocket]&@CR)
    If $oConnectedSocket[$iCurrentSocket] <> -1 Then
        $iCurrentSocket = $iCurrentSocket + 1
    EndIf
    For $i = 0 To 24
        If $oConnectedSocket[$i] <> -1 Or $oConnectedSocket[$i] <> "" Then
            $sResv = TCPRecv($oConnectedSocket[$i], 512)
            If $sResv <> "" Then
            For $c = 0 to 24
                TCPSend($oConnectedSocket[$c], "Socket[" & $i & "]: " & $sResv)
            Next
            ElseIf @error Then
                TCPCloseSocket($oConnectedSocket[$i])
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Client:

#include <GUIConstants.au3>
Example()

Func Example()
   ; Set Some reusable info
   ;--------------------------
    Local $ConnectedSocket, $szData
   ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 16802
    
    $Gui = GUICreate("Client(Connected: " & $szIPADDRESS & " : " & $nPORT & ")", 315, 275, 419, 185)
    $Group = GUICtrlCreateGroup(" Client ", 8, 8, 297, 257)
    $Edit = GUICtrlCreateEdit("", 17, 56, 281, 201)
    $Send = GUICtrlCreateButton("Wyslij", 17, 24, 81, 25, 0)
    $Input = GUICtrlCreateInput("", 110, 26, 185, 21)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    Sleep(2000)

   ; Start The TCP Services
   ;==============================================
    TCPStartup()

   ; Initialize a variable to represent a connection
   ;==============================================
    $ConnectedSocket = -1

   ;Attempt to connect to SERVER at its IP and PORT 33891
   ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
   ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
       ; If there is no error loop an inputbox for data
       ;   to send to the SERVER.
    Else
        While 1
            $recv = TCPRecv($ConnectedSocket, 2048)
            $nMsg = GUIGetMsg()
            Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Send
                TCPSend($ConnectedSocket, GUICtrlRead($input))
            EndSwitch
            If $recv <> "" Then
                GUICtrlSetData($edit,GUICtrlRead($edit) & $recv & @CRLF)
            EndIf
            If @error Then ExitLoop
        WEnd
    EndIf
EndFunc  ;==>Example

1. Run server.

2. Run client 2 times.

3. Send msg from both clients.

4. Shutdown one client.

5. Run new client(new client are connected in socket of disconnected client)

6. Send msg from new client.

In client which was always on msg are display one time, on new client msg are display 2 times(or more)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Here you go.

Compile it as a console app then you can get the data on the console screen of the server.

You had a couple of issues, first you never set the connected socket to -1 when the client disconnected and second your method never reused a disconnected socket so after 25 connections you were stuffed.

This will recycle connections

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****

Global $MaxConnections = 25, $iCurrentSocket = 0, $oListenSocket, $oConnectedSocket[$MaxConnections]
TCPStartup()

$oListenSocket = TCPListen(@IPAddress1, 16802, $MaxConnections)
If $oListenSocket = -1 Then
        MsgBox(0, "Error", "Unable to start listening on port 16802")
        Exit
EndIf
    
;Initialise the socket array with -1's
ConsoleWrite("Initialise Server " &  $MaxConnections & " sockets available" & @crlf)
For $iCurrentSocket = 0 to $MaxConnections -1
        $oConnectedSocket[$iCurrentSocket] = -1
Next    
    
While 1
    For $iCurrentSocket = 0 to $MaxConnections -1
        If $oConnectedSocket[$iCurrentSocket] = -1 then 
            $oConnectedSocket[$iCurrentSocket] = TCPAccept($oListenSocket);Just cycle the sockets
            If $oConnectedSocket[$iCurrentSocket] <> -1 then ConsoleWrite("Socket[" & $iCurrentSocket & "] Connected"  & @crlf)
        EndIf
    
    Next


    For $i = 0 To $MaxConnections -1
        If $oConnectedSocket[$i] <> -1 Or $oConnectedSocket[$i] <> "" Then
            $sResv = TCPRecv($oConnectedSocket[$i], 512)
            If $sResv <> "" Then
            ConsoleWrite("Socket[" & $i& "]" & $sResv&@CRLF)
            For $c = 0 to $MaxConnections -1
                If $oConnectedSocket[$c] <> -1 then TCPSend($oConnectedSocket[$c], "Socket[" & $i & "]: " & $sResv)
            Next
        ElseIf @error Then
                TCPCloseSocket($oConnectedSocket[$i])
                If $oConnectedSocket[$i] <> -1 then ConsoleWrite("Socket[" & $i & "] Disconnected" & @crlf)
                $oConnectedSocket[$i] = -1;Set the$oConnectedSocket[$i] socket back to -1 so it becomes available again
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Edit; Left a hard coded loop count in oopps

Edit2: Only tries to copy the message to open sockets not all of them now

Edited by ChrisL

Share this post


Link to post
Share on other sites

Thanks :)

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