winsock问题 ???
//服务端和客户端一直连不上 客户端返回的错误是10060#include<windows.h>
#define WM_SOCKET WM_USER+0
#define PORT 5150
#define MSGSIZE 1024
LRESULT CALLBACK wndproc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR iCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HELLO");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
WORD wVersionRequested;
WSADATA wsaData;
int err;
static SOCKET sockSrv;
SOCKADDR_IN addrSrv;
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("窗口注册失败!!!"), TEXT("警告"), MB_OK);
return(0);
}
hwnd = CreateWindow
(szAppName,
TEXT("服务器"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return(0);
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return(0);
}
sockSrv = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == sockSrv)
{
MessageBox(NULL, TEXT("socket失败!!!"), TEXT("警告"), MB_OK);
closesocket(sockSrv);
WSACleanup();
return TRUE;
}
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
if (SOCKET_ERROR == bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)))
{
MessageBox(NULL, TEXT("socket失败!!!"), TEXT("警告"), MB_OK);
closesocket(sockSrv);
WSACleanup();
return TRUE;
}
if (listen(sockSrv, 5) == SOCKET_ERROR)
{
MessageBox(NULL, TEXT("监听失败!!!"), TEXT("警告"), MB_OK);
closesocket(sockSrv);
WSACleanup();
return TRUE;
}
if (SOCKET_ERROR == WSAAsyncSelect(sockSrv, hwnd, WM_SOCKET,
FD_CONNECT | FD_READ | FD_ACCEPT))
{
MessageBox(NULL, TEXT("消息发送失败!!!"), TEXT("警告"), MB_OK);
closesocket(sockSrv);
WSACleanup();
return TRUE;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
closesocket(sockSrv);
WSACleanup();
return(msg.wParam);
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
SOCKET sockConn, sockSrv;
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
switch (message)
{
case WM_CREATE:
return 0;
case WM_SOCKET:
sockSrv = wParam;
sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
MessageBox(NULL, TEXT("!!!!!!!!!!!!"), TEXT("警告"), MB_OK);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
#include<windows.h>
LRESULT CALLBACK wndproc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR iCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HELLO");
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("窗口注册失败!!!"), TEXT("警告"), MB_OK);
return(0);
}
hwnd = CreateWindow
(szAppName,
TEXT("客户"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(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)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
static SOCKET sockClient;
SOCKADDR_IN addrSrv;
int i;
TCHAR szBuffer;
switch (message)
{
case WM_CREATE:
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return(0);
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return(0);
}
sockClient = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sockClient)
{
MessageBox(NULL, TEXT("套接字创建失败!!!"), TEXT("警告"), MB_OK);
}
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
if (0!= connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)))
{
i=WSAGetLastError();
wsprintf(szBuffer,TEXT("%d"),i);
MessageBox(NULL, szBuffer, TEXT("警告"), MB_OK);
}
if (SOCKET_ERROR == send(sockClient, "chuangkouchengxu", strlen("chuangkouchengxu") + 1, 0))
{
MessageBox(NULL, TEXT("消息发送失败!!!"), TEXT("警告"), MB_OK);
}
return(0);
case WM_DESTROY:
closesocket(sockClient);
WSACleanup();
PostQuitMessage(0);
return(0);
}
return(DefWindowProc(hwnd, message, wParam, lParam));
} 第一个是服务端程序 第二个是客户端程序 是不是因为用了WSAAsyncSelect()把他变成了非阻塞的所以出什么问题了第一次写winsock程序 不是很懂但真的是急用!! 顶顶顶~ 1
页:
[1]