我的客户端程序中“连接”按钮的响应程序段为 (运行时出现了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
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
解决方案 »
- 我想在自己的类或者其他类里面调用某个消息怎么弄~~
- VC++ MFC的问题。Dib.h
- 安装 vs2008 中文版时出现 未在系统中检测到兼容的
- 我用InstallShield for Visual C++ 6.0 做的VC++6.0的应用程序的安装程序
- 孙鑫VC视频(完整共20课)换孙鑫java视频(完整)
- 大家知道vb中的split函数吧,在vc中有没有类似的函数?
- VC中月历控件的使用求解~~
- 关于SMTP的身份认证的命令
- [走进IE编程]大家的兴趣才是我继续写的动力
- 为什么纹理底图颜色变暗了?
- 关于在CScrollView中设置坐标方向的问题
- 怎么用vc判断出某access文件已经损坏?
server.sin_addr=inet_addr(host->h_addr_list[0]);
server.sin_port()..//端口,自己设置
server.sin_family=AF_INET;
SOCKADDR_IN server;
ZeroMemory(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(iPort)