socket绑定失败!!!

wzwst 2007-05-02 12:58:50
为什么我调用bind失败啊?
链接了ws2_32.lib也加了头文件,也WSAStartup了,运行书上的源代码还是提示绑定失败,怎么了啊
...全文
1345 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
crystalbug 2010-07-28
  • 打赏
  • 举报
回复
端口已被其他程序占用了,你换一个别的端口,或者随机选择另一个其他可用端口
liujunqi67 2010-07-27
  • 打赏
  • 举报
回复
应该是6000端口被占用吧
lijianli9 2010-07-26
  • 打赏
  • 举报
回复
端口已经在使用了,你可以换一个端口试试看,比如 6001,其他的,
xumaojun 2010-07-26
  • 打赏
  • 举报
回复
10048错误:端口已经被使用,在cmd输入netstat查看一下什么程序在用
WSAEADDRINUSE
10048

Address already in use.
Typically, only one usage of each socket address (protocol/IP address/port) is permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that was not closed properly, or one that is still in the process of closing. For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). Client applications usually need not call bind at all— connect chooses an unused port automatically. When bind is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is committed. This could happen with a call to another function later, including connect, listen, WSAConnect, or WSAJoinLeaf.
Csuxiaowu 2010-07-26
  • 打赏
  • 举报
回复
还一个端口号试一试
Eleven 2010-07-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 wzwst 的回复:]
为什么我调用bind失败啊?
链接了ws2_32.lib也加了头文件,也WSAStartup了,运行书上的源代码还是提示绑定失败,怎么了啊
[/Quote]
sorry,你bind失败,WSAGetLastError()返回值是什么?
Eleven 2010-07-26
  • 打赏
  • 举报
回复
你哪里错误了?App的InitInstance()中错误?你至少要先找到哪里错误吧。。。
jhayumu 2010-07-26
  • 打赏
  • 举报
回复
int Error =WSAGetLastError();
printf("%d",Error);
m_tornado 2010-07-13
  • 打赏
  • 举报
回复
换个2000看看~
lijianli9 2010-07-13
  • 打赏
  • 举报
回复
端口已经在使用了,
wanglei5695312 2010-07-13
  • 打赏
  • 举报
回复
出现10048啊
aj3423 2007-05-03
  • 打赏
  • 举报
回复
if(SOCKET_ERROR==bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR)))
{
wsprintf(buff, "绑定失败!错误号: %d",WSAGetLastError());
return FALSE;
}
HolpFalcon 2007-05-03
  • 打赏
  • 举报
回复
绑定6000端口偶尔会和QQ冲突,试试换个端口号
CALCULUS 2007-05-03
  • 打赏
  • 举报
回复
用GetLastError()看看错误号~
wzwst 2007-05-02
  • 打赏
  • 举报
回复
InitSocket 在OninitDialog里调用
wzwst 2007-05-02
  • 打赏
  • 举报
回复
BOOL CChatApp::InitInstance()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
AfxMessageBox("Start up failed!");
return FALSE;
}


if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
AfxMessageBox("Not the right version you requeset!");
WSACleanup( );
return FALSE;
}

AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CChatDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}

BOOL CChatDlg::InitSocket()
{
m_socket=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,0);
if(INVALID_SOCKET==m_socket)
{
MessageBox("创建套接字失败!");
return FALSE;
}
SOCKADDR_IN addrSock;
addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSock.sin_family=AF_INET;
addrSock.sin_port=htons(6000);

if(SOCKET_ERROR==bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR)))
{
MessageBox("绑定失败!");
return FALSE;
}
if(SOCKET_ERROR==WSAAsyncSelect(m_socket,m_hWnd,UM_SOCK,FD_READ))
{
MessageBox("注册网络读取事件失败!");
return FALSE;
}

return TRUE;
}
an_bachelor 2007-05-02
  • 打赏
  • 举报
回复
贴代码出来 至少把错误号发出来
不然怎么知道你的是什么错

18,356

社区成员

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

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