I write a Tcp Client with C# and Tcp Server with C++ In TCP server side, I received data by a loop:
unsigned char* buffer = new unsigned char[BUFFERSIZE];
int pLen =0;
int recievedLen = 0;
do{
recievedLen = OnReadData(0, clientSock,buffer,&pLen,1000);
printBytes(buffer,recievedLen);
}while(recievedLen>0);
In TCP Client side, I send some data and recieve the response:
private void Send(string data)
{
NetworkStream stream = tcpClient.GetStream();
byte[] writeBuffer = Encoding.ASCII.GetBytes(data);
stream.Write(writeBuffer, 0, writeBuffer.Length);
Console.WriteLine("写入数据:" + Encoding.ASCII.GetString(writeBuffer));
}
private void Receive()
{
NetworkStream stream = tcpClient.GetStream();
var readBuffer = new byte[2048];
while (!stream.DataAvailable)
{
Thread.Sleep(1);
}
int writeCount = stream.Read(readBuffer, 0, readBuffer.Length);
String content = Encoding.ASCII.GetString(readBuffer, 0, writeCount);
Console.WriteLine("读取数据:" + content);
}
Q: When I just Send data and don't call Recieve data at TCP Client side, the loop for receiving data in TCP Server side can finish harmonious. But when call Send function and then call Receive Function in TCP Client side, the recv function in TCP Server side will be blocked at second time(expected result is receving 0 byte). Any one can explain it? Thx