user3169825
user3169825

Reputation: 11

how to resolve this ip address error?

There is a Master (INCA tool) in my project and contains an A2l file (it as the information of variables and address in the client). The Master will trigger the client via TCP layer (the code is below). Master trigger an client by asking a value for an variable at a particular address. The below code as some API provided in my project and explained what it does.

After loading a file in the Master (INCA), it will ask for the value of a variable and call the below API to send and receive the frame on a specific ip address and port number. But I am getting a ERROR as: No working base assigned to device. ERROR: Transport Layer Failure, CEtasEthSocket::connect: connect event check failed (10061 WSAGetLastError())

This API is like a client.

// pBytes : data to be transmitted
// numBytes: number of bytes at pBytes
// the above two data will be take care automatically and later I will SEND via the specified port 

void XcpApp_IpTransmit(uint16 port, Xcp_StatePtr8 pBytes, uint16 numBytes) // this function must transmit the specified byte on IP connection identified by port
{
        WSADATA wsa;
        SOCKET s;
        uint8 bytes_recieved;
        struct sockaddr_in server;  // creating a socket address structure: structure contains ip address and port number
         uint16 numTxBytes;
         uint8* pChunkData;
         uint16 chunkLen;
         port = 18000;  
         numTxBytes = 1;
            uint8 recv_data[100];


        printf("Initializing Winsock\n");
        if(WSAStartup(MAKEWORD(2,2), &wsa)!=0)
        {
            printf("Failed Error Code: %d", WSAGetLastError());
            return 1;
        }
        printf("Initialised\n");


        //CREATING a SOCKET

        if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
            printf("Could not Create Socket\n");
            //return 0;
        }
        printf("Socket Created\n");




        //Binding between the socket and ip address
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;     
        server.sin_port = htons(port); 


            //Connect to a remote server
        if(connect(s, (struct sockaddr*)&server, sizeof(server))<0)
        {
        puts("Connect Error\n");
        //return 1;
        }

        puts("Connected\n");







            while (1)
            { 

            //initally its waiting for the data to be received on the port s and stored in recv_data buffer of size 100
                        bytes_recieved = recv(s, recv_data, 100 ,0 );
                recv_data[bytes_recieved] = '\0';                // reading till the end of the buffer

                pChunkData = &recv_data;
                chunkLen = 1 ;

                //This is the given API, When ever the data is receives a frame on the IP port. I should call this function
                XcpIp_RxCallback (chunkLen, *pChunkData, port);   // original API is : void XcpIp_RxCallback (uint16 chunkLen, uint8* pChunkData, uint16 port); 

             // chunklen : number of bytes at pchunkdata
            // pChunkData : received frame
            //  port : port number

                  printf("received data from Master %d\n", *pChunkData);           


                //sending data from client to the Master

                send(s, pBytes, numBytes, 0);  // explained in the begining

                if (strcmp(pBytes, "q") != 0 && strcmp(pBytes, "Q") != 0) 
                send(s, pBytes, numBytes, 0); 

                else
                {
                    send(s, pBytes, numBytes, 0);
                     XcpIp_TxCallback (port, numTxBytes);
                         closesocket(s);
                WSACleanup();
                break;

            }
              }
            }
        XcpIp_OnTcpCxnClosed(port); // this iS AN API given to close the TCP connection.
        }             

Upvotes: 0

Views: 184

Answers (1)

alk
alk

Reputation: 70971

If you read the 100 bytes requested as maximum, this line recv_data[bytes_recieved] = '\0'; will fail miserably. It will provoke undefined behaviuos as writing of of recv_datas bounds.

To fix this changes this line:

bytes_recieved = recv(s, recv_data, 100 , 0 );

to be

bytes_recieved = recv(s, recv_data, sizeof(recv_data)-1, 0);

Upvotes: 1

Related Questions