C++ 10038 套接字错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3948164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:06:46  来源:igfitidea点击:

10038 socket error

c++visual-c++sockets

提问by SunilRai86

Is there any solution for 10038 server error .i have done coding in c++; the server runs fine 10 to 12 hours but sudenly it gives 10038 socket error

10038服务器错误有什么解决方案吗?我已经用c++完成了编码;服务器运行正常 10 到 12 小时,但突然出现 10038 套接字错误

回答by Mitch Wheat

Without seeing your code: the symptom you describe sounds like you are leaking memory/resources, i.e. you are forgetting to free/delete objects you are allocating. It could also be a timing issue. I suggest you post your (cut-down) code.

没有看到您的代码:您描述的症状听起来像是在泄漏内存/资源,即您忘记释放/删除正在分配的对象。这也可能是时间问题。我建议您发布您的(缩减)代码。

10038 (WSAENOTSOCK): Socket operation on nonsocket.An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.

10038 (WSAENOTSOCK):非套接字上的套接字操作。试图对不是套接字的东西进行操作。套接字句柄参数未引用有效套接字,或者对于选择,fd_set 的成员无效。

回答by Steve Townsend

I bet you are accessing a socket that you already closed. This is a very common timing bug in WinSock programming - the good news (and bad news, because it's hard to reproduce) is that you are not hitting it very often so it's likely your code does not need much work to make it perfect. I think you should add thread-safe diagnostics that output a string including the socket value (an int, basically) on every open and close, and from anywhere you see this 10038 or other unexpected errors.

我敢打赌你正在访问一个你已经关闭的套接字。这是 WinSock 编程中一个非常常见的计时错误 - 好消息(也是坏消息,因为它很难重现)是您不经常使用它,因此您的代码可能不需要太多工作来使其完美。我认为您应该添加线程安全诊断,int在每次打开和关闭时输出一个字符串,包括套接字值(基本上是 an ),以及从您看到此 10038 或其他意外错误的任何地方。

If you can add those diagnostics and then set up a stress test that focuses on open and close areas in your program (you may need to strip down the code to a small subset for unit testing of the sockets handling, maybe doing this back-to-back on localhost, or to two LAN-connected machines) then it will likely manifest much more quickly than 10-12 hours and you may find and fix other timing windows along the way. The goal is to try to compress 10-12 hours of 'normal' socket activity into as small a space of time as possible, to really expose any hard-to-detect concurrency problems.

如果您可以添加这些诊断,然后设置一个压力测试,重点关注您程序中的打开和关闭区域(您可能需要将代码剥离到一个小的子集以对套接字处理进行单元测试,也许这样做会回到-返回本地主机,或到两台 LAN 连接的机器)然后它可能会比 10-12 小时更快地显示出来,并且您可能会在此过程中找到并修复其他时间窗口。目标是尝试将 10-12 小时的“正常”套接字活动压缩到尽可能小的时间空间中,以真正暴露任何难以检测的并发问题。

回答by valdo

There may be two reasons for this:

这可能有两个原因:

  1. Your socket descriptor in uninitialized (i.e. doesn't reference a valid socket).
  2. You closed this socket (by a call to closesocket), and still try to use it.
  1. 您的套接字描述符未初始化(即未引用有效的套接字)。
  2. 您关闭了这个套接字(通过调用closesocket),并且仍然尝试使用它。

Such an error is always a bug, it's not related to the real network activity/state and etc. This is equivalent (in some sense) to either trying to use a resource/memory after you free it, or simply referencing an uninitialized pointer.

这样的错误总是一个错误,它与真实的网络活动/状态等无关。这等效于(在某种意义上)释放资源/内存后尝试使用它,或者只是引用一个未初始化的指针。

So that in order to solve the 10038 you must fix your code.

所以为了解决 10038 你必须修复你的代码。

P.S. If you have a multi-threaded application - it's likely that you close the socket in one thread, whereas the other thread still trying to use it.

PS 如果您有一个多线程应用程序 - 很可能您在一个线程中关闭了套接字,而另一个线程仍在尝试使用它。

Anyway, there's a good practice to initialize socket descriptors to INVALID_SOCKETat the beginning. Also set it to INVALID_SOCKETimmediately after you close it.

无论如何,INVALID_SOCKET在开始时将套接字描述符初始化为一个很好的做法。INVALID_SOCKET关闭后立即将其设置为。

Then, before trying to use it you may check if the socket is valid. In such a way you may find the problematic scenario.

然后,在尝试使用它之前,您可以检查套接字是否有效。通过这种方式,您可能会发现有问题的场景。

回答by Ted W

Also look out for the fact that -- at least in Windows -- you will get 10038 if you try to send on a socket on one thread that was opened in a different thread.

还要注意这样一个事实——至少在 Windows 中——如果你尝试在一个线程上的套接字上发送一个在不同线程中打开的套接字,你将得到 10038。