问题标签 [winsock2]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - C++,Send() 函数发送额外字节
我在使用 Winsock2 包装器类(客户端-服务器)时遇到了问题,经过无数小时的困惑,我决定如果我询问您的意见会更好。
更具体地说,问题是每次我使用我的 Send() 函数时,客户端和服务器(并非总是如此!)都会发送一两个额外的字节!
例如,我使用 SendBytes("Hello") 并且 Recv 函数返回 "Hello•",并在字符数组的末尾带有 '•' 或其他随机字符。
当然,服务器发送字符串“X5”,客户端打印 strlens ...
发送 // 接收函数
帮助将不胜感激^_^。
c++ - UDP服务器连接和发送数据异常
我正在为教育目的制作异步(非阻塞 rly)套接字库。TCP 部分工作得很好,但是当涉及到 UDP 时,我遇到了奇怪的行为。以下代码按预期工作 - 服务器接收数据:
但是如果 Sleep() 或 client.Write("\x0", 1); 被注释掉 - 它停止工作。服务器只是不会获取数据。以下是我的图书馆的一些部分,可以让您了解套接字是如何制作的:
如你所见,没有什么花哨的。也许是一些潜规则,需要发送一个字节的数据来初始化连接或什么?我在这里真的很困惑。
编辑:按要求编写函数。name 变量与上面代码的 Connect 调用中设置的非常相同。
编辑:同样在 select() 循环中,我只检查套接字是否可读。可能是由于连接被初始化而导致套接字不可写的情况吗?如果是这种情况,它应该解决 First sleep。但是那么发送一个字节呢?
c - 编写 ftp 客户端程序以列出服务器上的文件的最佳方法?
我正在尝试在 Windows 中用 C 语言编写客户端-服务器程序。目标是从服务器接收目录列表。现在我试图以利用大多数资源的方式开发客户端服务器。
一种实现方式是服务器进行一次 send() 调用以发送单个文件的信息。因此,如果有 100 个文件,它会进行 100 次调用。但是我觉得它浪费了网络资源。据我所知,Windows 中 send() 或 recv() 的缓冲区大小为 8kb。但是单个文件的信息几乎不会有 1kb。那么有没有办法让 send() 调用来发送多个文件信息(文件信息存储在结构中。所以它们基本上形成了一个链表)?也许我可以在一次 Send() 调用中发送至少 8 个文件的信息。这应该会将 send() 调用的总数减少到最多 13 个。
那么基本上有没有办法通过 send() 发送链表?如果您能想到任何替代方法,请告诉我。
qt - 我不能使用 MinGW 包含 winsock2.h
我将 Qt Creator 与 MinGW 一起使用。
我包括“windows.h”并编译它,但它失败了。错误如下:
我以为 MinGW 路径丢失了,但事实并非如此。我怎样才能解决这个问题?
c - socket() 返回 -1 但 errno 0
我试图在 mingw 上创建一个 UDP 套接字,但socket()
返回-1
errno = 0
。奇怪的。我已经包含了winsock2.h。最初我有编译错误undefined reference to socket@12
,在设置
-lws2_32
和-lwsock32
链接代码设置后::块,编译成功。
结果 --> sockfd -1 ERROR: No error , errno 0
好的,我将 RDF_LOG 改为 fprintf。
返回的结果仍然是 --> sockfd -1 socket: No error , errno 0 是不是mingw不支持errno?
c++ - 收听 UDP 广播
我需要收听来自 UDP 的网络广播。数据报包含一个j4cDAC_broadcast
结构。我尝试了一些教程,但他们似乎遗漏了一些东西,并且没有非常详细的解释(如果有的话)。
就我现在所拥有的,我收到一个错误BIND FAILED 10049
,错误 10049 表示该地址不可用。广播将在 255.255.255.255:7654 进行。如何修复此错误?
这是我到目前为止所拥有的:
另外,我找不到的另一件事是如何从标头中获取发件人的 IP 地址。
c++ - WinSock2:使用 recv 和 send 在单独的线程中处理接受的传入连接
我正在实现一个基于 Windows 的 Web 服务器,它使用WinSock2处理来自客户端的多个特定 HTTP 请求。我有一门课来启动和停止我的服务器。它看起来像这样:
whereTLM_ERROR
是我的服务器错误枚举的类型定义。
bool CMyServer::Start()
方法启动服务器,在配置的端口上创建一个套接字侦听并创建一个单独的线程DWORD CMyServer::Run(LPVOID)
来接受传入连接,如下所述:
I use ::accept(...)
to wait for incoming client connections in CMyServer::Run(LPVOID)
, and after new connection has been accepted I create a separate thread CMyServer::ProcessRequest(LPVOID)
to receive a data from a client and send a response passing the socket returned by ::accept(...)
as part of thread function's argument:
Testing this implementation manually gives me the desired results. But when I send multiple requests generated by JMeter I can see that some of them are not handled properly by DWORD CMyServer::ProcessRequest(LPVOID)
. Looking at log file created by ProcessRequest
I determine 10038 WinSock error code (meaning that ::recv
call was tried on nonsocket), 10053 error code (Software caused connection abort) or even 10058 error code (Cannot send after socket shutdown). But the 10038th error occurs more often than others mentioned.
It looks like a socket was closed somehow but I close it only after ::recv
and ::send
have been called in ProcessRequest
. I also thought that it can be an issue related to using ::CreateThread instead of ::_beginthreadex but as I can get it could only lead to memory leaks. I don't have any memory leaks detected by the method described here so I have doubts that it is the reason. All the more, ::CreateThread
returns a handle that can be used in ::WaitForMultipleObjects to wait for threads to be over, and I need it to stop my server properly.
Could these errors occur due to a client doesn't want to wait for response anymore? I am out of ideas, and I will thank you if you tell me what I am missing or doing/understanding wrong. By the way, both my server and JMeter run on the localhost.
Finally, here is my implementation of ProcessRequest
method:
winsock2 - 使用 Getaddrinfo 方法的概念查询
使用 getaddrinfo 方法时,我提供了 IP 地址和端口号。我在 out 变量中得到了一个链表。它工作正常。但我在这里有一个概念上的疑问。当我同时提供 IP 地址和端口时,怎么会有这么多可能的套接字返回?不应该只有一个吗?
c++ - 为什么不连接到本地主机的简单套接字?
我正在学习一个教我如何使用 win32 套接字(winsock2)的教程。我正在尝试创建一个连接到“localhost”的简单套接字,但是当我尝试连接到本地主机(在函数 connect() 处)时,我的程序失败了。
我需要管理员权限才能连接到本地主机吗?也许这就是它失败的原因?也许我的代码有问题?我已经尝试了端口 8888 和 8000,但它们都失败了。
此外,如果我将端口更改为 80 并连接到 www.google.com,我可以连接但我没有得到任何回复。那是因为我没有发送 HTTP 请求还是我打算得到一些响应?
这是我的代码(已删除包含):