当前位置: 技术问答>linux和unix
关于UDP的recvfrom收不到数据的问题,请高手帮忙
来源: 互联网 发布时间:2016-04-09
本文导语: 先把我遇到的问题部分的代码贴出来: bool CTestSession::getNATAddress(CAddressInfo aoNATServer) { int iReady; struct sockaddr_in loServAddress; fd_set SockFd; std::string lstrNATMsg("SHOWMENATE"); ...
先把我遇到的问题部分的代码贴出来:
bool CTestSession::getNATAddress(CAddressInfo aoNATServer)
{
int iReady;
struct sockaddr_in loServAddress;
fd_set SockFd;
std::string lstrNATMsg("SHOWMENATE");
struct timeval loWaitTime = {10, 500000};
int liBytes = 0;
char lcszMsgBuf[256] = {0};
CAddressInfo loRTPNATAddress;
if (!mbIsCreated)
return false;
bzero(&loServAddress, sizeof(loServAddress));
loServAddress.sin_family = AF_INET;
loServAddress.sin_addr.s_addr = inet_addr(aoNATServer.getIP().c_str());
loServAddress.sin_port = htons(aoNATServer.getPort());
int i = 0;
lstrNATMsg += "n";
while (!mbIsGotRTPNAT)
{
if (++i >= 100)
return false;
std::string lstrMsgType;
std::string lstrCmdBuf;
sendto(miRTPSocket, (const char *)lstrNATMsg.c_str(), lstrNATMsg.length(),
0, (const sockaddr *)&loServAddress, sizeof(loServAddress));
write_log(XFS_LOG_INFO, "send command", "send to server : %s.", lstrNATMsg.c_str());
memset(lcszMsgBuf, 0, 256);
FD_ZERO(&SockFd);
FD_SET(miRTPSocket, &SockFd);
iReady = select(miRTPSocket + 1, &SockFd, NULL, NULL, &loWaitTime);
if(iReady 0)
{
lstrCmdBuf.clear();
lstrMsgType.clear();
lstrCmdBuf += lcszMsgBuf;
std::stringstream lstrRTPStream(lstrCmdBuf);
lstrRTPStream >> lstrMsgType;
if (lstrMsgType == "SHOWMENATR")
{
loRTPNATAddress.decode(lstrRTPStream);
moNATLock.lock();
muiRTPPort = loRTPNATAddress.getPort();
moNATLock.unlock();
lstrRTPStream
bool CTestSession::getNATAddress(CAddressInfo aoNATServer)
{
int iReady;
struct sockaddr_in loServAddress;
fd_set SockFd;
std::string lstrNATMsg("SHOWMENATE");
struct timeval loWaitTime = {10, 500000};
int liBytes = 0;
char lcszMsgBuf[256] = {0};
CAddressInfo loRTPNATAddress;
if (!mbIsCreated)
return false;
bzero(&loServAddress, sizeof(loServAddress));
loServAddress.sin_family = AF_INET;
loServAddress.sin_addr.s_addr = inet_addr(aoNATServer.getIP().c_str());
loServAddress.sin_port = htons(aoNATServer.getPort());
int i = 0;
lstrNATMsg += "n";
while (!mbIsGotRTPNAT)
{
if (++i >= 100)
return false;
std::string lstrMsgType;
std::string lstrCmdBuf;
sendto(miRTPSocket, (const char *)lstrNATMsg.c_str(), lstrNATMsg.length(),
0, (const sockaddr *)&loServAddress, sizeof(loServAddress));
write_log(XFS_LOG_INFO, "send command", "send to server : %s.", lstrNATMsg.c_str());
memset(lcszMsgBuf, 0, 256);
FD_ZERO(&SockFd);
FD_SET(miRTPSocket, &SockFd);
iReady = select(miRTPSocket + 1, &SockFd, NULL, NULL, &loWaitTime);
if(iReady 0)
{
lstrCmdBuf.clear();
lstrMsgType.clear();
lstrCmdBuf += lcszMsgBuf;
std::stringstream lstrRTPStream(lstrCmdBuf);
lstrRTPStream >> lstrMsgType;
if (lstrMsgType == "SHOWMENATR")
{
loRTPNATAddress.decode(lstrRTPStream);
moNATLock.lock();
muiRTPPort = loRTPNATAddress.getPort();
moNATLock.unlock();
lstrRTPStream