c# tcp server: receive incomplete result from client

207 views Asked by At

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

0

There are 0 answers