Hi.
I am making just a simple socket program. Server program is always working. When I open client program and click the button, sending/receiving data normally. Everything's ok. But when I close client and opening again, and then clicking button, client program getting lock. Not getting error. When I debug, its waiting on "readline" line. What is the problem? Why its not working when I reopen client program?
Client
private void Form1_Load(object sender, EventArgs e)
{
try
{
tcp_client = new TcpClient("192.168.1.222", 4444);
}
catch
{
label2.Text = "not connected";
return;
}
ag_akimi = tcp_client.GetStream();
akim_okuyucu = new StreamReader(ag_akimi);
akim_yazici = new StreamWriter(ag_akimi);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
akim_yazici.WriteLine("sample ");
akim_yazici.Flush();
textBox1.Text += akim_okuyucu.ReadLine();
}
catch
{
MessageBox.Show("error!");
}
}
private void form_kapatiliyor(object sender, FormClosingEventArgs e)
{
try
{
akim_okuyucu.Close();
akim_yazici.Close();
ag_akimi.Close();
istemcisoketi.Close();
}
catch
{
MessageBox.Show("Not properly closed");
}
}
}
***********************************************
server
public void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
thread_dinleyici = new Thread(new ThreadStart(dinle));
thread_dinleyici.Start();
}
public void dinle()
{
tcp_listener = new TcpListener(IPAddress.Any, 4444);
tcp_listener.Start();
istemcisoketi = tcp_listener.AcceptSocket();
ag_akimi = new NetworkStream(istemcisoketi);
if (istemcisoketi.Connected)
{
while (true)
{
akim_yazici = new StreamWriter(ag_akimi);
akim_okuyucu = new StreamReader(ag_akimi);
try
{
richTextBox1.Text += akim_okuyucu.ReadLine();
akim_yazici.WriteLine("message");
akim_yazici.Flush();
}
catch
{
label1.Text = "closing";
istemcisoketi.Close();
ag_akimi.Close();
akim_yazici.Close();
akim_okuyucu.Close();
istemcisoketi.Close();
return;
}
}
}
else
{
}
}
Loading
Sam HobbsPosted Jan 12, 2010, 5:00 PM
I don't know why the client needs to send data back to the server; if it needs to send a request that indicates what data it needs, then that would be different from what I said above.
Sam HobbsPosted Jan 11, 2010, 7:48 PM
Note that I did have more comments for that thread if you needed more. I think one problem is that sockets cache small amounts of data and that is a problem for your test code. In my version of that code, it sent all the data going one way first then it sent the feedback data. That caused the two programs to not communicate the way they were intended to. I am not sure how to solve the problem but perhaps you need to use asynchronous communication. It might work better if you test by sending a large amount of test data. It also might work better if you don't get feedback after each write; you are sending data, then waiting for feedback and the waiting for feedback after sending a small abount of data is one problem.
Then in your Sending data to client I provided links to some relevant articles; did they not help? What about Network Programming in C# - Part 1, Network Programming in C# - Part 2, and Socket Programming in this web site? Have you looked at those two? I think the overall structure of your program is not consistent with the design of a good sockets program for doing what you are doing. I really think it would be best to start with a sample program written by an expert, such as in the articles above.
Then in your Sending a DataTable to a Client I suggested that you use a download manager; isn't that a better solution?