没有合适的资源?快使用搜索试试~ 我知道了~
WinSOCKET五种模型.pdf
需积分: 0 5 下载量 14 浏览量
2016-09-05
09:26:36
上传
评论
收藏 247KB PDF 举报
温馨提示
试读
24页
WinSOCKET五种模型.pdf
资源详情
资源评论
资源推荐
深圳 QQ 群:326023167
1
qzher.com 青哲科技
WinSocket 五种网络模型基础
深圳 QQ 群:326023167
// ---------------------------------------------------
//------------------- SELECT 模型-------------------
// ---------------------------------------------------
#include <winsock.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#define PORT 2345
#define MSGSIZE 1024
int g_nTotalConn = 0;
SOCKET
g_socketClients[100];
sockaddr_in
g_addrClients[100];
DWORD WINAPI
WorkerThread(
LPVOID
lpParameter);
#include <mutex>
std
::
mutex
mutex;
int main()
{
WSADATA
wsaData;
SOCKET
sockListen, sockClient;
SOCKADDR_IN
addrServer, addrClient;
int iaddrSize = sizeof(
SOCKADDR_IN
);
DWORD
dwThreadId;
WSAStartup
(0x0202, &wsaData);
sockListen =
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
addrServer.
sin_addr
.
S_un
.
S_addr
=
htonl
(
INADDR_ANY
);
addrServer.
sin_family
=
AF_INET
;
addrServer.
sin_port
=
htons
(PORT);
bind
(sockListen, (struct
sockaddr
*)&addrServer, sizeof(
SOCKADDR_IN
));
listen
(sockListen, 3);
CreateThread
(
NULL
, 0, WorkerThread,
NULL
, 0, &dwThreadId);
printf
("**************************************", PORT);
printf
("qzher 服务器启动成功, 监听端口号:%d", PORT);
printf
("**************************************", PORT);
深圳 QQ 群:326023167
2
int i = 0;
while (
TRUE
)
{
sockClient =
accept
(sockListen, (struct
sockaddr
*)&addrClient, &iaddrSize);
printf
("第%d 个客户端:%s:%d 已连接\n", i++,
inet_ntoa
(addrClient.
sin_addr
),
ntohs
(addrClient.
sin_port
));
g_socketClients[g_nTotalConn] = sockClient;
g_addrClients[g_nTotalConn] = addrClient;
g_nTotalConn++;
}
return 0;
}
DWORD WINAPI
WorkerThread(
LPVOID
lpParam)
{
int nRet = 0;
int nIndex = 0;
fd_set
fdRead;
struct
timeval
tv = { 1, 0 };
char chMsg[MSGSIZE] = { '\0' };
while (
TRUE
)
{
FD_ZERO
(&fdRead);
for (nIndex = 0; nIndex < g_nTotalConn; nIndex++)
{
FD_SET
(g_socketClients[nIndex], &fdRead);
}
nRet =
select
(0, &fdRead,
NULL
,
NULL
, &tv);
if (nRet == 0)
{
continue;
}
for (nIndex = 0; nIndex < g_nTotalConn; nIndex++)
{
if (
FD_ISSET
(g_socketClients[nIndex], &fdRead))
{
memset
(chMsg, 0, MSGSIZE);
nRet =
recv
(g_socketClients[nIndex], chMsg, MSGSIZE, 0);
if (nRet>0)
{
printf
("%s:%d 消息: %s \n",
inet_ntoa
(g_addrClients[nIndex].
sin_addr
),
ntohs
(g_addrClients[nIndex].
sin_port
), chMsg);
深圳 QQ 群:326023167
3
}
else
{
return 0;
}
}
}
}
return 0;
}
深圳 QQ 群:326023167
4
// ---------------------------------------------------
//-------------------WSAsyncSelect-------------------
// ---------------------------------------------------
#include <winsock.h>
#include <tchar.h>
#define PORT 2345
#define MSGSIZE 1024
#define WM_SOCKET
WM_USER
+100
#pragma comment(lib, "ws2_32.lib")
LRESULT CALLBACK
WndProc(
HWND
,
UINT
,
WPARAM
,
LPARAM
);
HDC
hDC =
NULL
;
int
WINAPI WinMain
(
HINSTANCE
hInstance,
HINSTANCE
hPrevInstance,
PSTR
szCmdLine, int
iCmdShow)
{
static
TCHAR
szAppName[] =
_T
("AsyncSelect Model");
HWND
hwnd;
MSG
msg;
WNDCLASS
wndclass;
wndclass.
style
=
CS_HREDRAW
|
CS_VREDRAW
;
wndclass.
lpfnWndProc
= WndProc;
wndclass.
cbClsExtra
= 0;
wndclass.
cbWndExtra
= 0;
wndclass.
hInstance
= hInstance;
wndclass.
hIcon
=
LoadIcon
(
NULL
,
IDI_APPLICATION
);
wndclass.
hCursor
=
LoadCursor
(
NULL
,
IDC_ARROW
);
wndclass.
hbrBackground
= (
HBRUSH
)
GetStockObject
(
WHITE_BRUSH
);
wndclass.
lpszMenuName
=
NULL
;
wndclass.
lpszClassName
= szAppName;
if (!
RegisterClass
(&wndclass))
{
MessageBox
(
NULL
,
TEXT
("This program requires Windows NT!"), szAppName,
MB_ICONERROR
);
return 0;
}
hwnd =
CreateWindow
(szAppName, // window class name
TEXT
("qzher.com--AsyncSelect"), // window caption
WS_OVERLAPPEDWINDOW
, // window style
CW_USEDEFAULT
, // initial x position
CW_USEDEFAULT
, // initial y position
CW_USEDEFAULT
, // initial x size
CW_USEDEFAULT
, // initial y size
NULL
, // parent window handle
深圳 QQ 群:326023167
5
NULL
, // window menu handle
hInstance, // program instance handle
NULL
); // creation parameters
ShowWindow
(hwnd, iCmdShow);
UpdateWindow
(hwnd);
hDC =
GetDC
(hwnd);
while (
GetMessage
(&msg,
NULL
, 0, 0))
{
TranslateMessage
(&msg);
DispatchMessage
(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK
WndProc(
HWND
hwnd,
UINT
message,
WPARAM
wParam,
LPARAM
lParam)
{
WSADATA
wsd;
static
SOCKET
sockListen;
SOCKET
sClient;
SOCKADDR_IN
addrLocal, addrClient;
int nRes, iAddrSize = sizeof(addrClient);
char chMsg[MSGSIZE];
static int yPos = 5;
switch (message)
{
case
WM_CREATE
:
WSAStartup
(0x0202, &wsd);
sockListen =
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
addrLocal.
sin_addr
.
S_un
.
S_addr
=
htonl
(
INADDR_ANY
);
addrLocal.
sin_family
=
AF_INET
;
addrLocal.
sin_port
=
htons
(PORT);
bind
(sockListen, (struct
sockaddr
*)&addrLocal, sizeof(addrLocal));
listen
(sockListen, 5);
WSAAsyncSelect
(sockListen, hwnd, WM_SOCKET,
FD_ACCEPT
);
return 0;
case
WM_DESTROY
:
closesocket
(sockListen);
WSACleanup
();
PostQuitMessage
(0);
剩余23页未读,继续阅读
青哲科技
- 粉丝: 14
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0