C# Stream.write()每个方法只工作一次

C# Stream.write()每个方法只工作一次,c#,sockets,C#,Sockets,我有一个方法可以向客户端发送消息,如下所示。但是,如果我一次又一次地调用这个方法两次,它将无法正常工作,而且数据似乎从未发送过 我在下面添加了一些评论来说明这个问题 public static void SendRequestInformation(Client target, int messageId) { NetworkStream stream = target.getClientSocket().GetStream(); byte[] byteD

我有一个方法可以向客户端发送消息,如下所示。但是,如果我一次又一次地调用这个方法两次,它将无法正常工作,而且数据似乎从未发送过

我在下面添加了一些评论来说明这个问题

public static void SendRequestInformation(Client target, int messageId)
    {
        NetworkStream stream = target.getClientSocket().GetStream();
        byte[] byteData = null;
        switch (messageId)
        {
            case 0:
                byteData = Encoding.ASCII.GetBytes("ping");
                break;
            case 1:
                byteData = Encoding.ASCII.GetBytes("handshake");
                break; 
            case 3:
                byteData = Encoding.ASCII.GetBytes("osif");
                break;
            case 4:
                byteData = Encoding.ASCII.GetBytes("idle");
                break;
        }
        try
        {
            stream.Write(byteData, 0, byteData.Length);
            stream.Flush();
        }
        catch (Exception e)
        {
            ClientHandle.RemoveFromClientPool(target, "Unable to reach Client"); 
        }
    }

// so if here I have a method like...
private void onRefreshMenuClick(object sender, RoutedEventArgs e)
    {
        MenuItem mi = sender as MenuItem;
        if (mi != null)
        {
            ContextMenu cm = mi.CommandParameter as ContextMenu;
            if (cm != null)
            {
                Grid g = cm.PlacementTarget as Grid;
                if (g != null)
                {

                    var p = Mouse.GetPosition(g);

                    int row = 0;
                    int col = 0;
                    double accumulatedHeight = 0.0;
                    double accumulatedWidth = 0.0;

                    // calc row mouse was over
                    foreach (var rowDefinition in g.RowDefinitions)
                    {
                        accumulatedHeight += rowDefinition.ActualHeight;
                        if (accumulatedHeight >= p.Y)
                            break;
                        row++;
                    }
                    Client c = getRowClient(row);
                    if (c != null)
                    {
//the below two lines seem to not be called if called only the top one seems to work 
                        ConnectionHandle.SendRequestInformation(c, 1);
                        ConnectionHandle.SendRequestInformation(c, 4);
                    }
                    else
                        if (debugWindow != null)
                            debugWindow.LogTextBox.AppendText("Unable to find client!");
                }
            }
        }
    }

你应该详细说明“不能正常工作”。例外情况?第一次写入是否成功,随后是否可以写入错误?你在别的地方合上插座吗?等等,我似乎没有得到任何异常抛出。try-around stream.write未捕获任何内容。我所说的不能正常工作的意思是它不会发送两个请求。如果我不背对背地调用该方法,它就可以工作。为什么SendRequestInformation方法被标记为静态的?也可以尝试在刷新后添加stream.Close()。您是否意识到在接收端,您可能在一次接收操作中接收多个数据包?出于这个原因,网络流数据应该更自我描述,例如,通过在每个数据包前面加上数据包的长度。如果没有更多的信息,我猜雷尼的建议解决了这个问题。您没有解释是什么让您认为“它没有发送两个请求”,但最明显的解释是,您的接收代码没有注意到发出了两个请求。