-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetSocket.cpp
More file actions
133 lines (120 loc) · 3.29 KB
/
NetSocket.cpp
File metadata and controls
133 lines (120 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// NetSocket.cpp: implementation of the CNetSocket class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NetSocket.h"
#include "SID_2FY.h"
#include "dMessageBox.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CMySID_2FYApp theApp;
CUDPNetWork CNetSocket::m_udpNetWork;
CNetSocket::CNetSocket()
{
m_socket=INVALID_SOCKET;
}
CNetSocket::~CNetSocket()
{
CloseSocket();
}
//创建一个端口号,或服务号为nPort的通过实体(socket)
bool CNetSocket::CreateSocket(UINT nPort)
{
m_nPort = nPort;
m_socket=::socket(AF_INET,SOCK_DGRAM,0);
CString info;
if(INVALID_SOCKET==m_socket)
{
info.LoadString( IDS_SOCKET_ERROR1 );
CdMessageBox box( theApp.m_pMainWnd, info );
box.MsgBoxEx();
return false;
}
m_wEvent=::WSACreateEvent();
if(::WSAEventSelect(m_socket,m_wEvent,FD_READ))
{
info.LoadString( IDS_SOCKETEVENT_ERROR );
CdMessageBox box(theApp.m_pMainWnd, info, _T(""), MB_OK);
box.MsgBoxEx();
return FALSE;
}
m_sendSockAddr.sin_family=AF_INET;
m_sendSockAddr.sin_port=htons(nPort);
m_sendSockAddr.sin_addr.S_un.S_addr = CNetSocket::m_udpNetWork.GetIPAddr();
m_bindSockAddr.sin_port=htons(nPort);
m_bindSockAddr.sin_family=AF_INET;
m_bindSockAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
if(bind(m_socket,(sockaddr *)&m_bindSockAddr,sizeof(m_bindSockAddr)))
{
return false;
}
return true;
}
/*******************************************************
函数名:SendData
描述:用网口连接设备时,发送数据函数
参数:lpBuf[in]:发送的内容;nLength[in]:发送数据长度
返回值:成功返回true,失败返回false
*******************************************************/
bool CNetSocket::SendData(void * lpBuf,int nLength)
{
if(!m_udpNetWork.IsEnable()) return false;
m_sendSockAddr.sin_addr.S_un.S_addr = m_udpNetWork.GetIPAddr();
if(SOCKET_ERROR==sendto(m_socket,(char *)lpBuf,
nLength,0,(sockaddr *)&m_sendSockAddr,sizeof(m_sendSockAddr)))
{
//请数据失败!
return false;
}
return true;
}
/***************************************************************************
返回值:
0: 接收超时
-1: 接收失败
>0: 接收到的数据字节数
***************************************************************************/
int CNetSocket::RecvData(void * lpBuf,int nLength,int nTimeOut)
{
sockaddr_in recvSock;
int sockLen = sizeof(recvSock);
int nRecvData;
DWORD index=::WSAWaitForMultipleEvents(1,&m_wEvent,TRUE,nTimeOut,FALSE); //进入临界区
//TRACE(_T("recv wait on %d port\n"),m_nPort);
if(index==WSA_WAIT_FAILED||index==WSA_WAIT_TIMEOUT)
return 0;
WSANETWORKEVENTS wnetEvent;
::WSAEnumNetworkEvents(m_socket,m_wEvent,&wnetEvent);
if(wnetEvent.lNetworkEvents & FD_READ)
{
if(wnetEvent.iErrorCode[FD_READ_BIT]!=0)
{
return -1;
}
nRecvData=recvfrom(m_socket,(char *)lpBuf,nLength,
0,(sockaddr *)&recvSock,&sockLen);
if(SOCKET_ERROR==nRecvData)
{
//接收失败!
return -1;
}
//TRACE(_T("recv seccuss on %d port\n"),m_nPort);
return nRecvData;
}
return -1;
}
void CNetSocket::CloseSocket()
{
if(m_socket != INVALID_SOCKET)
{
closesocket(m_socket);
WSACloseEvent(m_wEvent);
m_socket=INVALID_SOCKET;
}
}