1

I'm trying to develop a simple TCP Client Application for Windows Phone.

On the server side, I'm using a simple C# Server Application which accepts the connection and then saves the file.

I saw an example on MSDN (for the client app, http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202858(v=vs.105).aspx). But it only sends strings and I want to send files (pictures) from the client to the picture.

This is a server side code snippet which accepts the file sent from the client:

if (Listener.Pending())
                {
                    client = Listener.AcceptTcpClient();
                    netstream = client.GetStream();
                    Status = "Connected to a client\n";
                    result = MessageBox.Show(message, caption, buttons);

                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                        string SaveFileName = string.Empty;
                        SaveFileDialog DialogSave = new SaveFileDialog();
                        DialogSave.Filter = "All files (*.*)|*.*";
                        DialogSave.RestoreDirectory = true;
                        DialogSave.Title = "Where do you want to save the file?";

                        DialogSave.FileName = "sample.txt";
                        if (DialogSave.ShowDialog() == DialogResult.OK)
                            SaveFileName = DialogSave.FileName;
                        if (SaveFileName != string.Empty)
                        {
                            int totalrecbytes = 0;
                            FileStream Fs = new FileStream(SaveFileName, FileMode.OpenOrCreate, FileAccess.Write);
                            while ((RecBytes = netstream.Read(RecData, 0, RecData.Length)) > 0)
                            {
                                Fs.Write(RecData, 0, RecBytes);
                                totalrecbytes += RecBytes;
                            }
                            Fs.Close();
                        }
                        netstream.Close();
                        client.Close();
                    }
                }

Now, the problem that I'm facing is that when I send the string from the phone, the server successfully acknowledges the connection and prompts to save the file. But, when I save the file and open it, the file is blank.

To check if the server is working properly, I made a simple C# client app (on Windows, not phone) and sent a file using that. And, it was saved successfully with all the contents intact.

Please help me.

Problems: First, the string sent by the phone is acknowledged by the server but cannot be saved to a file. Second, how to send an image from the phone (client)?

I thought of converting the image into base64 string and then sending the string to the server. But, I don't know how to convert an image to a base64 string on Windows Phone.

Please help me. Thanks in advance!

4

2 回答 2

0

将 imageStream 转换为内存 Stream。然后转换为字节,然后转换为 Base 64。

Stream imgStream = readImgFromFile(filename);
var memoryStream = new MemoryStream(imgStream);
byte[] result = memoryStream.ToArray();
base64 = System.Convert.ToBase64String(result);
于 2013-06-28T06:50:06.223 回答
0

您当然可以自己编写所有这些,但您是否考虑过使用库来帮助节省时间?如果是这样,请查看networkcomms.net,特别是关于发送非原始对象的教程,使用示例中的图像,here

免责声明:我是这个库的开发人员。

于 2013-06-27T09:37:17.920 回答