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

时间:2015-11-18 20:39:13

标签: 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 = 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);

它永远不会回来

为什么呢?有谁可以帮助我?

0 个答案:

没有答案