t0fx
Messages postés22Date d'inscriptionmercredi 24 octobre 2007StatutMembreDernière intervention 1 décembre 2009
-
30 nov. 2009 à 22:03
t0fx
Messages postés22Date d'inscriptionmercredi 24 octobre 2007StatutMembreDernière intervention 1 décembre 2009
-
1 déc. 2009 à 15:02
Bonjour à tous ! J'essaye de créer une application de Chat multi postes, j'en suis au tout début et j'aurai besoin d'un peu d'aide pour un bout de code !
J'ai plusieurs ordinateurs qui se connectent à moi et j'aimerai envoyer un message général à tous les connectés. Voilà le code source de l'application :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace LanChat
{
public partial class Form1 : Form
{
public int Computers = 0;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
public void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Listen")
{
TcpListener = new TcpListener(IPAddress.Any, Convert.ToInt32(textPort.Text));
ListenThread = new Thread(ListenForClients);
ListenThread.Start();
button1.Text = "Stop";
}
else
{
button1.Text = "Listen";
TcpListener.Stop();
ListenThread.Abort();
ListenThread.Interrupt();
textOut.Clear();
}
}
public void Form1_Load(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Welcome!";
notifyIcon1.BalloonTipText = "LanChat Loaded!";
notifyIcon1.ShowBalloonTip(2000);
}
public TcpListener TcpListener;
public Thread ListenThread;
public void ListenForClients()
{
TcpListener.Start();
while (true)
{
//Attente de connexions
var client = TcpListener.AcceptTcpClient();
//création d'un thread pour gérer les communications
//avec chaque client
var clientThread = new Thread(HandleClientComm);
clientThread.Start(client);
}
}
public void HandleClientComm(object client)
{
var tcpClient = (TcpClient)client;
var clientStream = tcpClient.GetStream();
Users++;
lblusers.Text = "( Status : " + Computers + " )";
var message = new byte[4096];
while (true)
{
int bytesRead;
ASCIIEncoding encoder;
try
{
encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes("Ping");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
//attente de reception du pong
bytesRead = clientStream.Read(message, 0, 4096);
//Reception du message : envoi de la réponse auto
byte[] buffer1 = encoder.GetBytes("pause");
clientStream.Write(buffer1, 0, buffer1.Length);
clientStream.Flush();
}
catch
{
//socket error
//textOut.AppendText(client + " disconnected \r\n");
Users--;
lblusers.Text = "( Status : " + Computers + " )";
break;
}
if (bytesRead == 0)
{
//client déconnecté
//textOut.AppendText(client + " disconnected \r\n");
Users--;
lblusers.Text = "( Status : " + Computers + " )";
break;
}
//reception message ok, connexion réussie
encoder = new ASCIIEncoding();
textOut.AppendText(encoder.GetString(message, 0, bytesRead) + "Connected \r\n");
}
tcpClient.Close();
}
public void SendMessageToAll(string message)
{
foreach (?? in ??)
{
byte[] buffer1 = encoder.GetBytes(message);
clientStream.Write(buffer1, 0, buffer1.Length);
clientStream.Flush();
}
}
}
}
La ou je bloque c'est :
public void SendMessageToAll(string message)
{
foreach (?? in ??)
{
byte[] buffer1 = encoder.GetBytes(message);
clientStream.Write(buffer1, 0, buffer1.Length);
clientStream.Flush();
}
}
Si quelqu'un à un début de réponse ça serait sympa !
Kenavo !