我的方法有问题。我StreamReader.ReadLine()
写了一个 C# 程序,但只有 TCPclient 端。我正在使用 Hercules 程序来创建 TCPServer。我输入了端口号并建立了连接。然后我运行我的程序并单击连接按钮。然后,我与 TCPServer 建立了联系。
我将数据从 TCPClient 发送到 Hercules(TCPServer) 没有问题。如图所示,我将字符串输入到“Entered to Send”EditText,然后单击Send
按钮并发送字符串。(我们可以从Received data部分观察发送数据。)
到这里为止,我没有问题。发送部分后,我无法将数据从 hercules(TCPServer) 发送到 TCPClient。为了发送数据,我将数据写入 发送部分并单击Send
按钮。之后,要查看即将到来的数据,您应该单击Show
按钮.....问题就在这里。我无法从ReadStream Buffer中读取数据。我调试了我的程序,发现有问题的部分是:
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 !!!");
}
}
在该try
部分内线;
gelen = read_stream.ReadLine();
有问题 .ReadLine() 没有从缓冲区读取数据。这是有趣的部分,当您发送数据并单击Show
按钮时,程序会冻结,但如果您hercules.png
通过单击Close
按钮关闭连接,read_stream.ReadLine();
则会获取数据并放入Received: EditText。
- 为什么在与 TCPServer 断开连接后需要数据?
- 可能是
\r\n
字符有问题? - 我以为它在等待下一个角色。所以,当我断开连接时它起作用了,因为它意识到三个不再是字符。我的问题是:
如何使用read_stream.ReadLine(); 采取不断开连接发送数据?
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 !!!");
}
}
}
}
这是我的 main.cs 我忘了添加它。您可以编写它并尝试我的项目...注意:这是可移植的 hercules 的链接,您可以轻松运行它 [ http://www.hw-group.com/products/hercules/index_en.html] 3