我的客户端程序中“连接”按钮的响应程序段为 (运行时出现了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

解决方案 »

  1.   

    An address incompatible with the requested protocol was used. All sockets are created with an associated address family and a generic protocol type. This error is returned if an incorrect protocol is requested when creating a socket, or in an address of the wrong family is uses when sending data.
      

  2.   

    在case IDC_CONNECT:中
    server.sin_addr=inet_addr(host->h_addr_list[0]);
    server.sin_port()..//端口,自己设置
    server.sin_family=AF_INET;
      

  3.   

    错误应该是没有给server指定协议了
    SOCKADDR_IN server;
    ZeroMemory(&server,sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(iPort)