Jump to content

Unstable TCP socket connection server keeps disconnecting and connecting all the time


Recommended Posts

I am working on a code of  Client-Server Connection for a while (Like Teamviewer) , the problem is that the server keeps disconnecting and connecting all the time , In the client i wrote a code to notify me each time server is online , I am getting around 10 notifications each 1 minute from same server :

This is the server code :

TCPStartup()
Local $ConnectedSocket
Local $My_IP = "127.0.0.1"
Local $My_PORT = '5000'
Local $UserID = "MyID"


; Connect
Connect($My_IP, $My_PORT)

While 1

If TCPConnect($My_IP, $My_PORT) <> -1 Then
        $recv = TCPRecv($ConnectedSocket, 2048)
        $RecvSpl = StringSplit($recv, "|")


         Switch $RecvSpl[1]
            Case "order1"
          ; Do something
          MsgBox(64,"Success","You are now connected to the client")
          AddLogToClinet("My server is Online", $UserID, "Green")

         EndSwitch
Else
        $Connected = False
        Connect($IP, $My_PORT)
EndIf

    Sleep(100)
WEnd

; Connect To the Client
Func Connect($IP, $Port)
    Local $iConnectAttempts = 0
    TCPStartup()
    Do
        $ConnectedSocket = TCPConnect($IP, $Port)
        Sleep(1000)
    Until ($ConnectedSocket > 0 or $iConnectAttempts > 10)
    
            Return SetError(@error, 0, $ConnectedSocket)
 EndFunc   ;==>Connect

 ; Send To Client
Func SockSend($Cmd, $Text)
TCPSend($ConnectedSocket, $Cmd & $SockSpl & $Text & $PocketSpl, 4)
EndFunc   ;==>SockSend

; Add Log To Server
Func AddLogToClinet($Data, $UserID, $Color)
SockSend("AddLog", $Data & $SockSpl & $UserID & $SockSpl & $Color)
EndFunc   ;==>AddLogToClinet

At first the Connection function was : 

; Connect To the Client ( Old )
Func Connect($IP, $Port)
    TCPStartup()
    $ConnectedSocket = TCPConnect($IP, $Port)

    If @error Then
        Connect($IP, $My_PORT) ; <<<<<<<<<<<<<<< This was the causing recursion error
    Else
        $Connected = True
    EndIf
 EndFunc   ;==>Connect

It was working  good , except I had 'recursion level has been exceeded  error (old one)

WRisIQI.jpg

 I've tried  increasing the delay for current code by :

Opt("TCPTimeout", 5000)

However it did not help yet what do you suggest to me ?

 

Edited by DrAhmed
Link to comment
Share on other sites

Several times per second, you are opening a connection to the server, and not closing it.  If you are not communicating, or needing to keep the connection open, be sure to close it when done with it.  If you open too many connections, you will eventually be dropped.

Maybe try putting a TCPCloseSocket() statement after your TCPRecv() statement.

Link to comment
Share on other sites

11 hours ago, willichan said:

Several times per second, you are opening a connection to the server, and not closing it.  If you are not communicating, or needing to keep the connection open, be sure to close it when done with it.  If you open too many connections, you will eventually be dropped.

Maybe try putting a TCPCloseSocket() statement after your TCPRecv() statement.

But, like you said I need to keep the connection open all the time, to make the server receive orders any time when it's online .

Link to comment
Share on other sites

I was mainly suggesting that you not repeatedly create socket connections several times per second and keep them open.

Make a connection, do what you need to do (looks like all you are doing is checking to see if the connection succeeded), then disconnect.  If you are not actually transferring data across the connection, then you don't need to keep it open.  If you are transferring data, then suspend your loop since you do not need to be opening new connections.  The reason your script is reporting that you are dropping connectivity is because you are reaching the limit on open sockets.  You actually have several successfully open sockets, but since there are no more sockets available, TCPConnect() fails, and your script interprets that as a loss of connection to the server.  When your script re-issues the TCPStartup(), you are then killing all of those successful socket connections, and starting over.  Again, you have most likely not really lost connection to the server, but you are rather causing the connection to fail after falsely identifying the connection as lost.

Hopefully, that explanation is a little more helpful.  Good luck!

 

Link to comment
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
 Share

×
×
  • Create New...