I have the following code written in delphi2010:
TCPClient := TidTcpClient.Create;
TCPClient.Host := '192.168.12.131';
TCPClient.Port := 1312;
TCPClient.Connect;
TCPClient.IOHandler.WriteLn('msg', TEncoding.ASCII);
answer := TCPClient.IOHandler.ReadLn(TEncoding.ASCII);
This code works well – i get answer.
And i have the following C# code:
var client = new TcpClient();
client.Connect(endpoint);
var stream = client.GetStream();
var msgData = System.Text.Encoding.UTF8.GetBytes("msg");
stream.Write(msgData, 0, msgData.Length);
var answerData = new Byte[256];
var asnwerLength = stream.Read(answerData, 0, answerData.Length);
And it didn’t work – request timeout.
What is difference between these two parts?
Your delphi code is writing (by the looks of things) a complete line – presumably with some line ending characters. Maybe the server is expecting those?
Try:
Also, you need to bear in mind that
stream.Readwill return as much data as it currently has available – which may be less than a complete message from the server, or may include (parts of) several messages from the server. If you want to duplicate theReadLnbehaviour, you’ll need to search in the receive buffer for end of line characters yourself.