#include "stdafx.h"
#include "GzUDP.h"
#include "Robot.h"
#include "RobotDoc.h"
#include "RobotView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BUFFER_SIZE (1024 * 8)
char g_buffer[BUFFER_SIZE];
/////////////////////////////////////////////////////////////////////////////
CGzUdp::CGzUdp()
{
//初始化
/* 初始化为无效的套接字 */
m_sockReceive = INVALID_SOCKET;
m_sockSend = INVALID_SOCKET;
m_ipTarget = 0x7f000001; // 127.0.0.1
/* 本地及目标端口号 */
m_wTargetPort = 10091;
m_wLocalPort = 10082;
/* 接收线程初始化关闭 */
m_bIsReceiving = FALSE;
m_hRcvThread = NULL;
}
/* 析构函数,关闭资源 */
CGzUdp::~CGzUdp()
{
DeleteSender();
DeleteReceiver();
StopReceiving();
}
/* 初始化,加载socket库 */
BOOL CGzUdp::Init(void *p)
{
WSADATA wsaData;
WORD version = MAKEWORD(2, 0); /* 印象中是2.2的版本 */
int ret = WSAStartup(version, &wsaData);
if(ret != 0)
{
TRACE("Initilize Error!\n"); //初始化失败
// AfxMessageBox("Failed in initial socket");
return FALSE;
}
m_pParentWnd=p;
return TRUE;
}
/* 设置目标的IP */
void CGzUdp::SetTargetIP(DWORD inIP)
{
m_ipTarget = inIP;
}
/* 获取目标的IP,整数形式 */
DWORD CGzUdp::GetTargetIP(void)
{
return m_ipTarget;
}
/* 设置目标的IP地址,实质是设置成员变量m_ipTarget */
void CGzUdp::SetTargetIP(const char * inIP)
{
m_ipTarget = ntohl(inet_addr(inIP));
}
/* 获得目标的IP地址 */
void CGzUdp::GetTargetIP(char * outIP)
{
if (outIP)
{
struct in_addr in;
in.S_un.S_addr = htonl(m_ipTarget); /* 先将IP地址转化为in_addr的类型,从这点上看Windows下的socket与linux下是基本一致的*/
char * pStr = inet_ntoa(in);
strcpy(outIP, pStr);
}
}
/* 设置目标端口 */
void CGzUdp::SetTargetPort(WORD inPort)
{
m_wTargetPort = inPort;
}
WORD CGzUdp::GetTargetPort(void)
{
return m_wTargetPort;
}
void CGzUdp::SetLocalPort(WORD inPort)
{
m_wLocalPort = inPort;
}
WORD CGzUdp::GetLocalPort(void)
{
return m_wLocalPort;
}
//网络发送初始化
BOOL CGzUdp::CreateSender(void)
{
DeleteSender();
/* 创建UDP套接字 */
m_sockSend = socket(AF_INET, SOCK_DGRAM, 0);//UDP协议
/* 如果创建成功则返回 */
if (m_sockSend != INVALID_SOCKET)
{
return TRUE;
}
/* 如果创建不成功,则会执行下面的语句,setsockopt设置socket的参数,具体功能再细研究*/
int nZero=0;
setsockopt(m_sockSend,SOL_SOCKET,SO_SNDBUF,(char *)&nZero,sizeof(nZero));
//发送时限
int nNetTimeout=100;//100毫秒
setsockopt(m_sockSend,SOL_SOCKET,SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int));
return FALSE;
}
//关闭网络
/* 即关闭套接字 */
void CGzUdp::DeleteSender(void)
{
if (m_sockSend != INVALID_SOCKET)
{
closesocket(m_sockSend);
m_sockSend = INVALID_SOCKET; /* 好习惯,要学习---不能变量随意为空 */
}
}
//创建接收网络
BOOL CGzUdp::CreateReceiver(void)
{
DeleteReceiver();
/* 创建UDP套接字 */
m_sockReceive = socket(AF_INET, SOCK_DGRAM, 0);//UDP协议
/* 如果创建成功 */
if (m_sockReceive != INVALID_SOCKET)
{
BOOL flag = TRUE; /* 标记为创建成功 */
int ret = setsockopt(m_sockReceive, SOL_SOCKET, SO_REUSEADDR,
(char *) &flag, sizeof(flag));
/* 如果设置失败 */
if (ret == SOCKET_ERROR)
{
DeleteReceiver();
return FALSE;
}
/* 套接字绑定地址 */
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(m_wLocalPort);
ret = bind(m_sockReceive, (struct sockaddr*) &addr, sizeof(addr));
/* 如果设置失败 */
if (ret == SOCKET_ERROR)
{
DeleteReceiver();
return FALSE;
}
int nZero=0;
setsockopt(m_sockReceive,SOL_SOCKET,SO_RCVBUF,(char *)&nZero,sizeof(int));
//接收超时时限
int nNetTimeout=80;//80毫秒
setsockopt(m_sockReceive,SOL_SOCKET,SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int));
return TRUE;
}
return FALSE;
}
//关闭网络接收
void CGzUdp::DeleteReceiver(void)
{
if (m_sockReceive != INVALID_SOCKET)
{
closesocket(m_sockReceive);
m_sockReceive = INVALID_SOCKET;
}
}
//数据发送
int CGzUdp::Send(const char * inBuffer, long inLength)
{
SOCKADDR_IN addr;
memset((char *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(m_ipTarget);
addr.sin_port = htons(m_wTargetPort);
/* UDP发送数据的方式 */
int val = sendto(m_sockSend, inBuffer, inLength, 0,
(sockaddr *) &addr, sizeof(addr));
return val;
}
/* 与其上的功能一致 */
int CGzUdp::Send(unsigned char * inBuffer, long inLength)
{
SOCKADDR_IN addr;
memset((char *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(m_ipTarget);
addr.sin_port = htons(m_wTargetPort);
int val = sendto(m_sockSend, (char*)inBuffer, inLength, 0,
(sockaddr *) &addr, sizeof(addr));
return val;
}
/* 对任意IP发送数据 */
BOOL CGzUdp::SendTo(const char * inBuffer, long inLength, DWORD inIP, WORD inPort)
{
/* UDP套接字 */
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock != INVALID_SOCKET)
{
SOCKADDR_IN addr;
memset((char *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(inIP);
addr.sin_port = htons(inPort);
int val = sendto(sock, inBuffer, inLength, 0,
(sockaddr *) &addr, sizeof(addr));
if (val == SOCKET_ERROR)
{
}
closesocket(sock);
return (val != SOCKET_ERROR);
}
return FALSE;
}
//开启接收线程
BOOL CGzUdp::StartReceiving(void)
{
// Create socket if necessary
if (m_sockReceive == INVALID_SOCKET)
{
CreateReceiver();
}
if (m_sockReceive != INVALID_SOCKET)
{
if (m_bIsReceiving)
{
return TRUE;
}
/* 创建线程 */
DWORD threadID = 0;
m_hRcvThread = CreateThread(NULL, 0, ReceivingThrd,
this, 0, &threadID);
return (m_hRcvThread != NULL);
}
return FALSE;
}
void CGzUdp::StopReceiving(void)
{
if (m_bIsReceiving)
{
DeleteReceiver();
// Make sure the receiving thread has been terminated
if (m_hRcvThread != NULL)
{
if(WaitForSingleObject(m_hRcvThread, 100) != WAIT_OBJECT_0)
{
::TerminateThread(m_hRcvThread,1);
}
m_hRcvThread = NULL;
}
}
}
/* 参数是this指针 */
DWORD WINAPI CGzUdp::ReceivingThrd(void * pParam)
{
CGzUdp * pController = (CGzUdp*) pParam;
pController->ReceivingLoop();
return 0;
}
//接收循环
void CGzUdp::ReceivingLoop(void)
{
struct sockaddr_in addr_cli;
int addr_cli_len = sizeof(addr_cli);
int pc;
long bytes = 0;
m_bIsReceiving = TRUE;
while (m_bIsReceiving)
{
int addr_cli_len = sizeof(addr_cli);
bytes = recvfrom(m_sockReceive, (char *)g_buffer, BUFFER_SIZE, 0,(LPSOCKADDR) &addr_cli, (int *) &addr_cli_len);
// if(addr_cli.sin_addr.S_un.S_un_b.s_b4 == 13) /* 来自黑色机器人*/
// {
// pc=13;
// }
// else
// {
// pc=14; //ocu发送过来的
// }
/* pc记录IP地址的第四位 */
pc = addr_cli.sin_addr.S_un.S_un_b.s_b4;
/* 如果没有接到网络命令,则结束,否则交给View类来处理 */
if (bytes == SOCKET_ERROR || bytes == 0)
{
}
else
{
((CRobotView*)m_pParentWnd)->NetCommandAnalyse(g_buffer, bytes, pc);
}
}
}
- 1
- 2
- 3
前往页