コード例 #1
0
        public void ReadCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                String data = Encoding.ASCII.GetString(state.buffer, 0,
                                                       bytesRead);

                if (data != "!quit")
                {
                    Console.WriteLine("[s] Data : {0}", data);

                    // After logging in, send list of file inside the DIRECTORY_PATH
                    if (data.StartsWith(":log:"))
                    {
                        // change the label in the ServerForm to connected.
                        // MethodInvoker is used here to change the label because this part is being run in a separate thread.
                        MethodInvoker inv = delegate {
                            lblClientConnected.Text = "Client Connected.";
                        };
                        Invoke(inv);

                        if (Directory.Exists(DIRECTORY_PATH))
                        {
                            int count = 1;
                            foreach (string filename in Directory.GetFiles(DIRECTORY_PATH))
                            {
                                FileInfo fileInfo = new FileInfo(filename);
                                dirFiles.Add(count, Path.GetFileName(filename) + ":" + fileInfo.Length);
                                count++;
                            }

                            // process to send the dirFiles dictionary back to the client
                            UtilityLibrary.Message message = SerialLibrary.Serialize(dirFiles);
                            Send(handler, message);
                        }
                    }
                    else
                    {
                        // this part is when the client request for file download
                        string filepath = DIRECTORY_PATH + dirFiles[int.Parse(data)].Split(':')[0];
                        Console.WriteLine("initiate send file: {0}", filepath);
                        handler.SendFile(filepath);

                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();
                    }
                }
                else
                {
                    Console.WriteLine("[s] Quit Signal : {0}", data);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
        }
コード例 #2
0
 public void Send(Socket handler, UtilityLibrary.Message message)
 {
     // Begin sending the data to the remote device.
     handler.BeginSend(message.Data, 0, message.Data.Length, 0,
                       new AsyncCallback(SendCallback), handler);
 }