Hi All,
I'm getting inconsistent results from the RTCS getpeername() function when I use it to determine the IP port/adr of a remote device that requested a connection to my listen socket. I have an MQX task dedicated to supporting a blocking call to the accept() function (listening for a connection request). When accept() returns with a connection request I create a new socket for this connection and service it in it's own MQX task. When I use getpeername() to learn about who connected to me about half the time the IP address and port data are all 0's. Subsequent calls to getpeername() will some times return valid info and other times return 0's.
The example code for using this function is pretty simple but perhaps there's more to it? I'm running MQX ver 3.7 on a MPC5125 PowerPC Tower System Eval board. Thanks in advance for any suggestions. ~Tim
void NETCOM_SockListenTask(uint_32 lListenSock) { uint_32 lSocketHandle, lStatus, lTemp; uint_16 iNamelen; _task_id tiNewTaskID; sockaddr_in remote_sin;//Blocking call to wait for a connection//------------------------------------------------------------------ while(TRUE) { //Blocking call to listen for connection request //-------------------------------------------------------------- lSocketHandle = accept(lListenSock, NULL, NULL); if(lSocketHandle == RTCS_SOCKET_ERROR) { //Handle Error.... continue; } //Create a MQX task to service reception of data in this socket //-------------------------------------------------------------- tiNewTaskID = _task_create(0, SERVSOCKET_RX_TASK, iSockIndex); if(tiNewTaskID == MQX_NULL_TASK_ID) { //Handle Error.... continue; } //Learn about who we just accepted a connection from //-------------------------------------------------------------- lStatus = getpeername(lSocketHandle, &remote_sin, &iNamelen); if(lStatus == RTCS_OK) { lTemp = remote_sin.sin_addr.s_addr; //<- These values are lTemp = remote_sin.sin_port //<- often 0x00000000 //<- Why??? } return; }
Solved! Go to Solution.
Oops, problem solved. You need to set the iNamelen var to the size of the remote_sin object before calling getpeername() like this:
iNamelen = sizeof(remote_sin);lStatus = getpeername(lSocketHandle, &remote_sin, &iNamelen);
Now it works properly!
~Tim
Oops, problem solved. You need to set the iNamelen var to the size of the remote_sin object before calling getpeername() like this:
iNamelen = sizeof(remote_sin);lStatus = getpeername(lSocketHandle, &remote_sin, &iNamelen);
Now it works properly!
~Tim