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
C# 客户端断开连接时检测和写入_C#_Sockets_Client Server_Disconnect - Fatal编程技术网

C# 客户端断开连接时检测和写入

C# 客户端断开连接时检测和写入,c#,sockets,client-server,disconnect,C#,Sockets,Client Server,Disconnect,我正在用C#编写一个简单的客户机/服务器应用程序。 正如您看到的,代码中有if(cSocket.Connected)标记,我想要这样的东西。。。 如果cSocket断开连接。。。 我会给代码,你可以理解我的问题,从标题和我的解释 这是代码 服务器 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Socke

我正在用C#编写一个简单的客户机/服务器应用程序。 正如您看到的,代码中有if(cSocket.Connected)标记,我想要这样的东西。。。 如果cSocket断开连接。。。 我会给代码,你可以理解我的问题,从标题和我的解释

这是代码

服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        TcpListener sSocket = new TcpListener(System.Net.IPAddress.Any, 3162);
        int Counter = 0;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("\n >> Server Started!");
        sSocket.Start();
        while (true)
        {
            Socket cSocket = sSocket.AcceptSocket();
            NetworkStream NetworkStr = new NetworkStream(cSocket);
            BinaryReader bReader = new BinaryReader(NetworkStr);
            BinaryWriter bWriter = new BinaryWriter(NetworkStr);
            IPEndPoint remoteIpEndPoint = cSocket.RemoteEndPoint as IPEndPoint;

            if (cSocket.Connected)
            {
                Counter = Counter + 1;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\n >> Client Connected! ~ Total: " + Counter + " ~ [" + remoteIpEndPoint + "]");
                bWriter.Write("\n >> Server Says: You Connected to Me!");
            }
        }
    }
 }

谢谢你的帮助:)

我对.NET的东西不太熟悉,但理论上,应用程序和协议(包括TCP/IP)需要等待某种超时。与TCP/IP协议家族一样,一方在发送包后等待另一方一段时间,如果他没有得到答复,他会再次尝试并/或在一段时间后关闭连接


简而言之,您可以定期发送一个小请求,并检查客户端是否应答。

我对.NET的内容不太熟悉,但理论上应用程序和协议(包括TCP/IP)需要等待某种超时。与TCP/IP协议家族一样,一方在发送包后等待另一方一段时间,如果他没有得到答复,他会再次尝试并/或在一段时间后关闭连接


简言之,您可以定期发送一个小请求,并检查客户端是否应答。

正如Opi所说,这样做可以做到(它来自StackOverFlow中的另一个应答):


正如Opi所说的,类似这样的东西可以做到(来自StackOverFlow中的另一个答案):


你所说的连接是什么意思??您只能从连接的套接字初始化网络流。(我的意思是当AcceptSocket返回时它已经是连接的套接字)您所说的连接是什么意思??您只能从连接的套接字初始化NetworkStream。(我的意思是当AcceptSocket返回时它已经是连接的套接字)我的意思是在vb中有winsock控件,它有“close”事件,但在c中没有类似的事件,我将使用您的代码:)谢谢您的帮助…我的意思是在vb中有winsock控件,它有“close”事件,但是在c#中没有类似的事件,我将使用您的代码:)谢谢您的帮助。。。
 public static bool SocketConnected(Socket s)
        {
            if (!s.Connected) return false;
            bool part1 = s.Poll(1000, SelectMode.SelectRead);
            bool part2 = (s.Available == 0);
            if (part1 & part2)
                return false;
            return true;
        }