Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 第一次机会的例外_C#_Sockets - Fatal编程技术网

C# 第一次机会的例外

C# 第一次机会的例外,c#,sockets,C#,Sockets,我在研究C#中的套接字,在编写了一个基本的两人聊天之后,我决定转向多人聊天,它有一个服务器和X客户端 现在,即使只连接了一个客户端,也会出现一个问题。一旦客户端连接,服务器和客户端都会收到一条消息,“另一个客户端已连接”或“已连接到服务器”。第二次他们都单击OK,客户端程序崩溃,接着是服务器程序(稍后我将处理断开连接的问题,我想先让它工作)。你可以从标题中猜到,我唯一得到的是“第一次机会例外”,即使在谷歌搜索或阅读了这里,我也无法阻止它的出现,也无法理解它为什么会出现 以下是调试器输出的两行代码

我在研究C#中的套接字,在编写了一个基本的两人聊天之后,我决定转向多人聊天,它有一个服务器和X客户端

现在,即使只连接了一个客户端,也会出现一个问题。一旦客户端连接,服务器和客户端都会收到一条消息,“另一个客户端已连接”或“已连接到服务器”。第二次他们都单击OK,客户端程序崩溃,接着是服务器程序(稍后我将处理断开连接的问题,我想先让它工作)。你可以从标题中猜到,我唯一得到的是“第一次机会例外”,即使在谷歌搜索或阅读了这里,我也无法阻止它的出现,也无法理解它为什么会出现

以下是调试器输出的两行代码:

mscorlib.dll中发生类型为“System.FormatException”的第一次意外异常

程序“[6808]Chat-sockets.vshost.exe:Managed(v4.0.30319)”已退出,代码为0(0x0)

尝试捕获此异常时,“尝试捕获”无效。正如我之前所说,程序甚至在调试模式下崩溃,没有显示错误

代码如下:

客户端连接回调:

private void ClientConnectCallback(IAsyncResult res)
{
    serverSocket.EndConnect(res);
    MessageBox.Show("Server connected.");
    serverSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ClientRecieveCallback), null);
}
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);
}
缓冲区是一个局部变量,大小为1000的字节数组。serverSocket是一个使用“BeginConnect”方法的套接字

这是服务器接受客户端回调:

private void ClientConnectCallback(IAsyncResult res)
{
    serverSocket.EndConnect(res);
    MessageBox.Show("Server connected.");
    serverSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ClientRecieveCallback), null);
}
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
是一个套接字,我们将它绑定到端口和任何地址,然后调用它来“侦听(0)”
tempBuffer
是我们创建的一个新字节数组,仅用作此连接的缓冲区
clients
是一个套接字数组,包含服务器的客户端<代码>缓冲区是客户端缓冲区的列表

numOfPpl是当前对话中的人数,通过使用文本“~~~numOfPpl:”调用ServerSend,客户端将接收下一个数字作为人数而不是消息,并分别在其计算机中更改

我希望我说清楚,这是我在这个网站上的第一个问题


实际上,即使是一条有助于我进一步发展的信息(因为我现在没有什么可尝试的了)也会有很大帮助。

您可能在套接字回调中遇到异常,而该异常不在您的
catch
块中


在Visual Studio中,单击“调试”、“异常”,然后选中所有复选框,告诉Visual Studio在抛出任何异常时立即进入调试器。

谢谢,但这正是问题所在-我单击“调试”菜单-那里没有“异常”子项。。。我正在使用VS 2010 Express,我尝试从多个方向查找此异常选项位置,但目前运气不佳。@SLaks似乎必须这样做才能通知。@SLaks感谢我阅读了它并添加了此命令行,现在让我们看看它是否会通知我此异常哦,我的上帝。我真的。。。疯狂的由于int解析失败而导致异常!哇,谢谢你帮我找到这个!