Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème serveur socket

Sujet résolu
    30 juillet 2013 à 20:49:28

    Bonjour, j'ai un petit problème avec mon serveur socket qui crash, je ne sais pas vraiment pourquoi voilà l'erreur relevée par windows:

    Signature du problème :
      Nom d’événement de problème:    CLR20r3
      Signature du problème 01:    toonserv.exe
      Signature du problème 02:    1.0.0.0
      Signature du problème 03:    51f6cbe2
      Signature du problème 04:    System
      Signature du problème 05:    4.0.0.0
      Signature du problème 06:    4ba1dff4
      Signature du problème 07:    24aa
      Signature du problème 08:    17
      Signature du problème 09:    System.Net.Sockets.Socket
      Version du système:    6.1.7600.2.0.0.272.7
      Identificateur de paramètres régionaux:    1036
      Information supplémentaire n° 1:    0a9e
      Information supplémentaire n° 2:    0a9e372d3b4ad19135b953a78882e789
      Information supplémentaire n° 3:    0a9e
      Information supplémentaire n° 4:    0a9e372d3b4ad19135b953a78882e789

    Lire notre déclaration de confidentialité en ligne :
      http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x040c

    Si la déclaration de confidentialité en ligne n’est pas disponible, lisez la version hors connexion :
      C:\Windows\system32\fr-FR\erofflps.txt

    Et la dernière erreur enregistrer par le programme:

    [30/07/2013 20:41:18]:Client: Une connexion établie a été abandonnée par un logiciel de votre ordinateur hôte: System.Net.Sockets.SocketException (0x80004005): Une connexion établie a été abandonnée par un logiciel de votre ordinateur hôte
       à System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
       à ToonServ.Client.callback(IAsyncResult ar)

    Merci d'avance ...

    • Partager sur Facebook
    • Partager sur Twitter
      30 juillet 2013 à 21:59:09

      Bonjour,

      Montre le code...

      • Partager sur Facebook
      • Partager sur Twitter
      ** La doc, c'est comme le PQ: ça sert à se démerder tout seul **
        30 juillet 2013 à 23:40:38

        Client.cs:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Net;
        using System.Net.Sockets;
        using System.IO;
        using System.Collections;
        using System.Windows.Forms;
        
        namespace ToonServ
        {
            class Client
            {
        
                public string ID
                {
                    get;
                    private set;
                }
        
                public Perso perso;
        
                public int InRoom
                {
                    get;
                    set;
                }
        
                public bool NonDeco
                {
                    get;
                    set;
                }
        
                public bool InGame
                {
                    get;
                    set;
                }
        
                public IPEndPoint EndPoint
                {
                    get;
                    private set;
                }
        
                Socket sck;
                public Client(Socket accepted)
                {
                    sck = accepted;
                    ID = Guid.NewGuid().ToString();
                    EndPoint = (IPEndPoint)sck.RemoteEndPoint;
                    sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
                    InGame = false;
                    NonDeco = false;
                }
        
                void callback(IAsyncResult ar)
                {
                    try
                    {
                        sck.EndReceive(ar);
        
                        byte[] buf = new byte[8192];
        
                        int rec = sck.Receive(buf, buf.Length, 0);
        
                        if (rec < buf.Length)
                        {
                            Array.Resize<byte>(ref buf, rec);
                        }
        
                        if (Received != null)
                        {
                            Received(this, buf);
                        }
        
                        sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
        
                    }
                    catch (Exception ex)
                    {
                        if (!ex.Message.Contains("Nom du paramètre : length"))
                        {
                            string error = "[" + DateTime.Now.ToString() + "]:Client: " + ex.Message + ": " + ex.ToString();
                            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"error.txt", true))
                            {
                                file.WriteLine(error);
                            }
                        }
                        Close();
        
                        if (Disconnected != null)
                        {
                            Disconnected(this);
                        }
                    }
                }
        
                public void send(string msg)
                {
                    string msg_prep = msg;
                    Byte[] prep = System.Text.Encoding.ASCII.GetBytes(msg.ToCharArray());
                    sck.Send(prep, prep.Length, 0);
                }
        
        
                public void Close()
                {
                    sck.Close();
                    sck.Dispose();
                }
        
                public void update_id(string id)
                {
                    ID = id;
                }
        
                //public bool persoExist = false;
                public Perso persoClient(string sessid, int xpos, int ypos)
                {
                //persoExist = true;
                //int perso_random = new Random().Next(0, 1000);
                perso = new Perso(sessid, "", xpos, ypos, 0);
                return perso;
                }
        
                public delegate void ClientReceiveHandler(Client sender, byte[] data);
                public delegate void ClientDisconnecteHandler(Client sender);
        
                public event ClientReceiveHandler Received;
                public event ClientDisconnecteHandler Disconnected;
            }
        }
        
        • Partager sur Facebook
        • Partager sur Twitter
          31 juillet 2013 à 0:31:38

          Ligne 54/79: comment ton client est censé stocker les données qu'il reçoit si tu lui donne un buffer vide!

          De plus si tu donnes un buffer anonyme, comment fait tu pour traiter les informations reçues?

          • Partager sur Facebook
          • Partager sur Twitter
          ** La doc, c'est comme le PQ: ça sert à se démerder tout seul **
            31 juillet 2013 à 0:40:20

            La partie Main.cs:

            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;
            using System.Net.Sockets;
            
            namespace ToonServ
            {
                public partial class Main : Form
                {
                    string sessid = "";
                    Listener listener;
            
                    public Main()
                    {
                        InitializeComponent();
                        listener = new Listener(18000);
                        listener.SocketAccepted += new Listener.SocketAcceptedHandler(listener_SocketAccepted);
                        Load += new EventHandler(Main_Load);
                    }
            
                    void Main_Load(object sender, EventArgs e)
                    {
                        listener.Start();
                    }
            
                    void listener_SocketAccepted(System.Net.Sockets.Socket e)
                    {
                        Client client = new Client(e);
                        client.Received += new Client.ClientReceiveHandler(client_Received);
                        client.Disconnected += new Client.ClientDisconnecteHandler(client_Disconnected);
            
                        Invoke((MethodInvoker)delegate
                        {
                            ListViewItem i = new ListViewItem();
                            i.Text = client.EndPoint.ToString();
                            i.SubItems.Add(client.ID);
                            i.SubItems.Add("XX");
                            i.SubItems.Add("XX");
                            i.SubItems.Add("");
                            i.Tag = client;
                            lstClients.Items.Add(i);
                        });
                        this.Text = "Serveur | Connectés: " + lstClients.Items.Count;
                    }
            
                    private void SendDisconnect(int ID, int AppartID)
                    {
                        for (int i = 0; i < lstClients.Items.Count; i++)
                        {
                            Client client = lstClients.Items[i].Tag as Client;
                            if (client.InRoom == AppartID)
                            {
                                if (Convert.ToInt32(client.ID) != ID)
                                {
                                    client.send("LOST=" + ID + "\0");
                                }
                            }
                        }
                    }
            
                    private void SendToAll(string SENDINFO, int ID, int AppartID)
                    {
                        for (int i = 0; i < lstClients.Items.Count; i++)
                        {
                            Client client = lstClients.Items[i].Tag as Client;
                            if (client.InRoom == AppartID)
                            {
                                if (Convert.ToInt32(client.ID) != ID)
                                {
                                    client.send(SENDINFO + "\0");
                                }
                            }
                        }
                    }
            
                    void client_Disconnected(Client sender)
                    {
                        Invoke((MethodInvoker)delegate
                        {
            
                            for (int i = 0; i < lstClients.Items.Count; i++)
                            {
                                Client client = lstClients.Items[i].Tag as Client;
            
                                if (client.ID == sender.ID)
                                {
                                    if (client.InGame)
                                    {
                                        int AppartID = client.InRoom;
                                        int ID = Convert.ToInt32(client.ID);
                                        lstClients.Items.RemoveAt(i);
                                        DBConnect database = new DBConnect();
                                        database.UpdateOffline(ID);
                                        SendDisconnect(ID, AppartID);
                                    }
                                    else
                                    {
                                        lstClients.Items.RemoveAt(i);
                                    }
                                    this.Text = "Serveur | Connectés: " + lstClients.Items.Count;
                                    break;
                                }
                            }
                        });
                    }
            
                    void client_Received(Client sender, byte[] data)
                    {
                        Invoke((MethodInvoker)delegate
                        {
                            for (int i = 0; i < lstClients.Items.Count; i++)
                            {
                                Client client = lstClients.Items[i].Tag as Client;
            
                                if (client.ID == sender.ID)
                                {
                                    int inAppart = 0;
                                    Encoding.Default.GetString(data);
                                    lstClients.Items[i].SubItems[2].Text = Encoding.Default.GetString(data);
                                    lstClients.Items[i].SubItems[3].Text = DateTime.Now.ToString();
                                    if (Encoding.Default.GetString(data).Contains("<policy-file-request/>"))
                                    {
                                        client.send("<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\0");
                                    }
            						//Reste du code
                                    break;
                                }
            
                            }
                        });
                    }
            
            
                }
            }
            



            Le Listener.cs:

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Net;
            using System.Net.Sockets;
            using System.Windows.Forms;
            namespace ToonServ
            {
                class Listener
                {
                    Socket s;
            
                    public bool Listening
                    {
                        get;
                        private set;
                    }
            
                    public int Port
                    {
                        get;
                        private set;
                    }
            
                    public Listener(int port)
                    {
                        Port = port;
                        s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    }
            
                    public void Start()
                    {
                        if (Listening)
                            return;
            
                        s.Bind(new IPEndPoint(0, Port));
                        s.Listen(0);
                        s.BeginAccept(callback, null);
                        Listening = true;
                    }
            
                    public void Stop()
                    {
                        if (!Listening)
                            return;
            
                        s.Close();
                        s.Dispose();
                        s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    }
            
                    void callback(IAsyncResult ar)
                    {
                        try
                        {
                            Socket s = this.s.EndAccept(ar);
                            if (SocketAccepted != null)
                            {
                                SocketAccepted(s);
                            }
            
                            this.s.BeginAccept(callback, null);
                        }
                        catch(Exception ex)
                        {
                            string error = "[" + DateTime.Now.ToString() + "]:Listener: " + ex.Message + ": " + ex.ToString();
                            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"error.txt", true))
                            {
                                file.WriteLine(error);
                            }  
                        }
                    }
                    public delegate void SocketAcceptedHandler(Socket e);
                    public event SocketAcceptedHandler SocketAccepted;
                }
            }
            




            -
            Edité par MehdyZ 31 juillet 2013 à 0:41:20

            • Partager sur Facebook
            • Partager sur Twitter
              31 juillet 2013 à 15:22:17

              Problème résolut pour le moment, j'ai refais un nouveau serveur, pas de problèmes pour l'instant :)
              • Partager sur Facebook
              • Partager sur Twitter

              Problème serveur socket

              × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
              × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
              • Editeur
              • Markdown