回 帖 发 新 帖 刷新版面

主题:重问关于聊天程序的问题

重问关于聊天程序的问题!必加分!

调试 编译都通过,运行的时候才由MessageBox(hDlg,buff,"提示:",MB_OK)
提示绑定不成功!

bind()函数调用可能有问题,所以只列出了和该函数有关的代码

TCHAR buff[40];
static  sockaddr_in local;

local.sin_family=AF_INET;// 连接方式
local.sin_port=htons(5000);//该变量存放应用程序的端口号
local.sin_addr.s_addr=htonl(INADDR_ANY);//该变量存放IP地址

//绑定的时候我这样调用的
if(bind(server,(sockaddr*)&local,sizeof(local))==SOCKET_ERROR)
{
  wsprintf(buff,TEXT("%d"),WSAGetLastError);
  MessageBox(hDlg,buff,"提示:",MB_OK);
  return FALSE;
}

回复列表 (共12个回复)

沙发

先取得具体错误代码啊!!!!!
晕.

板凳

wsprintf(buff,TEXT("%d"),WSAGetLastError);
  MessageBox(hDlg,buff,"提示:",MB_OK);
用上面的代码取了啊,是个整型值,没资料可查啊,还忘你再指点一下

3 楼

晕,WINSOCK吗??
用WSAGetLastError.看看具体是什么错误:bind的错误代码如下:
If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, 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 has failed. 
WSAEACCES Attempt to connect datagram socket to broadcast address failed because setsockopt option SO_BROADCAST is not enabled. 
WSAEADDRINUSE A process on the computer 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 computer. 
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. 

大写的是错误代码宏,后面是解释.

4 楼

试过了,英文看不太明白

5 楼

WSAEACCES  注释短一点的英文我都能看懂,偏偏这个我没看懂的是我的错误!!

6 楼

你没有用setsockopt设置广播SO_BROADCAST选项,但又用了广播地址.

7 楼

我没法再给你评分了,这个答案应该30分

8 楼

哈哈......分数有那么重要吗???
如果只为分数,我不会帮你.

9 楼

只是强调了一下“setsockopt设置广播SO_BROADCAST选项”这个的重要性,真的很感谢你!我做一下总结!
谢谢你!!

10 楼

在写socket代码的时候,如果用到类似广播这样的地址,一定要设置其选项.否则通过不了编译,就算通过,也绑定地址和端口也不会成功!
  
节贴!

我来回复

您尚未登录,请登录后再回复。点此登录或注册