views:

120

answers:

1

Ok, simple problem hopefully. I have started to make a simple server for a simple MORPG game im making, the client connected, and then disconnects, simple, the server catches the new client, but doesn't catch when it disconnects, whats wrong?

Im hoping its something stupidly obvious.

Here's my server code:

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  A: 

If you make a blocking call to read on a network stream and it returns 0 bytes as the length that it read it means that your client has disconnected. You will not get an exception until you try write to this connection. That's my best guess as to what is happening.

Steven Behnke