Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 客户端/服务器无法通过TCP发送图片_C#_Tcp_Client Server - Fatal编程技术网

C# 客户端/服务器无法通过TCP发送图片

C# 客户端/服务器无法通过TCP发送图片,c#,tcp,client-server,C#,Tcp,Client Server,我通过TCP向服务发送图片,但代码停在这一行: temp_Image = Image.FromStream(stream); 这是我的客户: // send picture button private void button3_Click(object sender, EventArgs e) { try { openFileDialog1.ShowDialog(); string _path =

我通过TCP向服务发送图片,但代码停在这一行:

temp_Image = Image.FromStream(stream);
这是我的客户:

// send picture button 
  private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            openFileDialog1.ShowDialog();
            string _path = openFileDialog1.FileName;
            pictureBox1.Image = Image.FromFile(_path);
            bool yo = picture_send();
            if (yo == true)
            {
                ms = new MemoryStream();
                pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                byte[] buffer = ms.GetBuffer();
                br = new BinaryWriter(stream);
                br.Write(buffer);
            }
        }
        catch(Exception ex)
        {
            int stop = 0;
        }
    }

 bool picture_send()
    {
        string To_send = "Picture: ";
        byte[] message = Encoding.ASCII.GetBytes(To_send);
        stream.Write(message, 0, message.Length);
        string temp = ReadTCP();
        updateUI(temp);
        if (temp == "Send picture")
        return true;
        else
        return false;
          }
这是服务器:

private void TCPL ( object client)
    {
        TcpClient mClient = (TcpClient)client;
        while (true)
        {
            NetworkStream stream = mClient.GetStream();
            BinaryReader _reader = new BinaryReader(stream);
            int temp1 = mClient.Available;
            if (temp1 > 0 )
            {
                        Byte[] message = new byte[temp1];
                stream.Read(message, 0, message.Length);
                String temp = Encoding.ASCII.GetString(message);
                process(temp, stream);
                }

            }
        }


  void process(string message, NetworkStream stream)
    {

       string[] temp = message.Split(':');
        if( temp[0] == "Login")
        {
           process_Login(temp[1], stream);
        }
        else if (temp[0] =="Sign-up" )
        {
            process_Sign_up(temp[1], stream);
        }
        else if (temp[0] == "Picture")
        {
            SendFunction("Send picture", stream);
            ReciveImage(stream);
        }

    }

   void ReciveImage(NetworkStream stream)
    {
        Image temp_Image = null;
            try
            {
                temp_Image = Image.FromStream(stream);
                temp_Image.Save(@"c:\theImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            }catch (Exception e)
            {
                int stop = 0;
            }
    }
因此,在客户端是使其

br.Write(buffer);
在服务器端,它可以

temp_Image = Image.FromStream(stream);
它再也不会回来了


为什么??有人能帮我吗?

你有什么错误吗?您添加了日志记录吗?在代码的catch块中,如果它们被命中。。为什么不捕获
var errMsg=e.Message使用调试器并逐步完成代码我没有收到任何错误或异常。节目就放在那里。如果我关闭客户端,服务器将转到下一行并保存图片。但前提是我用调试器关闭客户端。为什么?不要使用ASCII编码[byte[]message=Encoding.ASCII.GetBytes(To_send);]。更改为UTF8编码。