dimanche 24 mars 2019

A Problem With The Module "SimpleTCP" When Making a Chat Application

So I wanted to make a chat app in C#, I watched a video about it(https://www.youtube.com/watch?v=ve2LX1tOwIM), I completely copied the code for now but when I run the server, Connect the client to the server and send a message from the client what happen is that it infinitely sends the message to both client and server. I am not sure why it does that since the code is exactly like shown in the video I watched. You can see this in my repository.

In case you want to see the code: Client:

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));
        }
    }
}

And that is the Server:

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();
            }
        }
    }
}

I would really appreciate getting help, I tried messing up with my code but it didn't really work, I do want to mention that in the video they said you need to connect to get the IP you want to connect using:

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

But it gives errors and what you really need to do is:

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

What I expect to happen is the server to send the message like it should but for some reason it doesn't. I would really appreciate getting help with this. Thanks.




Aucun commentaire:

Enregistrer un commentaire