问题标签 [berkeley-sockets]
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# - Socket Shutdown: when should I use SocketShutdown.Both
I believe the shutdown sequence is as follows (as described here):
The MSDN documentation (remarks section) reads:
When using a connection-oriented
Socket
, always call theShutdown
method before closing theSocket
. This ensures that all data is sent and received on the connected socket before it is closed.
This seems to imply that if I use Shutdown(SocketShutdown.Both)
, any data that has not yet been received, may still be consumed. To test this:
- I continuously send data to the client (via
Send
in a separate thread). - The client executed
Shutdown(SocketShutdown.Both)
. - The
BeginReceive
callback on the server executes, however,EndReceive
throws an exception: An existing connection was forcibly closed by the remote host. This means that I am unable to receive the0
return value and in turn callShutdown
.
As requested, I've posted the Server side code below (it's wrapped in a Windows Form and it was created just as an experiment). In my test scenario I did not see the CLOSE_WAIT
state in TCPView as I normally did without sending the continuous data. So potentially I've done something wrong and I'm interrupting the consequences incorrectly. In another experiment:
- Client connects to server.
- Client executes
Shutdown(SocketShutdown.Both)
. - Server receives shutdown acknowledgement and sends some data in response. Server also executes
Shutdown
. - Client receives data from server but the next
BeginReceive
is not allowed: A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call
In this scenario, I was still expecting a 0
return value from EndReceive
to Close
the socket. Does this mean that I should use Shutdown(SocketShutdown.Send)
instead? If so, when should one use Shutdown(SocketShutdown.Both)
?
Code from first experiment:
c++ - EHOSTDOWN 和 EHOSTUNREACH 在 connect() 中是否致命?
当这些错误发生在connect()
(非阻塞)时如何响应?我想知道我是否应该杀死这个套接字并创建一个新的,或者我可以等待一段时间然后用现有的套接字再试一次,一切都会奏效(如果远程主机在线)?
sockets - 如何禁用设置的超时 setsockopt()?
在我正在开发的程序中,我设置了一个超时使用setsockopt()
,以防止recvfrom()
无限期阻塞。如何禁用超时?
(我在 Ubuntu 上)
c - epoll ET,我应该在监听套接字上订阅哪些事件?
我有一个 fd:
我有一个epoll
例子。我需要知道,我应该通过哪些事件订阅epoll_ctl
?我需要边缘触发模式。
我有那些标志 atm:EPOLLET | EPOLLIN
我应该订阅EPOLLRDHUP
, EPOLLOUT
,EPOLLPRI
吗?
我应该处理EPOLLHUP
吗EPOLLERR
?为什么它们会发生?
c - C中的IPv6连接问题
我正在尝试编写一个不可知的回显服务器,它可以接受 IPv4 和 IPv6 连接。我正在使用 addrinfo 结构,使用 getaddrinfo 设置。
Ipv4 连接没有问题,而我无法获得有效的 ipV6 连接。我认为我的问题可能是由于错误的 getaddrinfo 参数,但我无法看到我哪里出错了。
这是我的代码
client.c
服务器.c
c - SO_RCVBUF 大小与传入数据包大小有何关联?
将套接字接收缓冲区设置为指定的字节数是否与可以存储多少大小的消息直接相关?
示例:
如果100
字节消息通过 UDP 连续发送到设置为4,000
字节的套接字缓冲区,我可以期望缓冲区能够保存40
消息吗?
我认为设置缓冲区大小,如下所示:
并让缓冲区从传入的数据包中填充,将导致缓冲区包含 40 条消息。
After turning off the UDP sender, and processing the buffer, that is not what I have observed.
Despite my messages being 100
bytes, it seems as though a 4,000
byte buffer could only hold about 4
messages.
How could 100
byte messages be taking up 1,000
bytes in the buffer?
Does this make sense? What is causing this, and how can I calculate a buffer size in accordance to how many messages can be held?
edit: duplicated question does not solve my problem.
The user there was calling setsockopt
incorrectly.
I'm trying to find documentation that describes the relationship between a socket recieve buffer, and the number of sized messages that can actually be held.
c++ - 如何使 .cpp 文件充当可访问的服务器
我用 Linux(Cent OS 7.0)和 C++ 编写了一个简单的程序。它是一个非常小的服务器,它向客户端发送回一串字符。但我的问题是我不知道如何使用 IP 地址访问该服务器?
我使用了 Linux Socket Interface (Berkeley),在定义地址的部分,我的代码执行以下操作:
我使用INADDR_ANY
我的服务器地址,它在其定义中定义为:
现在,我应该如何运行服务器,然后使用我的简单客户端程序向它发送请求。我的简单客户端程序接受一个 IP 地址作为它的目标地址,这个地址应该是发往服务器的地址。那我该怎么关联呢?
c++ - 基于 UNIX 套接字的客户端程序中的 C++ connect() 未建立到服务器的连接
我使用著名的伯克利套接字接口在 CentOS 7.0 中用 c++ 编写了一个简单的套接字服务器。我在任何端口上运行它,它等待连接。
然后我运行我也用 c++ 编写的简单客户端程序并向
192.168.122.1
(通过执行命令找到此 IP)发送请求,ip addr
但它拒绝连接。考虑到防火墙,我停止了 httpd.service (APACHE) 并在端口 80 上执行该过程,但无济于事,我收到错误“连接被拒绝”。
我该怎么办?
** UPDATE 001 ** 当我运行命令时,netstat -l
我得到以下输出:
**更新 001 结束** 以下是输出:
客户端--> 连接被拒绝
服务器--> [等待中...]
以下是代码:
客户:
服务器
c++ - Linux c++ recvfrom() 更改(销毁)[socket] 文件描述符
我写了一个简单的UDP服务器。好吧,我自然会recvfrom()
在其中的某个地方使用函数。我在网上搜了一下,发现是缓冲区溢出引起的。这是真的?但我不知道为什么我的代码失败并抛出相同的错误,这是与相关的部分recvfrom()
:
调用之前的文件描述符recvfrom()
是3
,但是当我调用它时,它变为-187301886
c++ - 使用 Berkeley 套接字的线程通知
我正在通过以下方式使用 Berkeley 套接字选择功能。
我编写了一个类,它将监视一个套接字容器(包装在另一个类中)并将一个 ID 添加到一个单独的容器中,该容器存储有关哪些套接字准备好被访问的信息。地图是 unordered_map。
我相信很多人已经注意到,这段代码运行得非常快,并且像没有明天一样吞噬 CPU(40% 的 CPU 使用率,地图中只有一个套接字)。我的第一个解决方案是拥有一个智能等待功能,将每秒的迭代次数保持在一个设定值。这对某些人来说似乎很好。 我的问题是:如果不使用此方法,如何在套接字准备好时收到通知? 即使它可能需要一堆宏垃圾来保持便携,也没关系。我只能认为可能有某种方法可以让操作系统为我监视它并在套接字准备好时获得某种通知或事件。为了清楚起见,我选择不使用点网。
循环在自己的线程中运行,当套接字准备好时向软件的其他部分发送通知。整个事情是多线程的,它的每个部分(除了这部分)都使用一个基于事件的通知系统,消除了繁忙的等待问题。我知道这方面的事情变得依赖于操作系统并且受到限制。
编辑:套接字以阻塞模式运行(但选择没有超时,因此不会阻塞),但它们在专用线程中运行。编辑:该系统在其上的智能睡眠功能上表现出色,但不如在某些通知系统到位(可能来自操作系统)的情况下那么好。