Answer the question
In order to leave comments, you need to log in
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();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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();
}
}
}
}
Maybe so?
byte[][] data = new byte[6][];
for(int i = 0; i < 6; i++) {
data[i] = Encoding.Unicode.GetBytes(dataDrive[i]);
}
socket.Send(data);
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 questionAsk a Question
731 491 924 answers to any question