调用accept()时出现异常
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用accept()时,第二个参数是您提供的用于存储地址信息的缓冲区,该缓冲区取决于您正在侦听的套接字的地址族。
因此,在调用accept()之前必须声明并分配第二个参数中指定的缓冲区,第三个参数应包含为缓冲区分配的大小。然后,该函数将填充缓冲区并返回由请求的信息填充的缓冲区的实际长度。
类似于:
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:
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.访问冲突读取位置 0x0000000
表示您获得了空指针访问。由于内存不足,malloc 会返回空指针,或者问题出在其他地方。我对套接字编程了解不多,但*sockPtr=accept(...)
看起来很可疑。Access violation reading location 0x0000000
means you got a nullpointer access. Eithermalloc
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.