Hello, i have some problem with detecting when end of recieve occurs. I want to communicate with Asterisk server, but that isn not the issue here. I have the following code:
// Connect to the asterisk server. Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.10.250"), 5038); clientSocket.Connect(serverEndPoint);
// Login to the server; manager.conf needs to be setup with matching credentials. clientSocket.Send(Encoding.ASCII.GetBytes("Action: Login\r\nUsername: administrator\r\nSecret: neven1206\r\nActionID: 1\r\n\r\n"));
int bytesRead = 0;
do { byte[] buffer = new byte[1024]; bytesRead = clientSocket.Receive(buffer); Console.WriteLine(bytesRead.ToString()); //Console.WriteLine(bytesRead + " bytes from asterisk server.");
string response = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine(response);
} while (bytesRead != 0); Console.WriteLine("Connection to server lost."); Console.ReadLine(); }
|
Everything is working except, i'm stuck in do-while. I cannot get to the
Console.WriteLine("Connection to server lost.");
part of code. I don't know what i'm doing wrong.
Please help
NevenPosted Jul 4, 2010, 2:17 AM
I want to get reply from server immediately and move from loop. I don't want to close the socket. The writing "connection to the server lost" is just example, the next step in code to see if my program reaches through the loop. I could put instead. :)
Thanks for quick reply
Deepak BhatiaPosted Jul 3, 2010, 11:41 PM
I just wann make one thing clear do you want to move out from do while loop when server closes the socket or you just want to read the reply immediately and move out from loop.
Reply if you are still waiting for your answer.