正在学习socket编程
编写console控制台socket程序的时候,一点问题没有,
但是改成win32 application却遇到许多莫名其妙的错误#include<windows.h>
#include <process.h>
#include <Winsock2.h>
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
VOID pro(PVOID para);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
char* hello="hello";
HINSTANCE hInst;
HWND hWndMain;
int APIENTRY WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
if (!InitApplication(hInstance))
return (FALSE);
if(!InitInstance(hInstance,nCmdShow))
return (FALSE);
while(GetMessage(&msg,
NULL,
0,
0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wce;
wce.style=0;
wce.lpfnWndProc=(WNDPROC)MainWndProc;
wce.hCursor=LoadCursor(NULL,IDC_ARROW);
wce.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wce.cbClsExtra=0;
wce.cbWndExtra=0;
wce.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wce.hInstance=hInstance;
wce.lpszClassName="SimpWClass";
wce.lpszMenuName=NULL;
return(RegisterClass(&wce));
}
BOOL InitInstance(HINSTANCE hInstance,
int nCmdShow)
{
hInst=hInstance;
hWndMain= CreateWindow("SimpWClass",
"window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(!hWndMain)
return(FALSE);
ShowWindow(hWndMain,nCmdShow);
UpdateWindow(hWndMain);
return(TRUE);
}
LRESULT CALLBACK MainWndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
_beginthread(pro,0,NULL);
break;
case WM_LBUTTONDOWN:
if(lParam&MK_SHIFT)
MessageBox(hWnd,"","",MB_OK);
break;
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
TextOut(hdc,20,10,hello,lstrlen(hello));
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd,message,wParam,lParam));
}
return(0);
} VOID pro(PVOID para)
{
char buff[20];
WSADATA wsa;
SOCKET slisten,saccept;
int ilen;
int isend;
HANDLE hmem;
struct sockaddr_in ser,cli;
HDC hh;
hh=GetDC(hWndMain);
WSAStartup(MAKEWORD(2,2),&wsa);
slisten=socket(AF_INET,SOCK_STREAM,0);
ser.sin_family=AF_INET;
ser.sin_port=htons(5000);
ser.sin_addr.s_addr =htonl(INADDR_ANY);
bind(slisten,(LPSOCKADDR)&ser,sizeof(ser)==SOCKET_ERROR);
listen(slisten,5);
while (1)
{
saccept=accept(slisten,(struct sockaddr*)&cli,&ilen);
TextOut(hh,0,0,
buff,
wsprintf(buff,"error!"));
closesocket(saccept);
} ReleaseDC(hWndMain,hh);
}
编写console控制台socket程序的时候,一点问题没有,
但是改成win32 application却遇到许多莫名其妙的错误#include<windows.h>
#include <process.h>
#include <Winsock2.h>
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
VOID pro(PVOID para);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
char* hello="hello";
HINSTANCE hInst;
HWND hWndMain;
int APIENTRY WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
if (!InitApplication(hInstance))
return (FALSE);
if(!InitInstance(hInstance,nCmdShow))
return (FALSE);
while(GetMessage(&msg,
NULL,
0,
0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wce;
wce.style=0;
wce.lpfnWndProc=(WNDPROC)MainWndProc;
wce.hCursor=LoadCursor(NULL,IDC_ARROW);
wce.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wce.cbClsExtra=0;
wce.cbWndExtra=0;
wce.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wce.hInstance=hInstance;
wce.lpszClassName="SimpWClass";
wce.lpszMenuName=NULL;
return(RegisterClass(&wce));
}
BOOL InitInstance(HINSTANCE hInstance,
int nCmdShow)
{
hInst=hInstance;
hWndMain= CreateWindow("SimpWClass",
"window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(!hWndMain)
return(FALSE);
ShowWindow(hWndMain,nCmdShow);
UpdateWindow(hWndMain);
return(TRUE);
}
LRESULT CALLBACK MainWndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
_beginthread(pro,0,NULL);
break;
case WM_LBUTTONDOWN:
if(lParam&MK_SHIFT)
MessageBox(hWnd,"","",MB_OK);
break;
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
TextOut(hdc,20,10,hello,lstrlen(hello));
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd,message,wParam,lParam));
}
return(0);
} VOID pro(PVOID para)
{
char buff[20];
WSADATA wsa;
SOCKET slisten,saccept;
int ilen;
int isend;
HANDLE hmem;
struct sockaddr_in ser,cli;
HDC hh;
hh=GetDC(hWndMain);
WSAStartup(MAKEWORD(2,2),&wsa);
slisten=socket(AF_INET,SOCK_STREAM,0);
ser.sin_family=AF_INET;
ser.sin_port=htons(5000);
ser.sin_addr.s_addr =htonl(INADDR_ANY);
bind(slisten,(LPSOCKADDR)&ser,sizeof(ser)==SOCKET_ERROR);
listen(slisten,5);
while (1)
{
saccept=accept(slisten,(struct sockaddr*)&cli,&ilen);
TextOut(hh,0,0,
buff,
wsprintf(buff,"error!"));
closesocket(saccept);
} ReleaseDC(hWndMain,hh);
}
#include <Winsock2.h>
#include <windows.h>
#include <process.h>