即時通訊在C#中編寫一個簡單的客戶端/服務器應用程序。 正如你所看到的那樣,如果(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!");
}
}
}
}
感謝您的幫助:)
你是什麼**連接意味着**?你只能從一個連接的套接字初始化NetworkStream(我的意思是在AcceptSocket返回時它已經連接套接字) – hmmftg 2012-07-27 01:41:43