编译器选项导致debug模式下accept函数出错?

xiaohyy 2003-02-23 12:25:19
运行《windows网络编程 第2版》第一章列子tcpserver时,遇到以下错误:

当在debug模式下运行时,accept函数出错,错误代码10014。
查看该错误代码为:一个封锁操作被对 WSACancelBlockingCall 的调用中断。

而在release模式下执行则正常。我把release模式下的编译起开关选项拷贝到debug模式下做一些简单改动既运行正常。请问各位应该是哪个选项的问题?
...全文
167 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohyy 2003-02-23
  • 打赏
  • 举报
回复
呵呵,就是就是,《windows网络编程 第2版》第一章列子tcpserver。
HongHuer 2003-02-23
  • 打赏
  • 举报
回复
//加下面的一句。ClientAddrLen变量没有初始化,
//这是书上的例子吗?
//居然有这样的错误,哈哈。
ClientAddrLen = sizeof(SOCKADDR);

if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
{
printf("accept failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}
xiaohyy 2003-02-23
  • 打赏
  • 举报
回复
#include <winsock2.h>
#include <stdio.h>

void main(void)
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int ClientAddrLen;
int Port = 5150;
int Ret;
char DataBuffer[1024];

// Initialize Winsock version 2.2

if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.

printf("WSAStartup failed with error %d\n", Ret);
return;
}

// Create a new socket to listening for client connections.

if ((ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
printf("socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
}

// Setup a SOCKADDR_IN structure that will tell bind that we
// want to listen for connections on all interfaces using port
// 5150. Notice how we convert the Port variable from host byte
// order to network byte order.

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

// Associate the address information with the socket using bind.

if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf("bind failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}

// Listen for client connections. We used a backlog of 5 which is
// normal for many applications.

if (listen(ListeningSocket, 5) == SOCKET_ERROR)
{
printf("listen failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}

printf("We are awaiting a connection on port %d.\n", Port);

// Accept a new connection when one arrives.

if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
{
printf("accept failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}


printf("We successfully got a connection from %s:%d.\n",
inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port));

// At this point you can do two things with these sockets. Await
// for more connections by calling accept again on ListeningSocket
// and start sending or receiving data on NewConnection. For
// simplicity We will stop listening for more connections by closing
// ListeningSocket. We will start sending and receiving data on
// NewConnection.

closesocket(ListeningSocket);

// Start sending and receiving data on NewConnection. For simplicity,
// we will just receive some data and report how many bytes were
// received.

printf("We are waiting to receive data...\n");

if ((Ret = recv(NewConnection, DataBuffer, sizeof(DataBuffer), 0))
== SOCKET_ERROR)
{
printf("recv failed with error %d\n", WSAGetLastError());
closesocket(NewConnection);
WSACleanup();
return;
}

printf("We successfully received %d byte(s).\n", Ret);

// For this application we do not plan to do anything else with the
// connection so we will just close the connection.

printf("We are now going to close the client connection.\n");

closesocket(NewConnection);

// When your application is finished handling the connections
// call WSACleanup.

WSACleanup();
}
xiaohyy 2003-02-23
  • 打赏
  • 举报
回复
To: HongHuer(痛苦:我什么都不懂)

没用
typedef struct sockaddr SOCKADDR;
是一样的。。
HongHuer 2003-02-23
  • 打赏
  • 举报
回复
我用error lookup查看10014是:一个封锁操作被对 WSACancelBlockingCall 的调用中断啊。

是错的,你可以看 头文件 winsock.h

有定义 10014,是我上面说的。
(奇怪,Error lookup 居然不同)


SOCKET accept(
SOCKET s,
struct sockaddr FAR *addr,
int FAR *addrlen
);

accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen))
改为
accept(ListeningSocket, (struct sockaddr *)&ClientAddr,
&ClientAddrLen))
试试。

天限天空 2003-02-23
  • 打赏
  • 举报
回复
up
xiaohyy 2003-02-23
  • 打赏
  • 举报
回复
To: HongHuer(痛苦:我什么都不懂)
if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
{
printf("accept failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return 0;
}

得到结果是:
accept failed with error 10014

我用error lookup查看10014是:一个封锁操作被对 WSACancelBlockingCall 的调用中断啊。
HongHuer 2003-02-23
  • 打赏
  • 举报
回复
#define WSABASEERR 10000
#define WSAEFAULT (WSABASEERR+14)

使用Error lookup :

10014 系统检测到在一个调用中尝试使用指针参数时的无效指针地址。

WSAEFAULT The addrlen parameter is too small or addr is not a valid part of the user address space.


看看你的 accpt()函数吧。
贴出来。

//by the way:使用了这个地址转换没有(struct sockaddr_in *)

18,356

社区成员

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

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