PDA

View Full Version : کمک در مورد برنامه ی چند نخی



ایمان اختیاری
پنج شنبه 12 شهریور 1388, 11:50 صبح
سلام به دوستان عزيز
بنده توي يه برنامه به مشکل خوردم گفتم از دوستان کمک بگيرم
يه برنامه به زبان #c نوشتم براي مديريت يه سري کلاينت ...
ولي اينجا مشکلي که وجود داره نمي دونم کدوم نخ به کدوم کلاينت وصل مي شه و در کل مديريت اين نخا بدجور گره ام زده ..
حتي دست به دامان MSDN هم شدم ولي چيزي دستگيرم نشد
ولي اميدوار هستم که بچه هاي خودمون مي تونن اين مسئله رو حل کنن...
منتظر و متشکرم
لينک سوال در msdn
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/35007847-0b9e-4c7f-a013-6f06c8e0f5f4

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.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace sample_pool
{

public partial class Form1 : Form
{
//private Socket connection;
private Thread readThread;

private TcpListener client;



public Form1()
{
InitializeComponent();
readThread = new Thread(new ThreadStart(Runserver));
readThread.Start();

}
public void Runserver()
{
client = new TcpListener(5000);
client.Start();
//Console.WriteLine("Waiting for clients...");
while (true)
{
while (!client.Pending())
{
Thread.Sleep(1000);
}
ConnectionThread newconnection = new ConnectionThread(client);
newconnection.threadListener = this.client;
ThreadPool.QueueUserWorkItem(new WaitCallback(newconnection.HandleConnection));

}
}

}

class ConnectionThread
{
private BinaryWriter writer;
private BinaryReader reader;
private NetworkStream sockstream;
public TcpListener threadListener;
private Socket connection;
//private Thread readThread;

private TcpListener client;
//private TcpListener listenr;

//private static int connections = 0;
public ConnectionThread(TcpListener c1)
{
this.client = c1;
}
public void HandleConnection(object state)
{

string thereaply;
while (true)
{
connection = client.AcceptSocket();
sockstream = new NetworkStream(connection);
writer = new BinaryWriter(sockstream);
reader = new BinaryReader(sockstream);
do
{
try
{
thereaply = reader.ReadString();
Console.Write(thereaply+"\n");
}
catch (Exception)
{
break;
}
} while (thereaply != "close" && connection.Connected);
connection.Close();
writer.Close();
reader.Close();
sockstream.Close();

Console.Write("i am closed");



}
//int recv;
//byte[] data = new byte[1024];
//TcpClient client = threadListener.AcceptTcpClient();
//NetworkStream ns = client.GetStream();
//connections++;
//Console.WriteLine("New client accepted: {0} active connections", connections);
//string welcome = "Welcome to my test server";
//data = Encoding.ASCII.GetBytes(welcome);

//ns.Write(data, 0, data.Length);
//while (true)
//{
// data = new byte[1024];
// recv = ns.Read(data, 0, data.Length);
// if (recv == 0)
// break;

//ns.Write(data, 0, recv);
// Console.Write(recv);
//}
// ns.Close();
//client.Close();
//connections--;
//Console.WriteLine("Client disconnected: {0} active connections", connections);

}
}


}