[英]Reading message(data) from TCPServer Buffer by using StreamReader.ReadLine() in TCPClient(ReadLine() doesn't return until a disconnection)
I have problem with StreamReader.ReadLine()
method.I wrote a C# program, but only TCPclient side. 我遇到了StreamReader.ReadLine()
方法的问题。我编写了一个C#程序,但只编写了TCPclient端。 I'm using Hercules program to create a TCPServer. 我正在使用Hercules程序来创建TCPServer。 I entered the port number and made a connection. 我输入了端口号并建立了连接。 Then I ran my program and clicked to Connect button. 然后我运行我的程序并单击“连接”按钮。 then, I had connection with TCPServer. 然后,我与TCPServer有联系。
I have no problem with sending data from TCPClient to Hercules(TCPServer). 从TCPClient向Hercules(TCPServer)发送数据没有问题。 As you see in the picture, I'm entering the string into "Entered to Send" EditText, then I clicked to Send
button and I sent the String.(we can observe the sending data from the Received data part.) 正如你在图片中看到的那样,我将字符串输入“Entered to Send”EditText,然后我点击Send
按钮,我发送了字符串。(我们可以观察从Received数据部分发送数据。)
Until here, I have no problem. 直到这里,我没有问题。 After sending part, I cannot send data from hercules(TCPServer) to TCPClient. 发送部分后,我无法将数据从hercules(TCPServer)发送到TCPClient。 In order to send data, I wrote data into the Send part and clicked the Send
button. 为了发送数据,我将数据写入发送部分并单击Send
按钮。 after that, to see the coming data you should click the Show
button .....the problem is right here. 之后,要查看即将发布的数据,您应该单击“ Show
按钮.....问题就在这里。 I can not read the data from the ReadStream Buffer . 我无法从ReadStream缓冲区读取数据。 I debugged my program and I found the problematic part which is: 我调试了我的程序,我发现有问题的部分是:
private void btnShow_Click(object sender, EventArgs e)
{
try
{
string gelen;
gelen = read_stream.ReadLine();
txtReceive.Text = gelen;
MessageBox.Show(gelen, "you have message from server");
}
catch
{
MessageBox.Show("message could not taken !!!");
}
}
inside the try
part the line; 在try
部分内线;
gelen = read_stream.ReadLine();
has problem .ReadLine() is not reading the data from buffer. 有问题.ReadLine()没有从缓冲区读取数据。 here is the interesting part, when you send data and click the Show
button the program freezes, but if you close the connection from hercules.png
by clicking Close
button, read_stream.ReadLine();
这是有趣的部分,当您发送数据并单击“ Show
按钮时,程序会冻结,但如果您通过单击“ Close
按钮Close
hercules.png
的连接,则read_stream.ReadLine();
is taking the data and putting into the Received: EditText. 正在获取数据并放入Received: EditText。
\\r\\n
characters are the problem? 可能是\\r\\n
字符是问题吗? How can I use read_stream.ReadLine(); 我怎样才能使用read_stream.ReadLine(); to take sending data without disconnection? 在不断开连接的情况下发送数据?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CSharpVeriDenemesi
{
public partial class Form1 : Form
{
//Burda server da tanımladıklarımızdan farklı olarak TcpClient sınıfı ile serverdan gelen bilgileri alıyoruz
public TcpClient Client;
private NetworkStream network_stream;
private StreamReader read_stream;
private StreamWriter write_stream;
private string local_host = "localhost";
private int port_number = 8001;
public TcpListener listener;
// IPAddress localAddress = IPAddress.Parse("127.0.0.1");
public Form1()//form oluşunca otomatik oluşturulan fonksiyon
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)//CONNECT
{
try
{
Client = new TcpClient(local_host, port_number);//İlk parametre bilgisayar adı ikincisi ise port numarasıdır.
MessageBox.Show("Baglandi");
}
catch
{
MessageBox.Show("Baglanamadi");
return;
}
network_stream = Client.GetStream();
read_stream = new StreamReader(network_stream);
write_stream = new StreamWriter(network_stream);
}
private void btnDisconnect_Click(object sender, EventArgs e)//DISCONNECT
{
txtSend.Text = "Disconnect clicked";
try
{
write_stream.Close();
read_stream.Close();
network_stream.Close();
}
catch
{
MessageBox.Show("Düzgün kapatilamiyor !!!" );
}
}
private void btnReset_Click(object sender, EventArgs e)//send ve receive text.box'larını resetliyor.
{
txtSend.Text = "";
txtReceive.Text = "";
MessageBox.Show("Reset'e basıldı");
}
private void Form1_Load(object sender, EventArgs e)//sayfa ilk açıldığında olcaklar için açılan dosya
{
}
private void btnSend_Click(object sender, EventArgs e)//veriyi server'a gönderiyor
{
try
{
write_stream.WriteLine(txtSend.Text);
write_stream.Flush(); //veriyi gönderiyor
MessageBox.Show("Veri gönderildi");
}
catch {
MessageBox.Show("Veri gönderilmedi !!!");
}
}
private void btnShow_Click(object sender, EventArgs e)
{
try
{
string gelen;
gelen = read_stream.ReadLine();
txtReceive.Text = gelen;
MessageBox.Show(gelen, "you have message from server");
}
catch
{
MessageBox.Show("message could not taken !!!");
}
}
}
}
here is my main.cs I forgot to add it. 这是我的main.cs我忘了添加它。 you can write it and try my project... note: here is the link for hercules it is portable, you can run it easily [ http://www.hw-group.com/products/hercules/index_en.html] 3 你可以写它并尝试我的项目...注意:这里是hercules的链接,它是便携式的,你可以轻松运行[ http://www.hw-group.com/products/hercules/index_en.html] 3
If your server side doesn't use Stream.WriteLine("something..");
如果您的服务器端不使用Stream.WriteLine("something..");
then you will always have to wait for End of line char (\\r or \\n or \\r\\n)
at client side when using ReadLine() function. 那么在使用ReadLine()函数时,您将始终必须等待客户End of line char (\\r or \\n or \\r\\n)
。
So the ReadLine() will complete only when connection is closed => your problem. 所以ReadLine()只有在连接关闭时才会完成=>你的问题。
Your code: 你的代码:
write_stream.WriteLine(txtSend.Text);
write_stream.Flush(); //veriyi gönderiyor
is used at client side, to feedback server, it has no meaning with server sending data. 在客户端使用,反馈服务器,它与服务器发送数据没有任何意义。
Try your client side by Reading char by char, or use Read() with BUFFER in specific length. 尝试使用char来读取char,或者使用具有特定长度的BUFFER的Read()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.