MFC中socket创建失败

jmf010 2008-09-16 09:18:20
创建一个基于对话框的MFC应用程序
添加头文件#include<afxsock.h>;
在APP中的InitInstance()函数中添加代码:
if(!AfxSocketInit())
{
AfxMessageBox(_T("套接字库加载失败"));
}
在CDlg中的OnInitDialog()添加代码:
m_socket = socket(AF_INET,SOCK_DGRAM,0);
if(m_socket == INVALID_SOCKET )
{
MessageBox(_T("套接字创建失败"));
return FALSE;
}
为什么在运行的时候总是说“套接字创建失败”
...全文
566 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
taitanyt 2008-09-23
  • 打赏
  • 举报
回复
AfxSocketInit()不是仅mfc的,调用了后就可用api socket和csocket,不需要再调用WSAStartup
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 sanshao27 的回复:]
OnInitDialog()中的代码比InitInstance()中AfxSocketInit更早,调整下顺序

不要误认子弟好不好啊,没有initiainstance,后面的对话框消息怎么发送到哪里去啊
Wenxy1 2008-09-20
  • 打赏
  • 举报
回复
1. AfxSocketInit()是MFC的库函数。接下来你调用了windows SDK 的API socket();。我怀疑是这个原因出问题。
2. 改正方法,在APP中的InitInstance()函数中添加代码:
unsigned short uVersionRequested;
WSADATA wsaData;

uVersionRequested = MAKEWORD(2, 2);

if(0 != WSAStartup(uVersionRequested, &wsaData))
{
return WSAGetLastError();
}
并注释掉AfxSocketInit相关的代码。

3. 我建议大家用SDK 的 socket API来写网络程序。

socoola 2008-09-19
  • 打赏
  • 举报
回复
没有调用WSAStartup。
The WSAStartup function must be the first Windows Sockets function called by an application or DLL。
lzh9955 2008-09-19
  • 打赏
  • 举报
回复
在CDlg中的OnInitDialog()添加代码:
m_socket = socket(AF_INET,SOCK_DGRAM,0);
if(m_socket == INVALID_SOCKET )
{
MessageBox(_T("套接字创建失败"));
return FALSE;
}
为什么在运行的时候总是说“套接字创建失败”


/////////////////
在对话框中初始化这个干什么?
是否与它创建的位置有关?
fantiyu 2008-09-16
  • 打赏
  • 举报
回复
CDlg中的OnInitDialog()中的代码比InitInstance()中AfxSocketInit更早,调整下顺序
taitanyt 2008-09-16
  • 打赏
  • 举报
回复
socket() err代码意思如下:

WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
WSAENETDOWN The network subsystem or the associated service provider has failed.
WSAEAFNOSUPPORT The specified address family is not supported.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEMFILE No more socket descriptors are available.
WSAENOBUFS No buffer space is available. The socket cannot be created.
WSAEPROTONOSUPPORT The specified protocol is not supported.
WSAEPROTOTYPE The specified protocol is the wrong type for this socket.
WSAESOCKTNOSUPPORT The specified socket type is not supported in this address family.
www 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 jmf010 的帖子:]
创建一个基于对话框的MFC应用程序
添加头文件#include <afxsock.h>;
在APP中的InitInstance()函数中添加代码:
if(!AfxSocketInit())
{
AfxMessageBox(_T("套接字库加载失败"));
}
在CDlg中的OnInitDialog()添加代码:
m_socket = socket(AF_INET,SOCK_DGRAM,0);
if(m_socket == INVALID_SOCKET )
{
MessageBox(_T("套接字创建失败"));
return FALSE;
}
为什么在运行的时候总是说“套接字创建失败”

[/Quote]


if(!AfxSocketInit()) <--------------------------------------必需放在DoModal之前。
{
AfxMessageBox(_T("套接字库加载失败"));
}
jmf010 2008-09-16
  • 打赏
  • 举报
回复
我加了啊还是不行
用getlasterror()返回error:操作成功完成
jmf010 2008-09-16
  • 打赏
  • 举报
回复
用GetLastError()返回提示:操作成功完成。
什么意思啊
jmf010 2008-09-16
  • 打赏
  • 举报
回复
用GetLastError()返回提示:操作成功完成。
什么意思啊
walkbywind 2008-09-16
  • 打赏
  • 举报
回复
用GetLastError返回错误码看看

[Quote=引用 5 楼 cnzdgs 的回复:]
AfxSocketInit要加在对话框创建之前。
[/Quote]
为什么?解释一下好吗
yyunffu 2008-09-16
  • 打赏
  • 举报
回复
你这个是不是链接了库文件?
加#pragma comment(lib, "ws2_32.lib")试试。
cnzdgs 2008-09-16
  • 打赏
  • 举报
回复
AfxSocketInit要加在对话框创建之前。
biaozai06 2008-09-16
  • 打赏
  • 举报
回复
建议LZ将m_socket = socket(AF_INET,SOCK_DGRAM,0); 改为
m_socket = socket(AF_INET,SOCK_STREAM,0); 试试
jameshooo 2008-09-16
  • 打赏
  • 举报
回复
在MFC中使用socket就没必要调用socket()函数了,直接使用CSocket类。

CSocket m_socket;
m_socket.Create(...);
taitanyt 2008-09-16
  • 打赏
  • 举报
回复
用GetLastError返回错误码看看
taitanyt 2008-09-16
  • 打赏
  • 举报
回复
用GetLastError返回错误码看看
sanshao27 2008-09-16
  • 打赏
  • 举报
回复
OnInitDialog()中的代码比InitInstance()中AfxSocketInit更早,调整下顺序




同意楼上的,

18,363

社区成员

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

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