2008-12-11 9 views
2

Ok, problème simple j'espère. J'ai commencé à faire un simple serveur pour un simple jeu MORPG en faisant, le client connecté, puis déconnecte, simple, le serveur attrape le nouveau client, mais n'attrape pas quand il se déconnecte, quel est le problème? Je souhaite que ce soit quelque chose d'étonnamment évident.Le serveur MORPG n'attrape pas la déconnexion

Voici mon code serveur:

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; 
     } 
    } 
} 

Répondre

2

Si vous faites un appel de blocage à lire sur un flux réseau et renvoie 0 octets la longueur qu'il lu cela signifie que votre client est déconnecté. Vous n'obtiendrez pas d'exception tant que vous n'essayez pas d'écrire sur cette connexion. C'est ma meilleure estimation quant à ce qui se passe.

Questions connexes