So I am kinda new to developing client and server sockets (protocols), and am developing something just for fun on my own network. This program basically will send all files from a specific folder to a client. So I am wondering if I wanted to send multiple files over one socket how would I differentiate between different files? I was thinking when we use text files we usually have some sort of delimiter, but what would I use for that kind of thing? Another thought I had was recording the number of bytes of each file in each transmission. I am new to protocol development, how would you all develop this. I am worried that if I use the length property of the FileInfo class it will not be precise, I have had problems before with sizes not being exact, but that could have been accounted for by a programming error on my part. Would the FileInfo length property be precise? I have read a java sockets book, and am in the process of reading a C# one, but the C# one is honestly not as good as the java one I am reading. The following code is untested.

    public class LanLeakThread {

        private Socket sock;
        private String path;

        public LanLeakThread(Socket sock, String path) {
            this.sock = sock;
            this.path = path;
        }//end constructor

        public void run() {
            ThreadPool.QueueUserWorkItem((object state) => {
                try {
                    sock.Send(GetBytes("Server Name: " + Environment.MachineName + "\n"));
                    sock.Send(GetBytes("Num Files:" + Directory.GetFiles(path).Length + "\n"));
                    foreach (String file in Directory.GetFiles(path)) {
                        try {
                            sock.Send(GetBytes("NumBytes: " + new FileInfo(file).Length));
                            sock.SendFile(file);
                        }
                        catch (Exception) { continue; }
                    }//end loop
                }
                catch (Exception) { sock.Close(); }
            });
        }//end method

        static byte[] GetBytes(string str) {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }//end method

        static string GetString(byte[] bytes) {
            char[] chars = new char[bytes.Length / sizeof(char)];
            System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
            return new string(chars);
        }//end method

    }//end class



    public class MultithreadedLanLeak {
        public const int PORT = 20;
        String path;

        public MultithreadedLanLeak(String path) {
            this.path = path;
        }//end constructor

        public void run() {
            ThreadPool.QueueUserWorkItem((object state) => {
                TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), PORT);
                while (true) {
                    new LanLeakThread(server.AcceptSocket(), path).run();
                }
                server.Stop();
            });
        }//end method

    }//end class

Don't use FileInfo. Load the file contents into a buffer (byte[]) and transmit the filename and buffer size in a well defined header(fixed length/format). Follow the header with the variable length byte[].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.