📄 基于winsock api的vc网络编程实战.htm
字号:
BOOL CALLBACK SendString(<br>
HWND hwndDlg, // handle to dialog box<br>
UINT uMsg, // message<br>
WPARAM wParam, // first message parameter<br>
LPARAM lParam // second message parameter<br>
);<br>
<br>
BOOL CALLBACK SendFile(<br>
HWND hwndDlg, // handle to dialog box<br>
UINT uMsg, // message<br>
WPARAM wParam, // first message parameter<br>
LPARAM lParam // second message parameter<br>
);<br>
<br>
BOOL CALLBACK ConnectToIP(<br>
HWND hwndDlg, // handle to dialog box<br>
UINT uMsg, // message<br>
WPARAM wParam, // first message parameter<br>
LPARAM lParam // second message parameter<br>
);<br>
<br>
//////////////////////////////////////////////////////mian.cpp<br>
#include "main.h"<br>
#define MAX_LOADSTRING 100<br>
HINSTANCE hInst; // current instance<br>
HWND hWnd, hWndDialog; // handle of the dialog<br>
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text<br>
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text<br>
CNetworking Networking;<br>
CConnection* Connection = NULL;<br>
int connections[8]; // holds our connection slots...<br>
// Foward declarations of functions included in this code module:<br>
ATOM MyRegisterClass(HINSTANCE hInstance);<br>
BOOL InitInstance(HINSTANCE, int);<br>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br>
void ReceiveCallback (DWORD ptr)<br>
{<br>
char buff[1024] = "";<br>
CConnection* c = reinterpret_cast <CConnection*> (ptr);<br>
c->Receive (buff, 1024);<br>
MessageBox (hWnd, buff, "Information", MB_OK | MB_ICONINFORMATION);<br>
}<br>
<br>
void CloseCallback (DWORD ptr)<br>
{<br>
MessageBox (hWnd, "The connection was closed.", "Information", MB_OK | MB_ICONINFORMATION);<br>
}<br>
<br>
void AcceptCallback (DWORD ptr)<br>
{<br>
char cip[15];<br>
unsigned int cp = 0;<br>
CNetworking* net = reinterpret_cast <CNetworking*> (ptr);<br>
if (Connection && Connection->IsConnected ())<br>
{<br>
CConnection* c = net->GetAccepted ();<br>
while (c)<br>
{<br>
char nocon[] = "The host can not accept your connection at this time.";<br>
c->Send (nocon, sizeof (nocon));<br>
c->Disconnect ();<br>
delete c;<br>
c = net->GetAccepted ();<br>
};<br>
}<br>
else<br>
{<br>
if (Connection)<br>
delete Connection;<br>
Connection = net->GetAccepted ();<br>
Connection->PeerInfo (&cip[0], 15, &cp);<br>
Connection->SetReceiveFunc (ReceiveCallback);<br>
Connection->SetCloseFunc (CloseCallback);<br>
char ci[128];<br>
sprintf (ci, "A connection was accepted.\n\nClient Information:\n %s:%i\n\n", cip, cp);<br>
MessageBox (hWnd, ci, "Client Info", MB_OK | MB_ICONINFORMATION);<br>
}<br>
}<br>
<br>
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,<br>
LPSTR lpCmdLine,int nCmdShow)<br>
{<br>
// TODO: Place code here.<br>
MSG msg;<br>
// Initialize global strings<br>
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);<br>
LoadString(hInstance, IDS_NETWORKING, szWindowClass, MAX_LOADSTRING);<br>
MyRegisterClass(hInstance);<br>
// Perform application initialization:<br>
if (!InitInstance (hInstance, nCmdShow))<br>
{<br>
return FALSE;<br>
}<br>
// Main message loop:<br>
while (GetMessage(&msg, NULL, 0, 0))<br>
{<br>
TranslateMessage(&msg);<br>
DispatchMessage(&msg);<br>
}<br>
return msg.wParam;<br>
}<br>
<br>
ATOM MyRegisterClass(HINSTANCE hInstance)<br>
{<br>
WNDCLASSEX wcex;<br>
wcex.cbSize = sizeof(WNDCLASSEX);<br>
wcex.style = CS_HREDRAW | CS_VREDRAW;<br>
wcex.lpfnWndProc = (WNDPROC)WndProc;<br>
wcex.cbClsExtra = 0;<br>
wcex.cbWndExtra = 0;<br>
wcex.hInstance = hInstance;<br>
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_NETWORKING);<br>
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);<br>
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);<br>
wcex.lpszMenuName = (LPCSTR)IDM_NETWORKING;<br>
wcex.lpszClassName = szWindowClass;<br>
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);<br>
return RegisterClassEx(&wcex);<br>
}<br>
<br>
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)<br>
{<br>
hInst = hInstance; // Store instance handle in our global variable<br>
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,<br>
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);<br>
if (!hWnd)<br>
{<br>
return FALSE;<br>
}<br>
ShowWindow(hWnd, nCmdShow);<br>
UpdateWindow(hWnd);<br>
return TRUE;<br>
}<br>
<br>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br>
{<br>
int wmId, wmEvent;<br>
switch (message)<br>
{<br>
case WM_CREATE:<br>
{<br>
hWndDialog = NULL;<br>
SetWindowPos (hWnd, HWND_TOP, 0, 0, 200, 44, SWP_NOOWNERZORDER | SWP_NOMOVE);<br>
char jakobIP[225] = "";<br>
Networking.GetLocalIPs (jakobIP, 225);<br>
MessageBox(NULL, jakobIP, "Info", NULL);<br>
Networking.SetAcceptFunc (AcceptCallback);<br>
break;<br>
}<br>
case WM_COMMAND:<br>
wmId = LOWORD(wParam);<br>
wmEvent = HIWORD(wParam);<br>
// Parse the menu selections:<br>
switch (wmId)<br>
{<br>
case IDM_EXIT:<br>
DestroyWindow (hWnd);<br>
break;<br>
case IDM_LISTEN:<br>
if (Connection)<br>
{<br>
if (MessageBox (hWnd, "You are still connected to a computer.\nPress Yes if you want<br>
to disconnect and No to abort.\n\nSince this is just a demonstration of what you<br>
can\ndo with Networking I kept things simple.", NULL, MB_YESNO | MB_ICONSTOP) == IDNO)<br>
break;<br>
delete Connection;<br>
Connection = NULL;<br>
}<br>
if (!Networking.Listen (CONNECT_PORT))<br>
{<br>
char cPort[1024] = "";<br>
sprintf (cPort, "Unable to listen at port %i", CONNECT_PORT);<br>
MessageBox (hWnd, cPort, NULL, MB_OK | MB_ICONSTOP);<br>
}<br>
else<br>
{<br>
char cPort[1024] = "";<br>
sprintf (cPort, "Now listening at port %i!\nPress 'Ok' to accept<br>
incoming connections.", CONNECT_PORT);<br>
MessageBox (hWnd, cPort, "Listening", MB_OK | MB_ICONINFORMATION);<br>
}<br>
break;<br>
case IDM_CANCELLISTEN:<br>
if (Networking.IsListening ())<br>
Networking.StopListen ();<br>
else<br>
MessageBox (hWnd, "Unable to cancel listen-process!\nMake sure you are listening.",<br>
NULL, MB_OK | MB_ICONINFORMATION);<br>
break;<br>
case IDM_CONNECT:<br>
{<br>
if (Connection)<br>
{<br>
Connection->Disconnect ();<br>
}<br>
DialogBox (hInst, "CONNECTIP", hWnd, ConnectToIP);<br>
break;<br>
}<br>
case IDM_SENDMSG:<br>
if (!Connection)<br>
MessageBox(hWnd, "Please connect before sending data.", NULL,<br>
MB_OK | MB_ICONINFORMATION);<br>
else<br>
DialogBox(hInst, "SENDMSG", hWnd, SendString);<br>
break;<br>
case IDM_DISCONNECT:<br>
if (Connection)<br>
Connection->Disconnect ();<br>
else<br>
MessageBox (hWnd, "Unable to close connection!\nMake sure you are connected.",<br>
NULL, MB_OK | MB_ICONINFORMATION);<br>
break;<br>
case IDM_LISTLAN:<br>
{<br>
char lanlist[1024] = "";<br>
Networking.GetNeighborhood (lanlist, 1024);<br>
MessageBox (hWnd, lanlist, "Information", MB_OK | MB_ICONINFORMATION);<br>
}<br>
default:<br>
return DefWindowProc(hWnd, message, wParam, lParam);<br>
}<br>
break;<br>
case WM_DESTROY:<br>
PostQuitMessage(0);<br>
break;<br>
default:<br>
return DefWindowProc (hWnd, message, wParam, lParam);<br>
}<br>
return 0;<br>
}<br>
<br>
四、小结<br>
<br>
本实例用它可以发送、接收、连接、断开和获取对方端口各种不同的信息。该类的主要优点是完全将连接对象化,不需要窗口就可以处理网络消息,通过定义回调函数或事件即可,甚至连通知消息都可以不用。如果读者朋友正在开发网络应用程序,那么该类也许会对你有所帮助。</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -