老師讓我們做一個UDP的接收程式,并且發給了我們原始碼。在main函式中遇到了問題,不太會寫。
這個是udpclient.h檔案
#ifndef UDPCLIENT_H_
#define UDPCLIENT_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <pthread.h>
#include <string>
#include <cstring>
#include "windef.h"
#define UDP_BUFF_LEN 4096
typedef void (*UdpEventCall)(void* hostPtr, sockaddr_in address, BYTE* buff, size_t len);
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 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:
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_ */
這個是udpclient.cpp
#include "UdpClient.h"
UdpClient::UdpClient()
{
m_thdListen = 0;
hostPtr_ = NULL;
callPtr_ = NULL;
m_sockfd = -1;
memset(&m_localAddr, 0, sizeof(sockaddr_in));
m_localAddrlen = 0;
memset(&m_remoteAddr, 0, sizeof(sockaddr_in));
memset(&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)) < 0)
{
perror("udp create");
return false;
}
//setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
int reuse = 1;
setsockopt(m_sockfd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST, &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 *) &m_localAddr, sizeof(m_localAddr)) < 0)
{
perror("Udp bind");
return false;
}
if (pthread_create(&m_thdListen, NULL, StaitcRecv, this) != 0)
{
perror("Udp pthread_create");
return false;
}
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(), message.size(), 0, (sockaddr *) &addrto, sizeof(addrto));
}
bool UdpClient::Send(std::string message)
{
return Send(m_remoteAddr, (BYTE*) message.c_str(), message.size());
}
bool UdpClient::Send(BYTE* buff, size_t len)
{
return Send(m_remoteAddr, buff, len);
}
bool UdpClient::Send(sockaddr_in addr, std::string message)
{
return Send(addr, (BYTE*) message.c_str(), message.size());
}
bool UdpClient::Send(sockaddr_in addr, BYTE* buff, size_t len)
{
int ret = sendto(m_sockfd, (void*) buff, len, 0, (sockaddr *) &addr, sizeof(addr));
if (ret >= 0)
return true;
else
{
perror("udp send");
return false;
}
}
void* UdpClient::StaitcRecv(void* p_pHost)
{
UdpClient* p = (UdpClient*) p_pHost;
p->Recv();
return NULL;
}
void UdpClient::Recv()
{
sockaddr_in addr;
socklen_t addrlen;
while (!m_bQuitRecv)
{
memset(&(m_buff), 0, sizeof(m_buff));
// recvfrom 如果接受不到,關閉防火墻
m_len = recvfrom(m_sockfd, m_buff, sizeof(m_buff), 0, (struct sockaddr*) &addr, &addrlen);
if (m_len > 0)
{
if (callPtr_ != NULL)
callPtr_(hostPtr_, addr, m_buff, m_len);
}
else if (m_len == 0)
continue;
else
perror("Udp recvfrom");
}
}
下面是main.cpp 這個是我自己寫的。不太會寫
#include <iostream>
#include <string>
#include "libgen.h"
#include "UdpClient.h"
#include "windef.h"
using namespace std;
void UdpDataReceived(void* p_pHost, sockaddr_in p_address, BYTE* p_pBuff, size_t p_pLen);
int main()
{
UdpDataReceived();
}
void UdpDataReceived(void* p_pHost, sockaddr_in p_address, BYTE* p_pBuff, size_t p_pLen)
{
UdpClient* g_pUdpClient = new UdpClient();
g_pUdpClient->SetEventCaller(NULL, UdpDataReceived);
g_pUdpClient->StartListen(8888);
std::string str = std::string((char*) p_pBuff, p_pLen);
std::cout << "udp receive: " << str << std::endl;
g_pUdpClient->Send(p_address, str);
}
int main里面不知道怎么寫。
uj5u.com熱心網友回復:
你這個原始碼和我老師給我的幾乎一模一樣,就是UDP改成TCP了,我也不會。。大哥你搞出來了嗎轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130067.html
標籤:C++ 語言
上一篇:c++初級
下一篇:c++
