C# Socket.receive连续接收0字节且循环中不阻塞

2024-05-03

我正在尝试用 C# 编写一个最简单的多线程 TCP 服务器,它接收来自多个客户端的数据。

每次连接新客户端时,都会建立套接字连接,并将套接字作为参数传递给新类函数,之后运行 while 循环并接收数据,直到客户端连接为止。

这里的问题是“socket.receive”没有阻塞并且接收到0字节的数据。因此循环连续运行,不会在 socket.receive 处阻塞(代码中的“clientSocket.Receive(bb)”)。

我使用Chrome浏览器作为客户端进行测试。 即使我使用任何其他客户端,TCP 服务器的行为也保持不变。

客户端只发送一次数据,但服务器不断收到0字节,并且while循环继续运行。

我粘贴服务器输出以供参考。

请帮助阻止服务器在 socket.receive 等待下一个客户端传输。 奇怪的是,即使接收到0个字节,异常也没有被调用。

请帮忙。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
Threading;
using System.Timers;
using System.Security;
using System.Security.Permissions;
namespace SERVER
{
    static class Constants
    {
        public const int port = 8080;
        public const int buffer_size = 512;


    }
    class Program
    {
        static public string LocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
            //return ip;
        }

        static void Main(string[] args)
        {
            //IPEndPoint ipObj = new IPEndPoint(IPAddress.Parse("Server IP goes here"), 20487); //20487 is port. you can change it according to your wish
            System.Net.IPAddress IP = IPAddress.Any;
            int port = Constants.port;
            TcpListener listnerObj = new TcpListener(IP, port);
            listnerObj.Start();
            string client_addr;
            string[] client_addr_split;
            string IP_string = LocalIPAddress();

            Console.WriteLine("Server Started on {0}:{1}", IP_string, port);
            while (true)
            {
                Console.WriteLine("================================");
                Console.WriteLine("**    Waiting For Client     **");
                Socket clientSocket = listnerObj.AcceptSocket(); // waiting for the client to connect

                client_addr = clientSocket.RemoteEndPoint.ToString();
                client_addr_split = client_addr.Split(':');
                client_addr = client_addr_split[0];

                Console.WriteLine("Client Connected {0}", client_addr);
                ParameterizedThreadStart thread = delegate { new communication().doCommunicate(clientSocket, client_addr); };
                Thread th = new Thread(thread);
                th.Start(); // start the thread here 
            }
        }

        class communication
        {
            public int byteReceived;
            public byte[] bb;
            public void doCommunicate(Socket clientSocket, string client_addr)
            {
                clientSocket.Blocking = true;
                bb = new byte[Constants.buffer_size];

                //Console.WriteLine("***** Entered DoCommunicate *****");
                while (clientSocket.Connected)
                {
                    //Console.WriteLine("Entered While");
                    try
                    {
                        //Console.WriteLine("Entered TRY");
                        Console.WriteLine("Waiting to recieve Data from IP : client_addr");
                        //int ReceivedDataLength = Client.Receive(ReceivedBytes, 0, ReceivedBytes.Length, SocketFlags.None);
                        byteReceived = clientSocket.Receive(bb, 0, bb.Length, SocketFlags.None);
                        //byteReceived = clientSocket.Receive(bb);
                    }
                    catch (SocketException e)
                    {
                        Console.WriteLine("Error: Socket Exception.\n{0}\n{1}.", e.Message, e.ErrorCode);
                        break;
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine("Error : Argument Null Exception.\n{0}", e.Message);
                        break;
                    }
                    catch (ObjectDisposedException e)
                    {
                        Console.WriteLine("Error : Socket Disposed Exception Caught.\n{0}", e.Message);
                        break;
                    }
                    catch (SecurityException e)
                    {
                        Console.WriteLine("Error: Security Exception.\n{0}", e.Message);
                        break;
                    }
                    //clientSocketglobal.Send(Encoding.Default.GetBytes("Hello Client"), SocketFlags.None);
                    Console.WriteLineWriteLine("Received Byte count : {0}, from IP : {1}", byteReceived, client_addr); // Do whatever you want to do with the data recieved. Parsing and storing etc.
                    Console.WriteLine(Encoding.UTF8.GetString(bb));
                }
                //Console.WriteLine("While Loop Exited");
                Console.WriteLine("Socked and Class Object  Disposed");
                clientSocket.Close();
                clientSocket.Dispose();
                GC.Collect();
            }
        }
    }
}

服务器的输出:

Server Started on 10.0.0.2:8080
================================
**    Waiting For Client     **
Client Connected 10.0.0.2
================================
**    Waiting For Client     **
Client Connected 10.0.0.2
================================
**    Waiting For Client     **
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 386, from IP : 10.0.0.2
GET /HelloWorld HTTP/1.1
Host: 10.0.0.2:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6


Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Waiting to recieve Data from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2
Received Byte count : 0, from IP : 10.0.0.2

如果您收到 0 字节,通常意味着发送方已关闭其发送套接字。在一个Socket, the send and receive通道是分开的;我预计发生的情况是您的发送(他们的接收)仍然打开且可用,因此clientSocket.Connected返回true(您仍然可以向他们发送回复),但是:他们在发送有效负载后立即关闭了发送(您的接收)(这很常见,以指示批次的结束)。基本上,您只需要检测 0 字节接收,并将其视为结束:不会有更多数据ever一旦您收到非肯定的回复,就会收到。因此,只需编写您需要编写的任何响应(他们仍然可以监听,即使他们再也不会说话),然后关闭套接字。

附带说明:在 HTTP/1.1 中Connection: keep-alive, the can保持套接字打开,准备发送下一个请求 - 但在这种情况下,他们根本没有这样做。他们在发出请求后立即关闭套接字。这很好。只需提供响应并完全关闭套接字即可。不会有更多请求传入。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# Socket.receive连续接收0字节且循环中不阻塞 的相关文章

随机推荐

  • C# 中的简单获取字符串(忽略末尾的数字)

    我认为正则表达式太过杀伤力 而且它需要我一些时间来编写一些代码 我想我现在应该学习 因为我知道一些正则表达式 分隔字母数字字符串中的字符串的最简单方法是什么 它将永远是 LLLLDDDDD 我只想要字母 l 通常只有 1 或 2 个字母 T
  • 如何为RenderTargetBitmap的输出设置透明度颜色?

    我正在尝试使用 RenderTargetBitmap 将具有透明背景的 Visual 对象保存到位图 public static RenderTargetBitmap RenderToBitmap this Visual Source in
  • LINQ TO ENTITY 无法与枚举类型进行比较

    下面是枚举叶子 public enum Leaves Annual 0 Medical 1 Hospitalization 2 Unpaid 3 下面是linq查询 public ActionResult ApproveLeave int
  • 在 Go 中获取机器 epsilon 的最简单方法

    在 Go 中获取机器 epsilon 的最简单方法是什么 浮点数的其他方面 例如精度 最小指数 最大指数 摆动等 又如何呢 我意识到有一个 math const 包 其中包含不同浮点类型的最大值和最小值 http golang org sr
  • 我应该如何使用 Perl URI 类?

    我需要在 Perl 程序中处理一些 HTTP URL 但我怀疑应该如何处理URI https metacpan org module URI类帮助我 特别是 我想使用URI用于解析相对 URL 并获取其组件的类 然而 问题是 我需要一个可以
  • Wordpress Cron 错误“SSL 证书:无法获取本地颁发者证书”

    我在安装 WordPress 时遇到错误 wp cron php 无法由 WordPress 执行 调试工具 Crontrol 报告错误 SSL证书 无法获取本地颁发者证书 WGET 无法访问 wp cron php 很可能是由于 SSL
  • 这段php代码安全吗?

    我知道我应该使用准备好的语句 但我的下一个项目将使用准备好的语句 我只需要完成这个简单的小应用程序 所以我的问题是 以下代码片段安全吗 我使用了 htmlentities 以及 mysql real escape string 因为我认为这
  • 如何更改鼠标进入/鼠标退出时按钮的图标图像?

    我想更改鼠标输入和鼠标退出时按钮的图标图像 private void jButton1MouseEntered java awt event MouseEvent evt this jButton1 setBackground Color
  • Django url 模式 - 带正斜杠的参数

    如何为两个参数创建 url 模式 其中第一个参数包含正斜杠作为其内容的一部分 da ta1 data2 最初我有以下模式 r view P
  • jQuery:在方法上取消绑定 jQuery 2.0

    在 jQuery 1 9 中live 已被弃用 因此新方法变为 document on mouseover blahblahfunc 我无法解除 blahblahfunc 的绑定 通过 unbind mouseover mouseout c
  • 如何在PHP中使用curl GET发送原始数据?

    我正在开发 REST API 虽然很容易在 cURL 中为 POST 请求设置原始 JSON 数据 payload json encode array user gt data attach encoded JSON string to t
  • CKFinder 如何在选择图像(文件:选择)时获取尺寸 URL 和尺寸(宽度/高度)?

    我正在使用 CkFinder 3 成功上传图像后 我需要能够在用户单击 选择 按钮后进行检测 文件名 ID url 原始图像的宽度和高度 目前我正在使用files choose但我找不到有关 cb 事件的信息 知道如何解决吗 代码示例将不胜
  • TSConfig JSX:React JSX 与 React

    在将 Typescript 与 React 一起使用时 我们必须指定jsx in compilerOptions in tsconfig json file It has preserve react react native react
  • go:找到模块但不包含包

    我正在尝试安装 go 的网络包 但收到 不包含包错误 终端截图 我咨询过 go 模块 latest 已找到但不包含包 https stackoverflow com questions 62974985 go module latest f
  • 使用 Pandas 滚动差异

    您好 我正在尝试使用 Pandas 滚动函数来计算下表中的滚动差异 我正在尝试生成 每月可用项目 列中的值 但没有得到任何结果 请帮忙 Item Adds Subtracts Month Monthly Available items A
  • 在opencv中保存帧而不压缩

    我正在尝试使用写 OpenCV 函数 我想保存帧 TIFF扩大 我遇到的问题是保存的图像被压缩 所以我无法使用它们 知道如何摆脱这种压缩吗 提前致谢 不要介意西奇说的话 TIFF 标志通过 LZW 压缩硬编码在 opencv 二进制文件中
  • 如何分发仅二进制的 go 包

    我想以二进制形式分发包而不包含源代码 我的演示项目目录结构是这样的 demo greet greet go hi hi go hello hello go main go main go package main import fmt de
  • 在提交 Google 表单时找不到具有给定 ID 的项目

    我创建了一个在 Google 表单上运行 onSubmit 的脚本 它应该获取上传到表单的图像的 ID 获取 Blob 形式的图像 然后将其转发到某个电子邮件地址 问题是 有时 大约十分之一 脚本会给出以下错误 例外 找不到具有给定 ID
  • 获取 WSA 错误代码的格式化消息

    我在 win32 C 应用程序中使用winsock2 我将使用 MessageBox 显示可以通过调用 WSAGetLastError 检索的网络错误 我怎样才能做到这一点 我看到 FormatMessage 但我不明白如何使用它 例如 以
  • C# Socket.receive连续接收0字节且循环中不阻塞

    我正在尝试用 C 编写一个最简单的多线程 TCP 服务器 它接收来自多个客户端的数据 每次连接新客户端时 都会建立套接字连接 并将套接字作为参数传递给新类函数 之后运行 while 循环并接收数据 直到客户端连接为止 这里的问题是 sock