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!