2
Reply

C# Socket - end of receieving detection

Neven

Neven

Jul 3 2010 5:22 PM
3k
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

Answers (2)