0

Question: I am getting LogicalDrivesList with DriveInfo.GetDrives() in client and then send the results back to the server, but the server will just show the first one. How can i get all results in server not just the first one ?

This is what this code will do: in case of server typed "Drive" client will get drives list and send back to server, then server will send next command. But server will just show "c:\" (not all result like "c:\" "d:\" "e:\" and ...)

Server:

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
     // RECEIVE
     var data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
     Console.WriteLine("Received: {0}", data);

     // SEND
     var command = Console.ReadLine();
     var commandToBytes = System.Text.Encoding.ASCII.GetBytes(command);
     stream.Write(commandToBytes, 0, commandToBytes.Length);
     Console.WriteLine("Sent: {0}", command);
}

Client:

// Get a stream object for reading and writing
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
    // RECEIVE
    var data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
    switch (data)
    {
         case "Drive":
             var drives = DriveInfo.GetDrives();
             foreach (var drive in drives)
             {
                  // SEND
                  var commandToBytes = System.Text.Encoding.ASCII.GetBytes(Convert.ToString(drive));
                  stream.Write(commandToBytes, 0, commandToBytes.Length);
                  Console.WriteLine("Sent: {0}", drive);
             }
             return;
         default:
             return;
    }
}

In client var drives = DriveInfo.GetDrives(); type is DriveInfo[]

And var commandToBytes = System.Text.Encoding.ASCII.GetBytes(Convert.ToString(drive)); type is Byte[]

In server var data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); type is String

Zsmaster
  • 1,549
  • 4
  • 19
  • 28
  • you should be aware that you could get things chunked with TCP. 1 `Write` may result in many `Read`s. – Daniel A. White Sep 03 '17 at 12:31
  • Thanks. i tried for more `Read`s and it worked. But thats sick, is there any better and cleaner way ? is saw SocketAsync and [CodeProjrct](https://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod) is it any better for somthing like chat or transfer bigger data? – DewinaTwice Sep 03 '17 at 13:03
  • 2
    TCP (or UDP) provides the pipe, so to speak. You need a higher level protocol (that sits on top of TCP) to exchange information. HTTP or FTP are examples, or you come up with your own... – C. Gonzalez Sep 03 '17 at 15:04
  • Thank you @C.Gonzalez , im goin to try it. – DewinaTwice Sep 04 '17 at 07:12
  • I have described how you can make your own (so called _message framing_, or more specifically _length-prefixing_) protocol in my answer to this question: [C# Deserializing a struct after receiving it through TCP](https://stackoverflow.com/a/37352525/3740093) - it will help you keep track of where data begins and ends. – Visual Vincent Sep 16 '17 at 08:13

0 Answers0