C#: socket closing if user user exists
Posted
by
corvallo
on Stack Overflow
See other posts from Stack Overflow
or by corvallo
Published on 2012-12-18T17:02:24Z
Indexed on
2012/12/18
17:02 UTC
Read the original article
Hit count: 222
Hi to everyone I'm trying to create a server/client application for a school project. This is the scenario: a server on a given port, multiple user connected, each user has it's own username. Now I want to check if a user that try to connect to the user use a valid username, for example if a user with username A it's already connected a new user that want to connect cannot use the username A. If this happen the server answer to the new client with an error code. This is the code for this part
private void Receive() { while (true) { byte[] buffer = new byte[64]; socket.Receive(buffer); string received = Encoding.Default.GetString(buffer); if (received.IndexOf("!error") != -1) { string[] mySplit = received.Split(':'); string errorCode = mySplit[1].Trim((char)0); if (errorCode == "user exists") { richTextBox1.AppendText("Your connection was refused by server, because there's already another user connected with the username you choose"); socket.Disconnect(true);
connectBtn.Enabled = true;
}
}
}
}
But when I try to do this the program crash and visual studio said that there's an invalid cross-thread operation on richTextBox1.
Any ideas. Thank you in advance.
© Stack Overflow or respective owner