Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
客户端未检测到C#服务器超时_C#_Sockets_Tcp_Timeout_Tcpclient - Fatal编程技术网

客户端未检测到C#服务器超时

客户端未检测到C#服务器超时,c#,sockets,tcp,timeout,tcpclient,C#,Sockets,Tcp,Timeout,Tcpclient,我正在编写一个C#TCP套接字服务器,由于超时,我无法检测到断开的连接 我的意思是,我将服务器配置为一个特定的超时,然后在没有写入任何内容的情况下关闭客户机&网络流(我也尝试过使用socket) 在客户端未检测到这一点。客户机无论如何都会执行“写入”操作,并在没有任何错误的情况下退出 这是服务器的代码: 谢谢, 费边 客户端代码有问题,但你们只发布服务器代码?客户端代码有问题,但你们只发布服务器代码? using System; using System.Net; using System.Ne

我正在编写一个C#TCP套接字服务器,由于超时,我无法检测到断开的连接

我的意思是,我将服务器配置为一个特定的超时,然后在没有写入任何内容的情况下关闭客户机&网络流(我也尝试过使用socket)

在客户端未检测到这一点。客户机无论如何都会执行“写入”操作,并在没有任何错误的情况下退出

这是服务器的代码:

谢谢, 费边


客户端代码有问题,但你们只发布服务器代码?客户端代码有问题,但你们只发布服务器代码?
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MyTcpServer {

    private const int BUFSIZE = 32;
    static void Main(string[] args) {
        int servPort = 5009;
        TcpListener listener = null;
        try {

            listener = new TcpListener(IPAddress.Any, servPort);
            listener.Start();
        } catch (SocketException se) {
            Console.WriteLine(se.ErrorCode + ": " + se.Message);
            Environment.Exit(se.ErrorCode);
        }
        byte[] rcvBuffer = new byte[BUFSIZE];
        for (;;) {
            Socket socket = null;

            try {
                socket = listener.AcceptSocket();
                socket.ReceiveTimeout = 3000;
                Console.Write("Handling client - ");

                Console.WriteLine("Reading from client");
                socket.Receive(rcvBuffer, rcvBuffer.Length, SocketFlags.None);
                string line = Encoding.UTF7.GetString(rcvBuffer);
                Console.WriteLine(line);
                Console.WriteLine("Closing connection");
                socket.Close();
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
    }
}