Hello,
I'm writing a Client-Server application using sockets (asynchronous).
There is a Server (Master) which accepts incoming connections, and
Client (Slave).
Afetr establishing connections with all Slaves I wanna hit a button
"Automatic" , then everything must be reorganised, there is an
ellection for a new Master.
Everything is all right when I'm connecting manually (when I hit a
button connect), but after hitting this button (Automatic) it looks
like there is to many connections at the same time :/
When a Slave is connecting he's sending a Login command, Master is
responding to him with a unique number for him (his name). It looks
like all of them are sent, but they are not reaching the slaves.
I'm new in C# and I think that I'm doing something wrong. Maybe there
is some special way for mantaining many request at the same time.
Here are the most important fragments of my code:
Form1.cs:
private void btnAutomatic_Cl ick(object sender, EventArgs e)
{
//We're flushing the info about CPU speed's of each
connected slave
//We're sending the Ellection Command to each connected
Slave
Data msgToSend = new Data();
byte[] message;
msgToSend.cmdCo mmand = Command.Ellecti on;
message = msgToSend.ToByt e();
lock (_master.__clie ntList)
{
foreach (ClientInfo client in _master.__clien tList)
{
if (client.type == 1)
{
client.cpuSpeed = 0;
client.socket.B eginSend(messag e, 0,
message.Length, SocketFlags.Non e, new AsyncCallback(_ master.OnSend),
client.socket);
}
}
}
}
- Master.cs:
private void OnReceive(IAsyn cResult ar)
{
lock (__clientList)
{
try
{
Socket clientSocket = (Socket)ar.Asyn cState;
clientSocket.En dReceive(ar);
//Transform the array of bytes received from the
user into an intelligent form of object Data
Data msgReceived = new Data(byteData);
//We will send this object in response the users
request
Data msgToSend = new Data();
byte[] message;
msgToSend.cmdCo mmand = msgReceived.cmd Command;
msgToSend.strNa me = msgReceived.str Name;
mainForm.messag eReceived(msgRe ceived.strName,
Convert.ToStrin g(msgReceived.c mdCommand), msgReceived.str Message);
switch (msgReceived.cm dCommand)
{
case Command.Login:
//When a user logs in to the server then
we're adding him to our list of clients
ClientInfo clientInfo = new ClientInfo();
clientInfo.sock et = clientSocket;
//Set an unique name (number) for every
connected client
if (_clientList.Co unt == 0)
clientInfo.nr = 0;
else
{
foreach (ClientInfo client in
__clientList)
{
if (clientInfo.nr <=
client.nr)
clientInfo.nr = client.nr
+ 1;
}
}
clientInfo.strN ame = msgReceived.str Name +
clientInfo.nr;
if (msgReceived.st rName == "slave")
clientInfo.type = 1;
else
clientInfo.type = 2;
_clientList.Add (clientInfo);
//Set the text of the message which will
be send to user who just logged into our server
msgToSend.strMe ssage = "Your name is: ";
msgToSend.strNa me =
clientInfo.nr.T oString();
mainForm.messag eReceived(msgTo Send.strName,
Convert.ToStrin g(msgToSend.cmd Command), "He's name is: ");
message = msgToSend.ToByt e();
clientInfo.sock et.BeginSend(me ssage, 0,
message.Length, SocketFlags.Non e, new AsyncCallback(O nSend),
clientInfo.sock et);
mainForm.newCli entConnected(cl ientInfo.strNam e);
break;
- Slave.cs:
private void OnReceive(IAsyn cResult ar)
{
try
{
clientSocket.En dReceive(ar);
Data msgReceived = new Data(byteData);
Data msgToSend = new Data();
byte[] b;
mainForm.messag eReceived(msgRe ceived.strName,
Convert.ToStrin g(msgReceived.c mdCommand), msgReceived.str Message);
//Process the message received
switch (msgReceived.cm dCommand)
{
case Command.Login:
nr = Convert.ToInt32 (msgReceived.st rName);
strName = "slave" + nr.ToString();
mainForm.setTit le(strName);
break;
Thank you very much for all issues and ideas.
P.S. Sorry for my english.
Lukas