客户端程序运行时总是有10047错误
我的客户端程序中“连接”按钮的响应程序段为 (运行时出现了10047号错误):
case IDC_CONNECT:
WSAStartup(0x0202, &wsadat);
s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
host= gethostbyname("YH");
CopyMemory(&server.sin_addr , host->h_addr_list[0],host->h_length );
connect(s, (struct sockaddr *)&server, sizeof(server));
//WSAGetLastError()
iError=WSAGetLastError();
wsprintf(&cError, (char *)&itoc, iError);
SetDlgItemText(hDlg, IDC_ERROR, &cError);
return false;
我的客户端程序的完整源码为 (呵,有点长了) :
#include "stdafx.h"
#include "resource.h"
#include "winuser.h"
#include <winbase.h>
#include "winsock2.h"
HINSTANCE hIns;
BOOL WINAPI MainDlg(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
hIns=hInstance;
DialogBox(hInstance,MAKEINTRESOURCE(IDD_CLIENT),NULL,(DLGPROC)MainDlg);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
BOOL WINAPI MainDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
WSADATA wsadat;
SOCKET s;
HOSTENT *host;
struct sockaddr_in server;
int port=5150, iError=0, addlen;
struct sockaddr clientaddr;
char cError, itoc[]="%d";
switch(message)
{
case WM_CLOSE:
EndDialog(hDlg,0);
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_CONNECT:
WSAStartup(0x0202, &wsadat);
s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
host= gethostbyname("SZ");
CopyMemory(&server.sin_addr , host->h_addr_list[0],host->h_length );
connect(s, (struct sockaddr *)&server, sizeof(server));
//WSAGetLastError()
iError=WSAGetLastError();
wsprintf(&cError, (char *)&itoc, iError);
SetDlgItemText(hDlg, IDC_ERROR, &cError);
return false;
case IDC_EXIT:
WSACleanup();
SendMessage(hDlg, WM_CLOSE, 0, 0);
}
}
return false;
}
//END