gpt4 book ai didi

c# - MORPG 服务器未捕获断开连接

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:16 25 4
gpt4 key购买 nike

好的,希望是简单的问题。我已经开始为我正在制作的一个简单的 MORPG 游戏制作一个简单的服务器,客户端连接,然后断开连接,简单,服务器捕获新客户端,但在断开连接时不捕获,怎么了?

我希望它是一些非常明显的东西。

这是我的服务器代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ChaWilPeiBle
{
public partial class MainForm : Form
{
public bool ServerON = false;
public TcpListener ServerL;
public int ServerLoad = 2;
public string ServerINFtxt = "";
public ASCIIEncoding AE = new ASCIIEncoding();

public MainForm()
{
InitializeComponent();
}

private void LBL_SS_Click(object sender, EventArgs e)
{

}

private void MainForm_Load(object sender, EventArgs e)
{
LBL_SS.ForeColor = Color.Red;
}

public void AddH(string S)
{
ServerINFtxt += S;
}

public void Server()
{
try
{
ServerL = new TcpListener(47);
ServerL.Start();
AddH("\nServer Started On Port 47");
}
catch(Exception e)
{
MessageBox.Show("Error starting Server.__________________\n"+e.ToString());
}
while (true)
{
TcpClient TCPC;
TCPC = ServerL.AcceptTcpClient();
if (ServerLoad < 100)
{
Thread T = new Thread(HandleClient);
T.Start((object)TCPC);
}
else
{
byte[] BYTES = AE.GetBytes("NoAccess:Full");
NetworkStream NS = TCPC.GetStream();
NS.Write(BYTES, 0, BYTES.Length);
}
}
}

public void HandleClient(object C)
{
ServerLoad++;
TcpClient Client = (TcpClient)C;
NetworkStream NS = Client.GetStream();
AddH("Client Connected.\nServer Load: " + ServerLoad);
Thread T = new Thread(ReadClient);
T.Start(C);
try
{
while (true) { NS.Write(AE.GetBytes(""), 0, AE.GetBytes("").Length); }
}
catch { }
ServerLoad--;
AddH("Client Disconnected.\nServer Load: " + ServerLoad);
}

public void ReadClient(object C)
{
//TcpClient Client = (TcpClient)C;
}

private void startStopToolStripMenuItem_Click(object sender, EventArgs e)
{
LBL_SS.Text = "Server On";
LBL_SS.ForeColor = Color.Green;
if (ServerON == false)
{
Thread T = new Thread(Server);
T.Start();
}
ServerON = true;
}

private void Update_Tick(object sender, EventArgs e)
{
ServerINF.Text = ServerINFtxt;
PB_SL.Value = ServerLoad;
}
}
}

最佳答案

如果您进行阻塞调用以读取网络流,并且它返回 0 字节作为它读取的长度,则意味着您的客户端已断开连接。在您尝试写入此连接之前,您不会得到异常。这是我对正在发生的事情的最佳猜测。

关于c# - MORPG 服务器未捕获断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/361212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com