سلام دوستان این موضوعی که من واسش تاپیک زدم تکراریه و مثلش زیاد هست داخل سایت که هیچ کدوم به تنیجه نریسده یا فقط سوال بوده یا لینک، من از دیروز ساعت 12 ظهر تا 1 شب سرچ کردم ولی به نتیجه نریسیدم
لطفا 1 پاسخ کامل بدید مرسی

من 1 برنامه تحت شبکه درست کردم که چند کاربر هم زمان باهاش کار میکنن که برای اینکه لیست نمایش داده ها همون گرید ویو ،با توجه به هر تغییر در دیتابیس توسط کابران دیگر به روز بشه و تغییراتو نمایش بده از سوکت استفاده کردم به طوری که بعد از هر تراکنش کلاینتی که تراکنش را انجام میده 1 را به سرور می فرسته و سرور هم 1 رو به همه ی کلاینتها می فرسته ، هر کلاینت که 1 را دریافت کرد گرید ویو رو مجدد از بانک بایند میکنه
حالا من تا کانکت کردن چند کلاینت به سرور پیش رفتم و همه کلاینها هم به سرور پیام میدن ولی نمیدونم سرور چه طور به همهی کلاینتهایی که وصل شدند پیام بده
من این برنامرو اول به شکل چت نوشتم تا از کارکردش مطمئن شم بعد به برنامه که بالا گفتم اظافه میکنم
کد سرور و کلاینت رو میزارم تا دوستان راحت تر رهنمایی کنن
نوع برنامه ویدوز فرمه

کد سرور
public partial class Form2 : Form
{
TcpClient clientSocket;
string clNo;
public Form2()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}



private void Form2_Load(object sender, EventArgs e)
{

Thread ctThread = new Thread(connect);
ctThread.Start();
}
private void connect()
{
TcpListener serverSocket = new TcpListener(9050);
TcpClient clientSocket = default(TcpClient);
int counter = 0;

serverSocket.Start();
MessageBox.Show(" >> " + "Server Started");

counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
string con = clientSocket.Client.AddressFamily.ToString();
listBox1.Items.Add(" >> " + "Client No:" + Convert.ToString(counter) + " started!");

startClient(clientSocket, Convert.ToString(counter));
}

clientSocket.Close();
serverSocket.Stop();
listBox1.Items.Add(" >> " + "exit");
}




public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
public void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;

while ((true))
{
try
{
dataFromClient = null;
serverResponse = null;
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
// dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Form2 frm = new Form2();

listBox1.Items.Add(" >> " + "From client-" + clNo +" : "+ dataFromClient);
if (dataFromClient.Substring(0,1) == "1")
{
rCount = Convert.ToString(requestCount);
serverResponse = "1";
//"Server to clinet(" + clNo + ") " + rCount;

sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
}
// listBox1.Items.Add(" >> " + serverResponse);
}
catch (Exception ex)
{
MessageBox.Show(" >> " + ex.ToString());
}
}
}

private void button1_Click(object sender, EventArgs e)
{

}

}


کد سمت کلاینت

public partial class Form1 : Form
{
private delegate void AddListBoxItemDelegate(object item);
static byte[] data = new byte[2048];
static string input, stringData;
static IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
static Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static int recv;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}

private void Form1_Load(object sender, EventArgs e)
{
Thread nakh1 = new Thread(new ThreadStart(server_cncnt));
nakh1.Start();
}

private void button1_Click(object sender, EventArgs e)
{
input = textBox1.Text;
server.Send(Encoding.ASCII.GetBytes(input));
listBox1.Items.Add("Client :" + input);
textBox1.Text = "";

}
public void server_cncnt()
{
try
{
server.Connect(ipep);
//server.Send(Encoding.ASCII.GetBytes("H! Server"));
}
catch (SocketException)
{
MessageBox.Show("unable connect to server", "wairning");
}
while (true)
{

recv = server.Receive(data);
if (Encoding.ASCII.GetString(data) == ".|.")
{
server.Shutdown(SocketShutdown.Receive);
server.Close();

}
listBox1.Items.Add("Server :"+Encoding.ASCII.GetString(data, 0, recv));
//AddListBoxItem(Encoding.ASCII.GetString(data, 0, recv));
}
}
private void AddListBoxItem(object item)
{
if (this.listBox1.InvokeRequired)
{ // This is a worker thread so delegate the task.
this.listBox1.Invoke(new AddListBoxItemDelegate(this.AddListBoxItem), item);
}
else
{ // This is the UI thread so perform the task.
this.listBox1.Items.Add(item);
}
}
}