/************************************************************************* > File Name : CUdpClient.cpp > Author : FengChao > EMail : smile.fengchao@gmail.com > Created Time : Tue 21 Jul 2020 02:02:08 PM CST ************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "CUdpClient.h" #include "CLog.h" CUdpClient::CUdpClient() { m_iClientSock = -1; m_uiPort = 0; memset(m_ucHost, 0x00, sizeof(m_ucHost)); memset(&m_stServerInfo, 0x00, sizeof(m_stServerInfo)); CUdpSocket(); } CUdpClient::CUdpClient(const char *pcHost, unsigned int uiPort) { m_iClientSock = -1; m_uiPort = uiPort; memset(m_ucHost, 0x00, sizeof(m_ucHost)); strcpy((char*)m_ucHost, pcHost); memset(&m_stServerInfo, 0x00, sizeof(m_stServerInfo)); CUdpSocket(); } CUdpClient::~CUdpClient() { if(0 < m_iClientSock) close(m_iClientSock); } int CUdpClient::CUdpGetSockaddr(const char * pcHost, unsigned int uiPort, SOCKADDR_IN_T *pstSockaddr) { SOCKADDR_IN_T stSockaddr; if((NULL == pcHost) || (0 == uiPort) || (NULL == pstSockaddr)) { SysLog(SLOG_ERR, "Para is error!"); return -1; } memset(&stSockaddr, 0x00, sizeof(stSockaddr)); stSockaddr.sin_family = AF_INET; stSockaddr.sin_port = htons(uiPort); stSockaddr.sin_addr.s_addr = inet_addr(pcHost); if(stSockaddr.sin_addr.s_addr == INADDR_NONE) { SysLog(SLOG_ERR, "Incorrect ip address!"); return -1; } memcpy(pstSockaddr, &stSockaddr, sizeof(SOCKADDR_IN_T)); return 0; } int CUdpClient::CUdpSocket() { m_iClientSock = socket(AF_INET, SOCK_DGRAM, 0); if(0 > m_iClientSock) { SysLog(SLOG_ERR, "Udp client socket failed!"); return 0; } CUdpSetRecvTimeout(3); CUdpSetSendTimeout(0); return 0; } int CUdpClient::CUdpSetSendTimeout(unsigned int uiSeconds) { TIMEVAL_T stTimeout; stTimeout.tv_sec = uiSeconds; stTimeout.tv_usec = 0; return setsockopt(m_iClientSock, SOL_SOCKET, SO_SNDTIMEO, &stTimeout, sizeof(stTimeout)); } int CUdpClient::CUdpSetRecvTimeout(unsigned int uiSeconds) { TIMEVAL_T stTimeout; stTimeout.tv_sec = uiSeconds; stTimeout.tv_usec = 0; return setsockopt(m_iClientSock, SOL_SOCKET, SO_RCVTIMEO, &stTimeout, sizeof(stTimeout)); } int CUdpClient::CUdpSetBroadcastOpt() { int iOptval = 1; return setsockopt(m_iClientSock, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &iOptval, sizeof(int)); } int CUdpClient::CUdpRecvData(void *pcBuff, unsigned int uiBuffLen) { return CUdpRecvData(pcBuff, uiBuffLen, (const char *)m_ucHost, m_uiPort); } int CUdpClient::CUdpRecvData(void *pcBuff, unsigned int uiBuffLen, const char *pcHost, unsigned int uiPort) { SOCKLEN_T stSockLen = 0; SOCKADDR_IN_T stSockaddr; if((NULL == pcBuff) || (NULL == pcHost) || (0 == uiPort)) { SysLog(SLOG_ERR, "Para is error!"); return -1; } memset(&stSockaddr, 0x00, sizeof(stSockaddr)); if(0 != CUdpGetSockaddr(pcHost, uiPort, &stSockaddr)) { SysLog(SLOG_ERR, "Get sockaddr failed!"); return -1; } stSockLen = sizeof(SOCKADDR_IN_T); return recvfrom(m_iClientSock, pcBuff, uiBuffLen, 0, (SOCKADDR_T *)&stSockaddr, (SOCKLEN_T *)&stSockLen); } int CUdpClient::CUdpSendData(const void *pcData, unsigned int uiDataLen) { return CUdpSendData(pcData, uiDataLen, (const char*)m_ucHost, m_uiPort); } int CUdpClient::CUdpSendData(const void *pcData, unsigned int uiDataLen, const char *pcHost, unsigned int uiPort) { SOCKADDR_IN_T stSockaddr; if((NULL == pcData) || (NULL == pcHost) || (0 == uiPort)) { SysLog(SLOG_ERR, "Para is error!"); return -1; } memset(&stSockaddr, 0x00, sizeof(stSockaddr)); if(0 != CUdpGetSockaddr(pcHost, uiPort, &stSockaddr)) { SysLog(SLOG_ERR, "Get sockaddr failed!"); return -1; } return sendto(m_iClientSock, pcData, uiDataLen, 0, (SOCKADDR_T *)&stSockaddr, (SOCKLEN_T)sizeof(stSockaddr)); }