Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:10:46+00:00 2026-05-23T05:10:46+00:00

I was studying sockets in C#, and after I managed coding a basic two-person

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:10:47+00:00Added an answer on May 23, 2026 at 5:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After studying ruby for about a month, I decided to move on to Sinatra.
After studying TCP/UDP difference all week, I just can't decide which to use. I
I'm studying Unix sockets programming. I made a time server that sends raw time
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
I am studying COM so there're some basic questions puzzling me... I know that
After studying the docs for a while, I came up with my first jQuery
Studying JSR-299, I read section 5.1 of the Weld reference which explains how scopes
I am studying asynchronous C# Sockets at the moment and i have noticed something
After studying the Perl require docs and other links like this on Stackoverflow I'm
I'm currently studying for a discrete mathematics test in which we are learning Chomsky's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.