First I'll apologize if any of my code doesn't make sense as i am fairly new to coding and currently in University.
Any way my problem is as described.
I have this code in my python server to send the data which is gathered from my local oracle server:
query = "SELECT * FROM DATABASE"
cursor.execute(query)
for row in cursor:
for cell in row:
cellF = str.format("{0}$",cell)
self.send(cellF.encode())
And this code to receive the data on my C# client:
public void Read()
{
var buffer = new byte[1024];
var stream = client.socket.GetStream();
stream.BeginRead(buffer, 0, buffer.Length, EndRead, buffer);
}
public void EndRead(IAsyncResult result)
{
var stream = client.socket.GetStream();
int endBytes = stream.EndRead(result);
if (endBytes == 0)
{
return;
}
var buffer = (byte[])result.AsyncState;
string data = Encoding.ASCII.GetString(buffer, 0, endBytes);
SetRecieved(data);
stream.BeginRead(buffer, 0, buffer.Length, EndRead, buffer);
}
Revision: I realized i had some misc code so i removed it and now it consistently works but only when the method is ran for the second time.
public void Read()
{
while (client.stream.DataAvailable)
{
client.stream.BeginRead(client.buffer, 0, client.buffer.Length, EndRead, client.buffer);
}
}
public void EndRead(IAsyncResult result)
{
var stream = client.socket.GetStream();
int endBytes = stream.EndRead(result);
var buffer = (byte[])result.AsyncState;
string data = Encoding.ASCII.GetString(buffer, 0, endBytes);
SetRecieved(data);
}
The first time i send the request to the server to get this data it works as expected but the second time i request the data i loose a random string of data along the way, the first time always works perfectly however and i receive all strings so I'm not sure what is happening.
Lets do some analysis...
You send some data from the server to the client that is less then size of the buffer. You read it and that's all. You call
returnin the asynccallback function. If you don't use while loop then you will not be able to receive new data from server.You send two messages from the server to the client. This two messages are in the same buffer, because they summed size is less then buffer size. You read them as one with your approach and your data is corrupted, because you don't know the data size.
If you want to treat your data as separate messages you need to know the message size and handle each case differently. Data is less then buffer size - you need to continue waiting for incoming data. Data is greater then buffer size - you need to read whole message. And so on.