PDA

View Full Version : سوال: ارسال پیغام از طرف سرور به تمامی کلاینت ها (چت)



No_Name
جمعه 25 آذر 1390, 02:05 صبح
سلام

من یه برنامه چت ساختم ولی تو یه قستمی بد جور هنگم :عصبانی++:

تا اینجاش پیش رفتم که کلاینت ها به سرور وصل میشن و میتونن به سرور پیغام ارسال کنن

ولی نمیدونم چطور میشه که سرور تمامی کلاینت ها رو یک جا جمع کنه و سپس پیغامی که مثلآ از طرف کلاینت A میاد رو به همه ی کلاینت ها ارسال کنه

فقط فقط فقط توی همین قسمتش موندم !:افسرده:

خودم اومدم اینکار رو کردم و این کد رو نوشتم ولی جواب نداد:

1- یه سوکت تعریف کردم:

public Socket client;


2- برای گرفتن تمامی کلاینت ها از این استفاده کردم:

private static Dictionary<int, Socket> allSocket = new Dictionary<int, Socket>();

3- کلاینت هایی که به سرور وصل میشن رو توی allsocket اضافه کردم:

private static int rand = 0; int id = 0; rand++; id = rand; allSocket.Add(rand, client);


4- خب الان هم برای ارسال پیغام به تک تکه کلاینت هایی که توی allsocket اضافه شدن رو از این کد استفاده کردم:

for (int i = 0; i < id ; i++) { allSocket[id].Send(Encoding.Unicode.GetBytes("Test Send Msg")); }


کسی میتونه بگه کجاش مشکل داره ؟ میتونین واسم درستش کنین ؟

چند هفتس فقط توی همین قسمتش گیر کردم

نمیخوام لینک سورس مورس بهم بدین، زیاد سورس باز کردم و هر کدام هم به یه روش نوشته بودن :|

یه روش از همه کاربردی تر رو میخوام بهم بگین

----> فقط در همین قستمی که مشکل دارم رو توضیح بدین

تشکر

No_Name
جمعه 25 آذر 1390, 15:20 عصر
انگارآ اینطوری یکم سخت توضیح دادم

این هم فول کد:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;

namespace chat_Server
{
public partial class Form1 : Form
{
Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ServerIP = new IPEndPoint(IPAddress.Any, 6634);

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}

private void btnStart_Click(object sender, EventArgs e)
{
ServerSocket.Bind(ServerIP);
ServerSocket.Listen(10);
Thread StartT = new Thread(Start);
StartT.Start();
MessageBox.Show("Server Started...");
}

void Start()
{
while (true)
{
ConnectionThread newconnection = new ConnectionThread();
try
{
newconnection.client = ServerSocket.Accept();
}

catch
{
break;
}

Thread newThread = new Thread(new ThreadStart(newconnection.HandleConnection));
newThread.Start();
}
}
class ConnectionThread
{
private static Dictionary<int, Socket> allSocket = new Dictionary<int, Socket>();
string Spliter = "~$%^&";
string ReverseSpliter = "&^%$~";
public Socket client;
int BufferSize = 100000;
private static int rand = 0;
int id = 0;

public void HandleConnection()
{

int recv;
byte[] data = new byte[BufferSize];
string textRecieve;
string StringData;
rand++;
id = rand;
allSocket.Add(rand, client);
recv = client.Receive(data);
textRecieve = Encoding.Unicode.GetString(data, 0, recv);

string[] txtR = GetDataPart(textRecieve);

if (txtR[0] == "hi")
{
MessageBox.Show("Show Hi");
}
else if (txtR[0] == "bye")
{
MessageBox.Show("Show Bye");
}

try
{
while (true)
{
data = new byte[BufferSize];
recv = client.Receive(data);
if (recv == 0)
{
allSocket.Remove(id);
client.Close();
break;
}
StringData = Encoding.Unicode.GetString(data, 0, recv);
txtR = GetDataPart(StringData);

if (txtR[0] == "Send")
{
MessageBox.Show("Client type --> Send");
}
else
SendText(StringData);

}
}
catch
{
allSocket.Remove(id);
}
}
void SendText(string Data)
{
string[] txtR = GetDataPart(Data);
for (int i = 0; i < id; i++)
{

allSocket[id].Send(Encoding.Unicode.GetBytes(Data));
}
}

string[] GetDataPart(string StringData)
{
string[] txtR = new string[10];
int Index = 0;
int LastIndex = 0;
int j = 0;
for (int i = 0; i < 10; i++)
txtR[i] = "";
while (Index != -1)
{
Index = StringData.IndexOf(Spliter, LastIndex);
if (Index != -1)
{
if ((Index + 5) == StringData.Length || ((Index + 10) < StringData.Length || StringData.Substring(Index + 5, 5) != ReverseSpliter))
{
txtR[j] += StringData.Substring(LastIndex, Index - LastIndex);
j++;
LastIndex = Index + 5;
}
else if ((Index + 10) < StringData.Length && StringData.Substring(Index + 5, 5) == ReverseSpliter)
{
txtR[j] += StringData.Substring(LastIndex, Index - LastIndex + 5);
LastIndex = Index + 10;
}
}
}
return txtR;
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ServerSocket.Close();
Application.Exit();
Application.ExitThread();
}

private void btnStop_Click(object sender, EventArgs e)
{
ServerSocket.Close();
}
}
}

امید وارم الان بتونین مشکلم رو حل کنین

فقط میخوام پیغام به همه کلاینت ها بفرستم ! مشکلم تو همینه