0

I'm learning to code and I'm currently making a server and a client with two-way communication. Is there a way to make the received message appear on the left side of the textbox and the sent message on the right side of the textbox just like in a real chat service. Is there also a way to add the timestamp, when the message was received and when it was sent?

I will attach all the code that I have written and images of the client and server windows.

Server Client

Server code:

namespace Full_Chatt_Server
{
    public partial class Form1 : Form
    {
        TcpListener listener;
        TcpClient klient;
        int port;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStartServer_Click(object sender, EventArgs e)
        {
            btnTaEmot.Enabled = false;

            port = int.Parse(tbxPort.Text);

            try
            {
                listener = new TcpListener(IPAddress.Any, port);
                listener.Start();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, Text);
                return;
            }

            StartAccepting();
        }

        public async void StartAccepting()
        {
            try
            {
                klient = await listener.AcceptTcpClientAsync();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, Text);
                return;
            }

            StartReading(klient);
        }

        public async void StartReading(TcpClient k)
        {
            byte[] buffer = new byte[1024];

            int n = 0;
            try
            {
                n = await k.GetStream().ReadAsync(buffer, 0, 1024);
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, Text);
                return;
            }

            tbxMessage.AppendText(Encoding.Unicode.GetString(buffer, 0, n) + "\r\n");

            StartReading(k);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string chatt = tbxChatt.Text;
            StartSending(chatt);
            tbxChatt.Clear();
            tbxMessage.AppendText(chatt + "\r\n");
        }

        public async void StartSending(string message)
        {
            if (klient.Connected)
            {
                byte[] OutStream = Encoding.Unicode.GetBytes(message);

                try
                {
                    await klient.GetStream().WriteAsync(OutStream, 0, OutStream.Length);
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message, Text);
                    return;
                }
            }
        }
    }

Client code:

namespace Full_Chatt_Klient
{
    public partial class Form1 : Form
    {
        TcpClient klient = new TcpClient();
        int port;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (!klient.Connected)
            {
                Connect();
            }
        }

        public async void Connect()
        {
            IPAddress address = IPAddress.Parse(tbxIP.Text);
            port = int.Parse(tbxPort.Text);

            try
            {
                await klient.ConnectAsync(address, port);
                StartReading(klient);
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, Text);
                return;
            }

            btnSkicka.Enabled = true;
            btnAnslut.Enabled = false;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string chatt = tbxChatt.Text;
            StartSending(chatt);
            tbxChatt.Clear();
            tbxMessage.AppendText(chatt + "\r\n");
        }

        public async void StartSending(string message)
        {
            if (klient.Connected)
            {
                byte[] OutStream = Encoding.Unicode.GetBytes(message);

                try
                {
                    await klient.GetStream().WriteAsync(OutStream, 0, OutStream.Length);
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message, Text);
                    return;
                }
            }
        }

        public async void StartReading(TcpClient k)
        {
            byte[] buffer = new byte[1024];

            int n = 0;
            try
            {
                n = await k.GetStream().ReadAsync(buffer, 0, 1024);
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, Text);
                return;
            }

            tbxMessage.AppendText(Encoding.Unicode.GetString(buffer, 0, n) + "\r\n");

            StartReading(k);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (klient != null)
            {
                klient.Close();
            }
        }
    }
}
Erhan Yaşar
  • 315
  • 1
  • 4
  • 19
EricC
  • 1
  • 1
  • It doesn't look like you actually tried anything. What's stopping you from just adding it to tbxMessage just before dumping the buffer contents? – Nyerguds Jan 19 '18 at 12:25
  • Do note you have an infinite call loop in `StartReading` that will eventually lead to a buffer overflow. You should put that in a loop, not make it call itself. You can abort the loop by checking on the status of the `TcpClient`, or even just check on `null` and make it `null` in your `FormClosed` code. – Nyerguds Jan 19 '18 at 12:26

0 Answers0