简体   繁体   English

10038套接字错误

[英]10038 socket error

Is there any solution for 10038 server error .i have done coding in c++; 10038服务器错误是否有任何解决方案.i已在c ++中完成编码; the server runs fine 10 to 12 hours but sudenly it gives 10038 socket error 服务器运行良好10到12小时,但很容易出现10038套接字错误

Without seeing your code: the symptom you describe sounds like you are leaking memory/resources, ie 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. 10038(WSAENOTSOCK):非插槽上的套接字操作。 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. 套接字句柄参数未引用有效套接字,或者对于select,fd_set的成员无效。

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. 这是WinSock编程中一个非常常见的时间错误 - 好消息(和坏消息,因为它很难重现)是你不经常打它,所以你的代码可能不需要太多的工作来完善它。 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. 我认为你应该添加线程安全的诊断,在每次打开和关闭时,以及从你看到10038或其他意外错误的任何地方输出包含套接字值(基本上是int )的字符串。

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. 如果你可以添加这些诊断,然后设置一个压力测试,专注于程序中的打开和关闭区域(你可能需要将代码拆分为一个小的子集,用于单元测试套接字处理,也许这样做可以回到 - 在本地主机或两台连接局域网的机器上然后它可能会比10-12小时更快地显示,你可以找到并修复其他时间窗口。 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. 目标是尝试将10-12小时的“正常”套接字活动压缩到尽可能小的时间空间,以真正暴露任何难以检测的并发问题。

There may be two reasons for this: 这可能有两个原因:

  1. Your socket descriptor in uninitialized (ie doesn't reference a valid socket). 您的套接字描述符未初始化(即不引用有效的套接字)。
  2. You closed this socket (by a call to closesocket ), and still try to use it. 你关闭了这个套接字(通过调用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,您必须修复代码。

PS 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_SOCKET at the beginning. 无论如何,在开头将套接字描述符初始化为INVALID_SOCKET是一个很好的做法。 Also set it to INVALID_SOCKET immediately after you close it. 关闭后立即将其设置为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. 通过这种方式,您可能会发现有问题的情况。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM