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
| public void Start()
{
this._Thread = new Thread(new ThreadStart(this.StartOnThread));
this._Thread.Start();
}
public void StartOnThread()
{
while (true)
{
try
{
List<Socket> read = new List<Socket>();
List<Socket> write = new List<Socket>();
for (int i = 0; i < this._clients.Count; i++)
{
read.Add(this._clients.ElementAt(i).Key);
if (this._clients.ElementAt(i).Value.Size > 0)
write.Add(this._clients.ElementAt(i).Key);
}
Socket.Select(read, write, null, 5);
if (read.Count > 0)
{
for (int i = 0; i < read.Count; i++)
{
try
{
if (this._clients[read[i]].IsServer)
{
this.Accept();
}
else
{
this.Read(read[i]);
}
}
catch (Exception)
{
read[i].Close();
}
}
}
if (write.Count > 0)
{
for (int i = 0; i < write.Count; i++)
{
try
{
this.Write(write[i]);
}
catch (Exception)
{
}
}
}
this.ClearDisconnectSocket();
}
catch (Exception E)
{
Log.Instance.Write(E.Message);
}
}
} |