0

I am using TF6310 TCP/IP to start a TCP server, await for connection (it is guaranteed that there is only one client connecting), and when a connection comes I accept it. Once accepted i receive/send data.

Problem I am having is that i do not recognize if the client disconnects, and I am not able to figure out how to detect that in order to close the socket.

In particuar all I want is to stay in step 3 as long as the client is connected, if he disconnects I want to go to step 100..

        1:  // Open Listener-Socket

        fbSocketListen(
            sSrvNetId:='', 
            sLocalHost:=sLocalHost, 
            nLocalPort:=nLocalPort, 
            bExecute:=TRUE, 
            tTimeout:=T#5S, 
            bBusy=> , 
            bError=>bError, 
            nErrId=>nErrorID,
            hListener=>hListener);

        IF hListener.handle <> 0
        THEN
            iState:=2;
        ELSIF bError
        THEN
            iState:=99;
        END_IF

    2:  // Accept Client connection

        fbSocketAccept(
            sSrvNetId:='', 
            hListener:=fbSocketListen.hListener, 
            bExecute:= bAcceptExecute:= NOT bAcceptExecute, 
            tTimeout:=T#5S, 
            bAccepted=> , 
            bBusy=> , 
            bError=>bError, 
            nErrId=>nErrorID,
            hSocket=>hSocket);
        //  bExecute:= bAcceptExecute:= NOT bAcceptExecute, 

        IF bError
        THEN
            iState:=99;
        ELSIF hSocket.handle <> 0
        THEN
            iState:=3;
            bConnected:=TRUE;
        END_IF

    3:  // client connected, send and receive data
        
        fbSocketListen();
        fbSocketAccept();
        fbSocketReceive(
            sSrvNetId:='', 
            hSocket:=fbSocketAccept.hSocket, 
            cbLen:=SIZEOF(stDataRx), 
            pDest:=ADR(stDataRx), 
            bExecute:= bReceiveExecute:= NOT bReceiveExecute,       // Check for new client telegrams every second cycle
            tTimeout:=T#14S, 
            bBusy=> , 
            bError=>bError, 
            nErrId=>nErrorID,
            nRecBytes=> );
            

        IF fbSocketReceive.nRecBytes <> 0       // Switch to send-state when data are received
        THEN
            bNewDataReceived:=TRUE;
        END_IF
                            
        IF fbSocketReceive.bError OR(NOT bEnable)       // Close connection when error is occuring or trigger is set
        THEN
            iState:=100;
        END_IF
        
        IF bNewDataReceived THEN
            (* got data, write it to the outputs..  *)
            stScannerRx := stDataRx;
            bNewDataReceived := FALSE;
        END_IF
        
        
        impNewPal(clk:=bNewPal);
        
        IF impNewPal.Q THEN
            MEMSET(ADR(stScannerRx),16#00,SIZEOF(stScannerRx));
        END_IF
        
        
        (* if a new crate is ready, send the packet *)
    
        IF bNewPal THEN
            
            stDataTx.iPalId := iPalId;
            stDataTx.iStackId := iStackId;
            stDataTx.bReady := 1;
            stDataTx.iCrateCnt := iCrateCnt;
                        
            
            fbSocketSend(           // Send Data to client
                sSrvNetId:='', 
                hSocket:=hSocket, 
                cbLen:=SIZEOF(stDataTx),
                pSrc:=ADR(stDataTx),
                bExecute:=TRUE, 
                tTimeout:=T#3S, 
                bBusy=> , 
                bError=>bError, 
                nErrId=>nErrorID);
                
        ELSE                
            fbSocketSend(bExecute:=FALSE);
        END_IF
        
        
        IF fbSocketSend.bError
        THEN
            iState:=100;            // Close connection in case of error 
        END_IF                  
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97
  • Infosys has **Timeout value setup** note that says it won't be detected. I think you need to create a separate timer for closing the socket if no data in x seconds. https://infosys.beckhoff.com/content/1033/tf6310_tc3_tcpip/84150667.html?id=2641180414945961591 – Quirzo Jan 19 '22 at 06:33
  • @Quirzo thank you, not sure I understand your first sentence. as for the separate timer, that will not work in my case as the client will send data at random times. could be a few seconds, could be 1 hour, as it depends on external factors. same on my end, I have to send data at random intervals, thats the reason why the connection needs to stay open. if the connection drops, I need to go back to the accept connection in order to be able to accept a new connection and get the new handle. – sharkyenergy Jan 19 '22 at 06:46
  • I understand your problem. Could the connection be opened every time data is sent? Or what if the client would send some kind of heartbeat signal? – Quirzo Jan 19 '22 at 08:19
  • nope, because the client must connect to the server, but the server does not know if the client is still connected.. – sharkyenergy Jan 19 '22 at 10:33
  • Sorry I'm not following. Why can't the client send some kind of "I'm alive" signal to the server? Or why can't the client disconnect after sending data and then connect again when it will send again. – Quirzo Jan 19 '22 at 12:59
  • @Quirzo because both send data, the server and the client. so the client does not know when i will send something over, and thus it needs to stay connected. the client cant be modified as it is made by another company. – sharkyenergy Jan 19 '22 at 14:27

0 Answers0