#include <Winsock2.h>
#include <Windows.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
HWND g_hwnd;
SOCKET g_sock ;
SOCKET sock[64];
int nTotial = 0;
int nErr = 0;
#define WM_SOCK (WM_USER + 100)
bool CreateWindows(HINSTANCE hInstance);
bool InitSock();
void Send();
LRESULT CALLBACK WinProc(HWND hwnd,
UINT uMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
if(CreateWindows(hInstance) != true)
{
return -1;
}
if(InitSock() != true)
{
return -1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
//把虚键消息翻译成字符消息(WM_CHAR),
//再把它放回到应用程序的消息队列中去
DispatchMessage(&msg);
//指示操作系统把这条消息发送到窗口
//过程WinProc进行处理
}
return 0;
}
bool CreateWindows(HINSTANCE hInstance)
{
const char ClassName[] = "SockClass";
const char WindowName[] = "SockWindow";
WNDCLASS WndClass;
ZeroMemory(&WndClass, sizeof(WndClass));
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.lpfnWndProc = (WNDPROC)WinProc;
WndClass.style = CS_HREDRAW | CS_VREDRAW;;
WndClass.hCursor = LoadCursor(NULL, IDC_CROSS);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.lpszMenuName = NULL;
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.lpszClassName = ClassName;
WndClass.hInstance = hInstance;
if(RegisterClass(&WndClass) == NULL)
{
return false;
}
g_hwnd = CreateWindow(ClassName, WindowName, WS_OVERLAPPEDWINDOW, 0,0, 200,200, NULL, NULL, hInstance,NULL);
if(g_hwnd ==0)
{
int i = GetLastError();
return false;
}
ShowWindow(g_hwnd, SW_SHOW);
UpdateWindow(g_hwnd);
return true;
}
LRESULT CALLBACK WinProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar, "你按下了%c键", (char)wParam);
MessageBox(hWnd, szChar, "WM_CHAR", 0);
break;
case WM_LBUTTONDOWN:
HDC hdc;
hdc = GetDC(hWnd);
TextOut(hdc, 0, 50, "坚持到底就是胜利", strlen("坚持到底就是胜利"));
Send();
ReleaseDC(hWnd, hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hWnd, &ps);
TextOut(hDC, 0, 0, "Hello, World!", strlen("Hello, World!"));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SOCK:
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
{
SOCKADDR_IN addr;
int nLen = sizeof(addr);
SOCKET socker = accept(g_sock, (SOCKADDR*)&addr, &nLen);
if(socker == SOCKET_ERROR)
{
return -3;
}
else
{
sock[nTotial] = socker;
nTotial++;
nErr = WSAAsyncSelect(socker, g_hwnd, WM_SOCK, FD_READ|FD_WRITE|FD_CLOSE);
if(SOCKET_ERROR == nErr) return -3;
else
{
MessageBox(g_hwnd, "FD_ACCEPT ", "FD_ACCEPT", MB_OK);
}
}
}
break;
case FD_READ:
{
MessageBox(g_hwnd, "FD_READ ", "FD_READ", MB_OK);
}
break;
case FD_WRITE:
{
MessageBox(g_hwnd, "FD_WRITE", "FD_WRITE", MB_OK);
Send();
}
break;
case FD_CLOSE:
{
MessageBox(g_hwnd, "close windows", "FD_CLOSE", MB_OK);
}
break;
}
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
bool InitSock()
{
int err;
WSADATA wsaData;
err = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (err != 0 )
{
return false;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return false;
}
g_sock = socket(AF_INET, SOCK_STREAM, 0);
if(g_sock == SOCKET_ERROR)
{
return false;
}
SOCKADDR_IN addr;
ZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(6000);
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
err = bind(g_sock, (SOCKADDR*)&addr, sizeof(addr));
if(err == SOCKET_ERROR)
{
return false;
}
err = listen(g_sock, 5);
if(err == SOCKET_ERROR)
{
return false;
}
sock[nTotial] = g_sock;
nTotial ++;
nErr = WSAAsyncSelect(g_sock, g_hwnd, WM_SOCK, FD_ACCEPT);
if(SOCKET_ERROR == nErr) return false;
return true;
}
void Send()
{
int nSend = 0;
//
for(int i = 0; i < nTotial; i++);
nSend = send(sock[1], "你好,我是服务器端", 20, 0);
if(nSend <= 0)
{
return;
}
else
{
MessageBox(g_hwnd, "send", "send", MB_OK);
}
}