MORPG服务器没有断开连接

时间:2008-12-11 22:20:07

标签: c# disconnect

好的,希望简单的问题。 我已经开始为一个简单的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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

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

豫ICP备18024241号-1