【问题标题】:A Problem With The Module "SimpleTCP" When Making a Chat Application制作聊天应用程序时模块“SimpleTCP”的问题
【发布时间】:2019-03-24 15:03:36
【问题描述】:

所以我想用 C# 制作一个聊天应用程序,我看了一个关于它的视频(https://www.youtube.com/watch?v=ve2LX1tOwIM),我现在完全复制了代码,但是当我运行服务器时,将客户端连接到服务器并发送消息从客户端发生的事情是它无限地将消息发送到客户端和服务器。我不确定为什么会这样,因为代码与我观看的视频中显示的完全一样。你可以在我的repository看到这个。

如果您想查看代码: 客户:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SimpleTCP;

namespace Client
{
    public partial class ClientForm : Form
    {
        SimpleTcpClient client;

        public ClientForm()
        {
            InitializeComponent();
        }

        private void ClientForm_Load(object sender, EventArgs e)
        {
            client = new SimpleTcpClient();
            client.StringEncoder = Encoding.UTF8;
            client.DataReceived += Client_DataReceived;
        }

        private void Client_DataReceived(Object sender, SimpleTCP.Message e)
        {
            txtStatus.Invoke((MethodInvoker)delegate ()
            {
                txtStatus.Text += e.MessageString;
                e.ReplyLine(String.Format("You: {0}", e.MessageString));
            });
        }

        private void SendMessageInput_Click(object sender, EventArgs e)
        {
            client.WriteLine(MessageInput.Text);
        }

        private void StartInput_Click(object sender, EventArgs e)
        {
            StartInput.Enabled = false;
            client.Connect(HostInput.Text, Convert.ToInt32(PortInput.Text));
        }
    }
}

这就是服务器:

using System;
using System.Net;
using System.Text;
using System.Windows.Forms;
using SimpleTCP;

namespace HyperChat
{
    public partial class ServerForm : Form
    {
        SimpleTcpServer server;

        public ServerForm()
        {
            InitializeComponent();
        }

        private void ServerForm_Load(object sender, EventArgs e)
        {
            server = new SimpleTcpServer();
            server.Delimiter = 0x13;
            server.StringEncoder = Encoding.UTF8;
            server.DataReceived += Server_DataReceived;
        }

        private void Server_DataReceived(object sender, SimpleTCP.Message e)
        {
            txtStatus.Invoke((MethodInvoker)delegate ()
            {
                txtStatus.Text += e.MessageString;
                e.ReplyLine(String.Format("You: {0}", e.MessageString));
            });
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            txtStatus.Text += "Server Started...";
            IPAddress ip = IPAddress.Parse(hostInput.Text);
            server.Start(ip, Convert.ToInt32(portInput.Text));
            StartButton.Enabled = false;
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            if (server.IsStarted)
            {
                server.Stop();
            }
        }
    }
}

非常感谢获得帮助,我尝试弄乱我的代码,但它并没有真正奏效,我想提一下,在视频中他们说你需要连接才能获得要连接的 IP:

System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(txtHost.Text))

但它会给出错误,而您真正需要做的是:

System.Net.IPAddress ip = System.Net.IPAddress.Parse(hostInput.Text);

我期望发生的是服务器按应有的方式发送消息,但由于某种原因它没有发送。 我真的很感激能得到这方面的帮助。谢谢。

【问题讨论】:

    标签: c# .net web chat


    【解决方案1】:

    问题是您在客户端和服务器中都有e.ReplyLine…,将其从客户端中删除,您的代码应该可以工作。

    发生的情况是客户端发送一条消息,然后服务器接收并发送回复,客户端收到回复,如果您的代码向服务器发送回复,这导致服务器接收回复并发送回复等等......

    【讨论】:

      【解决方案2】:

      所以@Chris Taylor 解释了这个问题的解决方案,正如他所说,我所做的是客户端向服务器发送了一条消息,当服务器收到一条消息时,他回复一条消息,客户端回复导致无限循环的回复。解决这个问题的方法是删除客户端回复服务器的行并防止循环。 我真的很感激你的帮助,谢谢克里斯。

      【讨论】:

        猜你喜欢
        • 2023-02-25
        • 2021-07-01
        • 1970-01-01
        • 2021-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-15
        • 1970-01-01
        相关资源
        最近更新 更多