problème socket synchrone
Bonjour tout le monde,
C'est mon premier post ici :)
Bref, je suis débutant en c# et j'ai un problème concernant les sockets synchrones, en fait je réalise un projet qui consiste à faire une communication entre Client/serveur, donc j'ai codé un petit serveur , tout marche très bien la connexion avec le client et la réception et l'envoie du premier message sauf que ma socket se bloque à la réception des messages suivants et une exception
Citation:
System.ObjectDisposedException: impossible d'accéder à un objet supprimé
voila mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace BD1.metier
{
class SynchronousSocketListener
{
public static byte[] bytes = new Byte[655360];
public static string data = null;
private static Socket handler;
public static void StartListening()
{
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 8002);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
handler = listener.Accept();
// An incoming connection needs to be processed.
while (true)
{
bytes = new byte[655360];
data = null;
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
VerifMsg.Verif(data);
Console.WriteLine("Text received : {0}", data);
SendLogon();// à la fin de cette methode:
// byte[] msg = Encoding.ASCII.GetBytes(sb.ToString());
// handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
} |
J'ai besoin d'une aide svp :roll:
Merci.