Home > Back-end >  C a small white
C a small white

Time:10-18

Teacher, let's do a UDP receiving procedures, and sent us the source code, the problems encountered in the main function, not too will write,
This is the udpclient. H file
 # # ifndef UDPCLIENT_H_ 
# define UDPCLIENT_H_

# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Unistd. H>

# include & lt; Sys/types. H>
# include & lt; Sys/socket. H>
# include & lt; Netinet/in. H>
# include & lt; ARPA/inet. H>
# include & lt; Errno. H>
# include & lt; Pthread. H>
# include & lt; string>
# include & lt; Cstring>

# include "windef. H"

# define UDP_BUFF_LEN 4096

UdpEventCall typedef void (*) (void * hostPtr, sockaddr_in address, BYTE * buff, size_t len);

The class UdpClient
{
Public:
UdpClient ();
Virtual ~ UdpClient ();
Void SetEventCaller (void * hostPtr, UdpEventCall callPtr);
Void SetRemoteAddress (STD: : string p_strRemoteIp, uint16_t p_nRemotePort);
Bool StartListen (uint16_t p_nLocalPort);
Void the Close ();
Void SendBroadcast (uint16_t p_nRemotePort, STD: : string message);
Bool Send (STD: : string message);
Bool Send (BYTE * buff, size_t len);
Bool Send (sockaddr_in addr, STD: : string message);
Bool Send (sockaddr_in addr, BYTE * buff, size_t len);
Private:
The static void * StaitcRecv (void * p_phost);
Void Recv ();
Pthread_t m_thdListen;
Int m_sockfd;
Sockaddr_in m_localAddr;
Socklen_t m_localAddrlen;
Sockaddr_in m_remoteAddr;
Uint16_t m_nRemotePort;
Void * hostPtr_;
UdpEventCall callPtr_;
BYTE m_buff [UDP_BUFF_LEN];
Size_t m_len;
Bool m_bQuitRecv;
};

# endif UDPCLIENT_H_/* */




This is udpclient CPP
 # include "udpclient. H 
"
UdpClient: : UdpClient ()
{
M_thdListen=0;
HostPtr_=NULL;
CallPtr_=NULL;
M_sockfd=1;
Memset (& amp; M_localAddr, 0, sizeof (sockaddr_in));
M_localAddrlen=0;
Memset (& amp; M_remoteAddr, 0, sizeof (sockaddr_in));

Memset (& amp; M_buff, 0, sizeof (m_buff));
M_len=0;
M_nRemotePort=1024;
M_bQuitRecv=false;
}

UdpClient: : ~ UdpClient ()
{
//TODO Auto - generated destructor stub
}

Void UdpClient: : SetEventCaller (void * hostPtr, UdpEventCall callPtr)
{
HostPtr_=hostPtr;
CallPtr_=callPtr;
}

Void UdpClient: : SetRemoteAddress (STD: : string p_strRemoteIp, uint16_t p_nRemotePort)
{
M_remoteAddr. Sin_family=AF_INET;
M_remoteAddr. Sin_addr. S_addr=inet_addr (p_strRemoteIp c_str ());
M_remoteAddr. Sin_port=htons (p_nRemotePort);
}

Bool UdpClient: : StartListen (uint16_t p_nLocalPort)
{
If ((m_sockfd=socket (AF_INET, SOCK_DGRAM, 0)) & lt; 0)
{
Perror (" udp create ");
return false;
}
//the setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, & amp; Reuse, sizeof (reuse));
Int reuse=1;
The setsockopt (m_sockfd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST, & amp; Reuse, sizeof (reuse));

M_localAddr. Sin_family=AF_INET;
M_localAddr. Sin_addr. S_addr=htonl (INADDR_ANY);//inet_addr (" 127.0.0.1 ")
M_localAddr. Sin_port=htons (p_nLocalPort);
M_localAddrlen=sizeof (struct sockaddr_in);

If (bind (m_sockfd, (struct sockaddr *) & amp; M_localAddr, sizeof (m_localAddr)) & lt; 0)
{
Perror (" Udp bind ");
return false;
}
If (pthread_create (& amp; M_thdListen, NULL, StaitcRecv, this)!=0)
{
Perror (" Udp pthread_create ");
return false;
}
The else
{
Pthread_detach (m_thdListen);
return true;
}
}

Void UdpClient: : Close ()
{
M_bQuitRecv=true;
If (m_sockfd!=1)
{
Shutdown (m_sockfd SHUT_RDWR);
Close (m_sockfd);
M_sockfd=1;
}
If (m_thdListen!=0)
{
Pthread_join (m_thdListen, NULL);
M_thdListen=0;
}
}

Void UdpClient: : SendBroadcast (uint16_t p_nRemotePort, STD: : string message)
{
Sockaddr_in addrto;
Addrto. Sin_family=AF_INET;
Addrto. Sin_addr. S_addr=htonl (INADDR_BROADCAST);
Addrto. Sin_port=htons (p_nRemotePort);
Sendto (m_sockfd, (void *) message. C_str (), the message the size (), 0, (sockaddr *) & amp; Addrto, sizeof (addrto));
}

Bool UdpClient: : Send (STD: : string message)
{
Return the Send (m_remoteAddr, (BYTE *) message. C_str (), the message. The size ());
}

Bool UdpClient: : Send (BYTE * buff, size_t len)
{
Return the Send (m_remoteAddr, buff, len);
}

Bool UdpClient: : Send (sockaddr_in addr, STD: : string message)
{
Return the Send (addr, (BYTE *) message c_str (), the message. The size ());
}

Bool UdpClient: : Send (sockaddr_in addr, BYTE * buff, size_t len)
{
Int ret=sendto (m_sockfd (void *) buff, len, 0, (sockaddr *) & amp; Addr, sizeof (addr));
If (ret & gt;=0)
return true;
The else
{
Perror (" udp send ");
return false;
}
}

Void * UdpClient: : StaitcRecv (void * p_pHost)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
Next:c
  • Related