This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/TM4C1294NCPDT: Socket reopen bind failed

Part Number: TM4C1294NCPDT


Tool/software: TI-RTOS

Hi

My application is create two socket server.

First socket is use to data exchange.

Second socket is use to reopen two socket when connect, this socket will raise reopen flag, and directly close.

I use the tcp Echo example to modify.

Below is flow of shutdown two socket, tcpworker, clientfd.

when the flow is executed, will create new tcpHandler, the same with example.

        if(SocResFlag == SocketRestart)
        {
        	SocResFlag = SocketNonRestart;
        	UARTprintf("Execute restart procedure\n");
        	fdOpenSession( TaskSelf() );

        	if(fdShare((SOCKET)clientfd) == -1)
        	{
        		UARTprintf("fdShare clientfd failed\n");
        	}
        	if(fdShare((SOCKET)Socket_0) == -1)
        	{
        		UARTprintf("fdShare Socket_0 failed\n");
        	}
        	
        	if(fdShare((SOCKET)Socket_1) == -1)
        	{
        		UARTprintf("fdShare Socket_1 failed\n");
        	}
        	
        	UARTprintf("Close fd socket_clientfd\n");
            fdClose( (SOCKET)clientfd );
            fdClose( (SOCKET)Socket_0);
            fdClose( (SOCKET)Socket_1);
            UARTprintf("TaskDestroy WorkerHandle\n");
            TaskDestroy(WorkerHandle);
            UARTprintf("TaskDestroy SocketHandle\n");
            TaskDestroy(SocketHandle);
            Socket_Open_Flag = 0;
            fdCloseSession( TaskSelf() );

        }

Question is when reopen socket two socket server will bind failed.

If my flow is wrong, please tell me how to do.

Please help me to solve this.

Thank you very much.

Allen

CCS version : 6

TI-RTOS version : 2.16.1.14

NDK version : 2.25.00.09

 

  • Allen,

    I need a better understanding of your program to provide assistance.

    Are you saying that when a client makes a new connection to the server, the server will create two new sockets: one for communication with the client, and a second additional socket. Is this correct? What is the second socket used for? Who will connect to the second socket?

    Thanks,
    ~Ramsey

  • Hi Ramsey

    Thanks for your reply.

    The flow is below:

    1. When MCU started, it will create two socket server. First for data exchange, and a second socket for reopen the two socket.

        (Two socket port are different)

    2.When other device(client) connected to first socket.

    3.If any unexpected problem let the port to occur problem, client can connect the second socket port to trigger reopen procedure, and the second socket will shutdown by self.

    4.When client trigger reopen procedure, MCU will reopen first socket port, and second port.

    My problem is occur in step 4, when reopen socket server, it will bind fail.

    I'm beginner of NDK and ethernet.

    Please help to solve.

    Thank you very much.

    Allen

  • Allen,

    From your description, it sounds like the server is listening on two ports, lets say port 80 and port 81. Clients will connect to port 80 for data exchange. If an error occurs, a client connects to port 81 to indicate to the server that the original connection should be closed. The server automatically closes the second connection.

    Did I understand your description correctly?

    Unfortunately, this design has a problem. The server should never initiate the connection close operation, it should wait until the client initiates the close operation.

    When the client detects that there is some error (in your Step 3 above), all it needs to do is close the socket. The act of closing the socket will send a TCP packet to the server indicating that the client has closed its socket.

    On the server, when this TCP packet is received, the tcpWorker thread will unblock and return from recv() with -1. You can check errno to see the actual error condition. Only now, is it okay for the server to close its socket and terminate the worker thread.

    Back on the client, it can establish a new connection to the server. When this new connection request reaches the server, the tcpHandler task will return from accept() and create a new tcpWorker task to manage the new connection.

    The server does not need to call bind() again. In the tcpecho example, you can see that the tcpHandler task creates a socket, then calls bind() and listen() only one time for this socket. The call to accept() is done in a loop. This call will return each time a client makes a new connection. The return value from accept() is a new socket, created by the stack, which is used by the tcpWorker task for communicating with the client. It is not the same socket created by the server for listening.

    ~Ramsey

  • Hi Ramsey
    Sorry for late.
    And thanks for your reply.
    Yes, it is my description.
    Further explain.
    If port 80 in listening , I want to reopen the socket port 80.
    My application is when client couldn't connect server, through connected port 81 to let MCU reopen socket port 80.
    Could you tell me socket reopen flow detail.
    Please to help me.
    Thank you very much.
  • Hi Ramsey
    Thanks for your reply.
    I solved my problem.
    Use setsockopt function and set SO_REUSEADDR, it will not bind failed.

    Allen
  • Allen,

    That is good news. Thanks for letting us know.

    I'm curious, did you figure out why the client was unable to connect? I'm wondering if the connection failure will keep happening.

    Thanks,
    ~Ramsey

  • Hi Ramsey

    Because we test in other network stack, it will occur port unable to connect occasionally.
    And we do not want to restart our machine, so we must have reopen socket function in remote.
    Or we only allowed one client to connect, if the client closed not correct, and not send finish package.
    The socket server will not closed.
    In this case, we also need reopen function.
  • Allen,

    Okay, understood. Thanks for letting me know.

    ~Ramsey