调用accept()时出现异常

发布于 2024-12-16 22:11:59 字数 657 浏览 0 评论 0原文

//this is server side code snippet used for chat application 

int* sockPtr;
int result;
result=listen(sClient,10); //sClient is SOCKET that is bind to specific port for  listining incoming connection 
sockPtr = (int*)malloc(sizeof(int));        
*sockPtr= accept(sClient,(SOCKADDR*)&client_info,&addrlen); //here is the problem on sockPtr [which is integer type pointer]

但是当客户端尝试连接时出现异常,提示

Unhandled exception at 0x0041ce6b in server.exe: 0xC0000005:
Access violation reading location 0x0000000.

我该怎么办?

//this is server side code snippet used for chat application 

int* sockPtr;
int result;
result=listen(sClient,10); //sClient is SOCKET that is bind to specific port for  listining incoming connection 
sockPtr = (int*)malloc(sizeof(int));        
*sockPtr= accept(sClient,(SOCKADDR*)&client_info,&addrlen); //here is the problem on sockPtr [which is integer type pointer]

but when a client try to connect there is an exception saying

Unhandled exception at 0x0041ce6b in server.exe: 0xC0000005:
Access violation reading location 0x0000000.

What shall I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法少女 2024-12-23 22:11:59

调用accept()时,第二个参数是您提供的用于存储地址信息的缓冲区,该缓冲区取决于您正在侦听的套接字的地址族。

因此,在调用accept()之前必须声明并分配第二个参数中指定的缓冲区,第三个参数应包含为缓冲区分配的大小。然后,该函数将填充缓冲区并返回由请求的信息填充的缓冲区的实际长度。

类似于:

struct sockaddr_in client_info;
int                client_info_length;
[...]

client_info_length = sizeof(client_info);
*sockPtr= accept(sClient,(SOCKADDR*)&client_info,&client_info_length);

struct sockaddr_in 结构存储“互联网”地址族 (TCP/IP) 的地址。如果您正在使用其他协议(我对此表示怀疑......),请使用适当的结构。

when calling accept(), the second parameter is a buffer you provide to store the address informations, which depends on the address family of the socket you are listening for.

so, the buffer specified in the second parameter have to be declared and allocated before calling the accept(), and the third parameter should contain the allocated size for the buffer. the function will then fill the buffer and return the actual length of the buffer which was filled by the requested informations.

something like:

struct sockaddr_in client_info;
int                client_info_length;
[...]

client_info_length = sizeof(client_info);
*sockPtr= accept(sClient,(SOCKADDR*)&client_info,&client_info_length);

the struct sockaddr_in structure stores an address for an "internet" address family (TCP/IP). if you are using another protocol (which i doubt...), use the appropriate structure.

或十年 2024-12-23 22:11:59

访问冲突读取位置 0x0000000 表示您获得了空指针访问。由于内存不足,malloc 会返回空指针,或者问题出在其他地方。我对套接字编程了解不多,但 *sockPtr=accept(...) 看起来很可疑。

Access violation reading location 0x0000000 means you got a nullpointer access. Either malloc returns a nullpointer since you're out of memory, or you the problem lies somewhere else. I don't know much about socket programming, but that *sockPtr= accept(...) looks suspicios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文