P
P
piffo2020-06-28 18:46:35
C++ / C#
piffo, 2020-06-28 18:46:35

Sending data about all disks to the server?

Customer

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
 
namespace joke1
{
    class Program
    {
        static int port = 8005;
        static string address = "127.0.0.1";
 
        static void Main(string[] args)
        {
            DriveInfo[] allDrivers = DriveInfo.GetDrives();
 
            string[] dataDrive = new string[6];
 
            foreach(DriveInfo drive in allDrivers)
            {
                dataDrive[0] = drive.Name + " ";
                dataDrive[1] = drive.DriveType.ToString() + " ";
                dataDrive[2] = drive.DriveFormat + " ";
                long totalSize1 = drive.TotalSize >> 30;
                long freeSpace1 = drive.AvailableFreeSpace >> 30;
                dataDrive[3] = Convert.ToInt32(totalSize1).ToString() + "gb" + " ";
                dataDrive[4] = Convert.ToInt32(freeSpace1).ToString() + "gb" + " ";
                dataDrive[5] = drive.RootDirectory.ToString() + " ";
            }
 
            try
            {
                IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(address), port);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
                socket.Connect(ipPoint);
                byte[][] data = new byte[6][];
                for(int i = 0; i < 6; i++)
                {
                    data[i] = Encoding.Unicode.GetBytes(dataDrive[i]);
                    socket.Send(data[i]);
                }
 
                Console.WriteLine("Данные отправлены");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

Through foreach I get data about all disks on the PC. But the client only sends data about the latter. How can I send data about all disks?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir S, 2020-06-28
@piffo

you collect data on all disks - but only one remains - the last one, because in the first loop foreach(DriveInfo drive in allDrivers)you overwrite them.
to put it simply, you can do it like this

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
 
namespace joke1
{
    class Program
    {
        static int port = 8005;
        static string address = "127.0.0.1";
 
        static void Main(string[] args)
        {
            DriveInfo[] allDrivers = DriveInfo.GetDrives();

            var dataDrivers = new List<string[]>();

            foreach(DriveInfo drive in allDrivers)
            {
                string[] dataDrive = new string[6];
                dataDrive[0] = drive.Name + " ";
                dataDrive[1] = drive.DriveType.ToString() + " ";
                dataDrive[2] = drive.DriveFormat + " ";
                long totalSize1 = drive.TotalSize >> 30;
                long freeSpace1 = drive.AvailableFreeSpace >> 30;
                dataDrive[3] = Convert.ToInt32(totalSize1).ToString() + "gb" + " ";
                dataDrive[4] = Convert.ToInt32(freeSpace1).ToString() + "gb" + " ";
                dataDrive[5] = drive.RootDirectory.ToString() + " ";
                dataDrivers.Add(dataDrive);
            }
 
            try
            {
                IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(address), port);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
                socket.Connect(ipPoint);

                foreach(var driveInfo in dataDrivers)
                {
                    byte[][] data = new byte[6][];
                    for(int i = 0; i < 6; i++)
                    {
                        data[i] = Encoding.Unicode.GetBytes(driveInfo[i]);
                        socket.Send(data[i]);
                    }
                }

                Console.WriteLine("Данные отправлены");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

T
Timur Pokrovsky, 2020-06-28
@Makaroshka007

Maybe so?

byte[][] data = new byte[6][];
for(int i = 0; i < 6; i++) {
     data[i] = Encoding.Unicode.GetBytes(dataDrive[i]);
}
socket.Send(data);

P
Peter, 2020-06-28
@Morpheus_God

Of course, I may not understand something from the question, but what prevents you from simply creating a certain structure of your data and sending it in a bunch through serialization? You can form a List on the client from information about hard drives and then get everything on the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question