المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Using StreamReader to read an unkown amount of lines / data.



C# Programming
07-31-2009, 05:50 AM
Hello guys!
I'm writing my first network based application in order to learn yet a little more about the C# programming ********. And I've been up for hours now working on this program that works, to about 99%. But I want this extra feature that helps me when beta testing it. It's a server / client application that simply connects using sockets and IP addresses. I found it to be the best starting point since it's really basic. http://www.barakasoft.com/script/Forums/Images/smiley_biggrin.gif

So, the problem I have is that I made a "preloader" for my program, when the server starts it directly tries to establish a connection to my IP in order to send the servers IP address to me. If I got my client up and running and I've choosen to scan for new connections then it works as intended. However, I only get 1 IP adress, the first line sent to me. I know why, I just dont know the best solution to get multiple lines recieved.

Posting a small example so that you guys might be able to help me out on this one.


private void b3_Click(object sender, EventArgs e)
{
try
{
string serverIP = "";
TcpListener tcpListener2 = new TcpListener(1235);
tcpListener2.Start();
Socket socketfromServer = tcpListener2.AcceptSocket();

networkStream2 = new NetworkStream(socketfromServer);
streamReader2 = new StreamReader(networkStream2);
streamWriter2 = new StreamWriter(networkStream2);

serverIP = streamReader2.ReadLine();
t3.Text = serverIP;

}
catch (Exception xx)
{
MessageBox.Show(xx.ToString());
}
}


The server uses a For loop and if more IP addresses then one are found, then it will use WriteLine to post them one after another until they are all sent. With this usage, I got 1x ReadLine(), and thus I only get the first line sent from the server. So, how do I get multiple lines in a smart way? ^^