我正在用 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!");
}
}
}
}
感谢您的帮助:)