Reputation:
I am trying to send some strings over Network between Server and Client. It works great for small strings(upto ~1400 Bytes) but as soon i send more than ~ 1400 Bytes of data, most of the times i get only 1400 Bytes and data >1400 Bytes get lost. Hier is my Client code:
public static string Senddata(string BarcodeText)
{
try
{
string textToSend = BarcodeText.ToString();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
//---send the text---
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
return "Success";//if sent
}
catch (Exception)
{
return "Fail";//if not sent
}
}
// Recieve Data from Server
public static string Getdata()
{
try
{
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
str = Encoding.GetEncoding("ISO-8859-1").GetString(bytesToRead, 0, bytesRead);
return str;
}
catch (Exception)
{
return "Did not recieved anything from Virtual PC";
}
}
Hier is the code for the Server:
public static string GetDatafromClient()
{
try
{
//---get the incoming data through a network stream---
nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
//Console.WriteLine("buffer");
//---read incoming stream---
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
return Encoding.ASCII.GetString(buffer, 0, bytesRead);
}
catch (Exception)
{
return "-";
}
}
public static void SendDatatoClient(string DataToSend)
{
FileHandling.SaveToFile(DataToSend);
//---write back the text to the client---
byte[] bytesTosend = Encoding.GetEncoding("ISO-8859-1").GetBytes(DataToSend);
nwStream.Write(bytesTosend, 0, bytesTosend.Length);
}
Upvotes: 1
Views: 658
Reputation:
So, I solved this issue by sending first 4 bytes as a data length and then the actual data and i am getting 0 data loss now. Thank you all guys for tying to help me. The client side looks like:
public static string Getdata()
{
try
{
byte[] DataLengthinBytes = new byte[4];
nwStream.Read(DataLengthinBytes, 0, 4);
int DataLength = BitConverter.ToInt32(DataLengthinBytes, 0);
byte[] bytesToRead = new byte[DataLength];
StringBuilder str = new StringBuilder();
int bytesRead = 0;
int i = 0;
while(i < DataLength){
bytesRead = nwStream.Read(bytesToRead, 0, DataLength);
str.AppendFormat("{0}", Encoding.GetEncoding("ISO-8859-1").GetString(bytesToRead, 0, bytesRead));
i += bytesRead;
}
return str.ToString();
}
catch (Exception)
{
return "Did not recieved anything from Virtual PC";
}
}
and the server side:
public static void SendDatatoClient(string DataToSend)
{
//---write back the text to the client---
byte[] bytesTosend = Encoding.GetEncoding("ISO-8859-1").GetBytes(DataToSend);
byte[] Datalength = BitConverter.GetBytes((Int32)bytesTosend.Length);
nwStream.Write(Datalength, 0, 4);
nwStream.Write(bytesTosend, 0, bytesTosend.Length);
}
Upvotes: 1
Reputation: 2072
You can do a loop as in the MSDN example
// Examples for CanRead, Read, and DataAvailable.
// Check to see if this NetworkStream is readable.
if(myNetworkStream.CanRead){
byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while(myNetworkStream.DataAvailable);
// Print out the received message to the console.
Console.WriteLine("You received the following message : " + myCompleteMessage);
else{
Console.WriteLine("Sorry. You cannot read from this NetworkStream.");
}
The problem is maybe that your data is sent in more than one "package". There are many ways to solve this. The Length byte is one of them. Another solution is to use framing bytes. -> you put your data between this bytes so you know the start and end -> 0x02 data 0x03.
In the long run it's maybe simpler to use a high level protocol such as REST.
Upvotes: 1