客户端程序运行时总是有10047错误

lovemoreh 2003-04-18 09:27:04
我的客户端程序中“连接”按钮的响应程序段为 (运行时出现了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
...全文
256 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ksyou 2003-04-18
  • 打赏
  • 举报
回复
使用的地址与被请求的协议不兼容
cmouse 2003-04-18
  • 打赏
  • 举报
回复
书上说明:使用的地址与被请求的协议不兼容。
zzh5335 2003-04-18
  • 打赏
  • 举报
回复
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.

lovemoreh 2003-04-18
  • 打赏
  • 举报
回复
是本人疏怱忘记指定服务器进程的端口和地址族。
非常感谢各位仁兄帮忙圆满解决此问题,希望以后能继续得到你们的帮助!
kaitty 2003-04-18
  • 打赏
  • 举报
回复
错误应该是没有给server指定协议了
SOCKADDR_IN server;
ZeroMemory(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(iPort)
rhb 2003-04-18
  • 打赏
  • 举报
回复
在case IDC_CONNECT:中
server.sin_addr=inet_addr(host->h_addr_list[0]);
server.sin_port()..//端口,自己设置
server.sin_family=AF_INET;
lovemoreh 2003-04-18
  • 打赏
  • 举报
回复
可是我的程序中,地址族和协议族没有不兼容的地方啊!

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧