I was studying sockets in C#, and after I managed coding a basic two-person chat, I decided to move on to multiplayer chat, which has a server and X clients.
Now, there is a problem popping up even when there is only one client connected. As soon as the client connects, both the server and client get a message, "Another client connected" or "Connected to server". The second they both clicked OK, the client's program crashes, follows by the server one (I will deal with disconnecting later, I want to get it working first). And as you can guess from the title, the only thing I get is "A first chance exception" which even after googling or reading here, I couldn't stop it from coming, nor understanding WHY it was coming.
Here are the two lines from the debugger output:
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
The program '[6808] Chat - sockets.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
Try-catch didn't work when trying to catch this exception. And as I said before, the program crashes even on debugging mode, showing no error.
And here is the code:
Client connect callback:
private void ClientConnectCallback(IAsyncResult res)
{
serverSocket.EndConnect(res);
MessageBox.Show("Server connected.");
serverSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ClientRecieveCallback), null);
}
buffer is a local variable, a byte array sized as 1000. serverSocket is a socket on which we use the 'BeginConnect' method.
And this is the server accept client callback:
private void ServerAcceptCallback(IAsyncResult res)
{
//Recieving the socket.
Socket tempSocket = localHost.EndAccept(res);
//Creating a new buffer for it.
byte[] tempBuffer = new byte[1000];
//Adding the socket to the array.
Socket[] tempArray = clients;
clients = new Socket[tempArray.Length + 1];
Array.Copy(tempArray, clients, tempArray.Length);
clients[clients.Length - 1] = tempSocket;
//Adding the buffer to the list.
buffers.Add(tempBuffer);
MessageBox.Show("Another client connected");
//Begin receive data.
tempSocket.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, new AsyncCallback(ServerRecieveCallback), null);
localHost.BeginAccept(new AsyncCallback(ServerAcceptCallback), null);
numOfPpl++;
ServerSend("~~~NumOfPpl:" + numOfPpl);
}
localHost
is a socket which we bind to the port, and to ANY address, and then we call it to 'Listen(0)'. tempBuffer
is a new byte array which we create for using as a buffer for this connection only. clients
is a socket array, containing the clients of the server. buffers
is a List of the buffers of the clients.
numOfPpl is the number of people in the current conversation, and by calling ServerSend with the text "~~~NumOfPpl:", the clients receive the next number as the number of people and not a message, and change it respectively in their computer.
I hope I made myself clear, my first question in this site.
Actually, even a piece of information that will help me to get myself further (because I am left with nothing to try now) will help a lot.
first-chance exception (plural first-chance exceptions) (computing) An exception (error condition) when handled by a debugger, such that the programmer has the first chance to study it; such an exception would otherwise proceed to a handler or (in its absence) crash the program.
In short, First chance exception gives the debugger a first chance to inspect the exception and application state before the application handles the exception. You can stop the debugger at this point (it' usually a setting like "break into debugger when exception is created".
You can also add or delete exceptions. With a solution open in Visual Studio, use Debug > Windows > Exception Settings to open the Exception Settings window. Provide handlers that respond to the most important exceptions.
You probably got an exception inside a socket I/O callback, which isn't in your catch
block.
In Visual Studio, click Debug, Exceptions, and then check all of the checkboxes, to tell Visual Studio to enter the debugger as soon as any exception is thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With