Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

File tranfer over a Stream

$
0
0

Hello

I've chat Tchat Program in WPF and  I´m trying to send a file over a network with TCP/IP.

Here is some part of my code : (sending and receiving file)

Client Side

public void SendMessage(string to, string msg, string file)
        {
            if (_conn)
            {

                bw.Write(IM_Send);
                bw.Write(to);
                bw.Write(msg);
		bw.Flush();
                byte[] bytes = File.ReadAllBytes(file);

                    sw.WriteLine(bytes.Length.ToString());
                    sw.Flush();

                    sw.WriteLine(file);
                    sw.Flush();
            }
        }

...

 TcpClient client;
        NetworkStream netStream;
        SslStream ssl;
        BinaryReader br;
        BinaryWriter bw;
        StreamWriter sw;
        bool _conn = false;    // Is connected/connecting?

...

 client = new TcpClient(Server, Port);  // Connect to the server.
            netStream = client.GetStream();
            ssl = new SslStream(netStream, false, new RemoteCertificateValidationCallback(ValidateCert));
            ssl.AuthenticateAsClient("InstantMessengerServer");
            // Encrypted connection.

            br = new BinaryReader(ssl, Encoding.UTF8);
            bw = new BinaryWriter(ssl, Encoding.UTF8);
            sw = new StreamWriter(ssl, Encoding.UTF8);
SERVER SIDE

while (client.Client.Connected)  // While we are connected.
                {
                    byte type = br.ReadByte();  // Get incoming packet type.

                    if (type == IM_IsAvailable)
                    {

                    }
                    else if (type == IM_Send)
                    {
                        string to = br.ReadString();
                        string msg = br.ReadString();

                        string filepath = "";
                        string rd = "";
                        string attachName = "";
                        string attachFile = "";
                        string filesize = "";


                            string cmdFileSize = sr.ReadLine();
                            string cmdFileName = sr.ReadLine();

                            int length = Convert.ToInt32(cmdFileSize);
                            byte[] buffer = new byte[length];
                            int received = 0;
                            int read = 0;
                            int size = 1024;
                            int remaining = 0;

                           try
                            {
                                while (received < length)
                                {
                                    remaining = length - received;
                                    if (remaining < size)
                                    {
                                        size = remaining;
                                    }

                                    read = netStream.Read(buffer, received, size);

                                    received += read;
                                }
                            }
                            catch (Exception e)
                            {
                                Console.Write(e.Message);
                            }

                            using (FileStream fStream = new FileStream(Path.GetFileName(cmdFileName), FileMode.Create))
                            {
                                fStream.Write(buffer, 0, buffer.Length);
                                fStream.Flush();
                                fStream.Close();
                            }

							//...


                    }



                }
The file is successfully transfered on server side but can not be read.

Please help me to fix  this issue .

Thanks


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>