求教:scoket绑定时的问题!!!

ufox 2002-03-30 11:35:03
CSocket *m_pSocketSrv
m_pSocketSrv = new CSocketServer();

//PORT_FOR_ESAS==7800 lpszIPAddr=="192.168.0.6"
m_pSocketSrv->Create(PORT_FOR_ESAS,SOCK_STREAM,lpszIPAddr)
if(!m_pSocketSrv->Bind(PORT_FOR_ESAS,lpszIPAddr))
{
int n = GetLastError();
if (n == WSAEINVAL) //这正是返回的错误值
AfxMessageBox("The socket is already bound to an address.");
}

Bind为什么会发生错误,错误是WSAEINVAL
谢谢!!!!
...全文
55 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
slimak 2002-03-30
  • 打赏
  • 举报
回复
你们说得一个是用WinSocket一个是用MFC封装的CSocket类的办法.
的确建立服务器要Bind,所以说CSocket把它封装到了Create函数里
面,所以你就不用再显式的调用Bind了.而在WinSocke API中还是需要
安照C/S模式socket-->bind-->listen......
bailange 2002-03-30
  • 打赏
  • 举报
回复
bind
The Windows Sockets bind function associates a local address with a socket.

int bind(
SOCKET s,
const struct sockaddr FAR *name,
int namelen
);
Parameters
s
[in] Descriptor identifying an unbound socket.
name
[in] Address to assign to the socket from the SOCKADDR structure.
namelen
[in] Length of the value in the name parameter.
Return Values
If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

这是MSDN上的关于Bind的解释!

好象建立服务器套接口,都要用Bind才行呀!

ufox 2002-03-30
  • 打赏
  • 举报
回复
可msdn上如下解释的:
BOOL CAsyncSocket::Bind(...)
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function
bailange 2002-03-30
  • 打赏
  • 举报
回复
请注意:

bind()的返回值,成功为0,否则为非0值!

if(!m_pSocketSrv->Bind(PORT_FOR_ESAS,lpszIPAddr))
{
语句...
}
中,如果成功bind,则返回的是0,那么!0的话就是真了,所以就会做花括号中的内容!
slimak 2002-03-30
  • 打赏
  • 举报
回复
原因很简单:因为Create()操作已经包含了Bind()操作,所以说你的第二次Bind()就会报错说该地址已经被Bind过了.
在MSDN里面对Create有这样的解释(请注意最后一句话)
Remarks

Call the Create member function after constructing a socket object to create the Windows socket and attach it. Create then calls Bind to bind the socket to the specified address.

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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