views:

50

answers:

2

Dear Sirs; i have two Application to listen network Stream : Server.cs on the other hand; send file Client.cs. But i want to send more files on a stream from any folder. For example. i have C:/folder whish has got 3 jpg files. My client must run. Also My server.cs get files on stream:

Client.cs:

 private void btn_send2_Click(object sender, EventArgs e)
        {
            string[] paths= null;

            paths= System.IO.Directory.GetFiles(@"C:\folder" + @"\", "*.jpg", System.IO.SearchOption.AllDirectories);

            byte[] Dizi;
            TcpClient Gonder = new TcpClient("127.0.0.1", 51124);
            FileStream Dosya;
            FileInfo Dos;
            NetworkStream Akis;

            foreach (string path in paths)
            {
                Dosya = new FileStream(path , FileMode.OpenOrCreate);
                Dos = new FileInfo(path );
                Dizi = new byte[(int)Dos.Length];
                Dosya.Read(Dizi, 0, (int)Dos.Length);
                Akis = Gonder.GetStream();
                Akis.Write(Dizi, 0, (int)Dosya.Length);
                Gonder.Close();
                Akis.Flush();
                Dosya.Close();
            }
        }

Also i have Server.cs

 void Dinle()
        {
            TcpListener server = null;
            try
            {

                Int32 port = 51124;
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                server = new TcpListener(localAddr, port);
                server.Start();
                Byte[] bytes = new Byte[1024 * 250000];
                // string ReceivedPath = "C:/recieved";


                while (true)
                {
                    MessageBox.Show("Waiting for a connection... ");

                    TcpClient client = server.AcceptTcpClient();
                    MessageBox.Show("Connected!");

                    NetworkStream stream = client.GetStream();
                    if (stream.CanRead)
                    {
                        saveFileDialog1.ShowDialog();

                        // burası degişecek
                        string pathfolder = saveFileDialog1.FileName;
                        StreamWriter yaz = new StreamWriter(pathfolder);

                        string satir;
                        StreamReader oku = new StreamReader(stream);

                        while ((satir = oku.ReadLine()) != null)
                        {


                            satir = satir + (char)13 + (char)10;
                            yaz.WriteLine(satir);

                        }
                        oku.Close();
                        yaz.Close();
                        client.Close();
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }


            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }

Please look Client.cs: icollected all files from "c:\folder"

 paths= System.IO.Directory.GetFiles(@"C:\folder" + @"\", "*.jpg", System.IO.SearchOption.AllDirectories);

My Server.cs how to get all files from stream?

A: 

It's not a typical way of doing things.

Usually you would write a single file in a single stream and repeat the process for multiple files. You risk data corruption this way as you'll have to place some kind of marker in the stream to know where to split it.

Doobi
A: 

The easiest way is to use multiple sockets, one for commands and one (or more) for sending files.

The below code have NOT been tested, just wrote it up to show what I mean. It's multithreaded and can receive multiple files from the same client, just call sendfiles multiple times in the client.

You might want to add error/exception handling if you decide to finish the code.

Server.cs

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Test2
{
    public class Server
    {
        private readonly TcpListener _listener = new TcpListener(1234);

        public void Start()
        {
            _listener.BeginAcceptTcpClient(OnClient, null);
        }

        private void OnClient(IAsyncResult ar)
        {
            // End async accept and start wait for a new connection again
            TcpClient client = _listener.EndAcceptTcpClient(ar);
            _listener.BeginAcceptTcpClient(OnClient, null);

            // Let's start receiving files from the accepted client.
            var context = new Context {Client = client, Buffer = new byte[8196]};
            client.GetStream().BeginRead(context.Buffer, 0, context.Buffer.Length, OnReceive, context);
        }

        /// <summary>
        /// Got some stuff from a client
        /// </summary>
        /// <param name="ar"></param>
        private void OnReceive(IAsyncResult ar)
        {
            // got a file command
            var context = (Context) ar.AsyncState;
            int bytesRead = context.Client.GetStream().EndRead(ar);

            string cmd = Encoding.UTF8.GetString(context.Buffer, 0, bytesRead);
            string[] parts = cmd.Split(';');
            string command = parts[0];


            // want to send another file
            if (command == "sendfile")
            {
                // context info for receiving files
                var client = new FileClient();
                client.FileName = parts[1];
                client.Size = long.Parse(parts[2]);
                client.FileStream = new FileStream("C:\\" + client.FileName, FileMode.CreateNew, FileAccess.Write);

                // startup listener where we are going to receive the file.
                var listener = new TcpListener(IPAddress.Any, 0); // get a kernelassigned number
                client.Listener = listener;
                listener.Start();
                listener.BeginAcceptTcpClient(OnFileSocket, client);

                // send reply
                var ep = (IPEndPoint) listener.LocalEndpoint;
                byte[] reply = Encoding.UTF8.GetBytes(ep.Port.ToString());
                context.Client.GetStream().Write(reply, 0, reply.Length);
            }
        }

        // Receiving the actual files.
        private void OnFileSocket(IAsyncResult ar)
        {
            var client = (FileClient) ar.AsyncState;
            client.Socket = client.Listener.EndAcceptTcpClient(ar);

            var buffer = new byte[8192];
            client.Buffer = buffer;
            client.Socket.GetStream().BeginRead(buffer, 0, buffer.Length, OnReceiveFile, client);
        }

        private void OnReceiveFile(IAsyncResult ar)
        {
            var client = (FileClient) ar.AsyncState;
            int bytesRead = client.Socket.GetStream().EndRead(ar);
            client.Received += bytesRead;

            client.FileStream.Write(client.Buffer, 0, bytesRead);

            // recieved complete file, disconnect and exit.
            if (client.Received == client.Size)
            {
                client.FileStream.Close();
                client.Socket.Close();
                return;
            }

            client.Socket.GetStream().BeginRead(client.Buffer, 0, client.Buffer.Length, OnReceiveFile, client);
        }

        #region Nested type: Context

        private class Context
        {
            public byte[] Buffer;
            public TcpClient Client;
        }

        #endregion

        #region Nested type: FileClient

        private class FileClient
        {
            public byte[] Buffer;
            public string FileName;
            public FileStream FileStream;
            public TcpListener Listener;
            public long Received;
            public long Size;
            public TcpClient Socket;
        }

        #endregion
    }
}

Client.cs

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Test2
{
    internal class Client
    {
        private readonly IPAddress _server;
        private readonly TcpClient _tcpClient = new TcpClient();

        public Client(IPAddress server)
        {
            _server = server;
            _tcpClient = new TcpClient();
        }

        public void Connect()
        {
            _tcpClient.Connect(new IPEndPoint(_server, 1234));
        }

        // asks server on which port the file should be sent.
        private int RequestPort(string fileName, long length)
        {
            // lock tpcClient for each request, so we dont mix up the responses.
            lock (_tcpClient)
            {
                // send request
                byte[] bytes = Encoding.UTF8.GetBytes("sendfile;" + fileName + ";" + length);
                _tcpClient.GetStream().Write(bytes, 0, bytes.Length);

                // get reply
                var buffer = new byte[1024];
                int bytesRead = _tcpClient.GetStream().Read(buffer, 0, buffer.Length);
                string reply = Encoding.UTF8.GetString(buffer, 0, bytesRead);

                // error message or port?
                int port;
                if (!int.TryParse(reply, out port))
                    throw new InvalidOperationException("Server sent an error:" + reply);
                return port;
            }
        }

        public void SendFiles(string[] fileNames)
        {
            // Use a buffer to not preserve memory (instead of reading whole file into memory)
            var buffer = new byte[8192];
            foreach (string fileName in fileNames)
            {
                using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    // Send on commandchannel that we want to send a file.
                    int filePort = RequestPort(Path.GetFileName(fileName), fileStream.Length);
                    var client = new TcpClient(new IPEndPoint(_server, filePort));
                    NetworkStream stream = client.GetStream();

                    // repeat until there are no more bytes to read.
                    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                    while (bytesRead > 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                    }

                    stream.Close();
                    client.Close();
                }
            }
        }
    }
}
jgauffin