gpt4 book ai didi

c# - TCP/IP 在不关闭连接的情况下连续接收消息

转载 作者:可可西里 更新时间:2023-11-01 02:53:51 25 4
gpt4 key购买 nike

我一直在观看以下关于如何创建 TCP/IP 连接的视频:Server Client我需要做的是创建一个游戏 (flipcard),在其中我只为矩阵发送 2 个坐标。而已。或更少。

(接下来是代码)

据我观察,他每次发送消息时都会关闭并重新打开连接。它的工作。但是,我如何修改代码以保持连接打开并检测消息何时到达

我所有的谷歌搜索都让我找到了这两种方法:

  1. I have had luck using the socket object directly (rather than the TCP client). (但我不想使用套接字)
  2. Also uses sockets

当我点击启动服务器时

        private void bStartServer_Click(object sender, EventArgs e)
{
Thread tcpServerRunThread = new Thread(new ThreadStart(TCPServerRun));
tcpServerRunThread.Start();
}

服务器运行并等待连接

    private void TCPServerRun()
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 2016);
tcpListener.Start();
updateUI("Listening");
while (true)
{

TcpClient client = tcpListener.AcceptTcpClient();
updateUI("Connected");
Thread tcpHandlerThread = new Thread(new ParameterizedThreadStart(tcpHandler));
tcpHandlerThread.Start(client);
}
}

我处理我的消息

    private void tcpHandler(object client)
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 5004);
TcpClient mClient = (TcpClient)client;
NetworkStream stream = mClient.GetStream();
byte[] message = new byte[1024];
stream.Read(message,0, message.Length);
updateUI("New message = " + Encoding.ASCII.GetString(message));
stream.Close();
mClient.Close();
}

注意:updateUI是他做的更新列表框项的函数

客户端 - 几乎像服务器端 `private void button1_Click(object sender, EventArgs e) { 线程 mThread = new Thread(new ThreadStart(ConnectAsClient)); mThread.Start();

    private void ConnectAsClient()
{
// aici putem inlocui cu adresa de IP dorita
//client.Connect(IPAddress.Parse(textBox.Text), 5004);
client = new TcpClient();
//client.Connect(IPAddress.Parse("127.0.0.1"), 2016); // for when I'm not connected to network
client.Connect(IPAddress.Parse("my_ip_here"), 2016); // for when I am connected to the network
updateUI("connected");
NetworkStream stream = client.GetStream();
string s = "Hello world!";
byte[] message = Encoding.ASCII.GetBytes(s);
stream.Write(message, 0, message.Length);
this.updateUI("Message sent!");
stream.Close();
// client.Close();
}`

如果我没记错的话,每次他再次连接时,他都会创建一个新连接,而不是打开旧连接,对吗?(我是第一次使用TCP/IP,自学,所以如果可能我宁愿不使用套接字)

-------------------------------------------- --------------------------

我的代码的完整链接(根据开头提供的视频稍作修改):

  1. TCPClient
  2. TCPServer

最佳答案

您每次发送消息时都会重新连接。为此使用两种不同的方法;一个用于连接到服务器,然后另一个用于实际发送消息。像这样:

TcpClient client;
private void ConnectAsClient()
{
//client.Connect(IPAddress.Parse(textBox.Text), 5004);
client = new TcpClient();
//client.Connect(IPAddress.Parse("127.0.0.1"), 2016);
client.Connect(IPAddress.Parse("my_ip_here"), 2016);
updateUI("connected");
}
void SendMessage()
{
NetworkStream stream = client.GetStream();
string s = "Hello world!";
byte[] message = Encoding.ASCII.GetBytes(s);
stream.Write(message, 0, message.Length);
this.updateUI("Message sent!");
}

关于c# - TCP/IP 在不关闭连接的情况下连续接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41558538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com