Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A first chance exception

Tags:

c#

sockets

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.

like image 830
Jong Avatar asked Jun 14 '11 19:06

Jong


People also ask

What is first chance exception?

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.

What is first chance and second chance exception?

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".

How do I enable exceptions in Visual Studio?

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.


1 Answers

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.

like image 108
SLaks Avatar answered Sep 19 '22 12:09

SLaks