简体   繁体   中英

A first chance exception

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM