socket 问题,请帮忙看看

hansx1981 2008-04-02 05:16:13
最近在学习孙鑫的vc教程,其中有个chat例子,按照例子写好了,编译没有问题,就是绑定端口时出错,xp 系统
代码如下
BOOL CChatDlg::InitSocket()
{
m_socket=socket(AF_INET,SOCK_DGRAM,0);
if(INVALID_SOCKET==m_socket)
{
AfxMessageBox("创建套接字失败");
return FALSE;
}
SOCKADDR_IN addrSock;
addrSock.sin_family=AF_INET;
addrSock.sin_port=htons(6000);
addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

int res;
res=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));

if(SOCKET_ERROR==res) / /bind返回值为SOCKET_ERROR
{
closesocket(m_socket);
int err=WSAGetLastError();
MessageBox("绑定失败");
return FALSE;
}
return TRUE;

}
...全文
180 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyuncxl 2008-04-08
  • 打赏
  • 举报
回复
贴错!
看这个:
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.

Error Codes
Error code Meaning
WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAEACCES
WSAEADDRINUSE A process on the machine is already bound to the same fully-qualified address and the socket has not been marked to allow address reuse with SO_REUSEADDR. For example, the IP address and port are bound in the af_inet case). (See the SO_REUSEADDR socket option under setsockopt.)
WSAEADDRNOTAVAIL The specified address is not a valid address for this machine.
WSAEFAULT The name or namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains an incorrect address format for the associated address family, or the first two bytes of the memory block specified by name does not match the address family associated with the socket descriptor s.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEINVAL The socket is already bound to an address.
WSAENOBUFS Not enough buffers available, too many connections.
WSAENOTSOCK The descriptor is not a socket.
wuyuncxl 2008-04-08
  • 打赏
  • 举报
回复
If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error code Meaning
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.


这里说得很明白!
Eleven 2008-04-08
  • 打赏
  • 举报
回复
看看WSAGetLastError()的返回值时什么?
ntcong 2008-04-08
  • 打赏
  • 举报
回复
编译都没问题了,头文件应该就不回错

我最近也在写socket的 也是频繁报绑定出错

最好是多试试其他端口看是否端口被占用?
yltao83 2008-04-08
  • 打赏
  • 举报
回复
头文件对了么?是否使用socket2?
m_tornado 2008-04-07
  • 打赏
  • 举报
回复
看他的初始化socket的代码你有加上去了么
WSADATA wsaData;
::WSAStartup(MAKEWORD(2,2),&wsaData);


这个最有可能
wujuan120 2008-04-07
  • 打赏
  • 举报
回复
以防端口被占用,你可以设个五位的端口号,诸如14875,希望能有用
Eleven 2008-04-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sangermax 的回复:]
1.看他的初始化socket的代码你有加上去了么
WSADATA wsaData;
::WSAStartup(MAKEWORD(2,2),&wsaData);
2.端口冲突
[/Quote]
萧乐颜 2008-04-07
  • 打赏
  • 举报
回复
呵呵,我以前也写过socket,也有端口冲突的错误,后来设值的时候都是设的8000多
zdleek 2008-04-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sangermax 的回复:]
1.看他的初始化socket的代码你有加上去了么
WSADATA wsaData;
::WSAStartup(MAKEWORD(2,2),&wsaData);
2.端口冲突
[/Quote]
ding
caixingxin 2008-04-02
  • 打赏
  • 举报
回复
我以前做的时候就是端口被占用了...
mr.zhoux 2008-04-02
  • 打赏
  • 举报
回复
1.看他的初始化socket的代码你有加上去了么
WSADATA wsaData;
::WSAStartup(MAKEWORD(2,2),&wsaData);
2.端口冲突
ouyangzhongmin 2008-04-02
  • 打赏
  • 举报
回复
可能是你的端口已经被使用了,当然不行了,试试把端口号改成其它数字

18,356

社区成员

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

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