Trying to make a simple chat on local network with TcpClient and TcpListener in C#

49 views Asked by At

This is my code. when running this on 2 different computers they just can't connect one to the other

Code for computer 1

using System;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Chat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Receive()
        {
            TcpListener list = new TcpListener(IPAddress.Any, 1234);
            list.Start();
            TcpClient client = list.AcceptTcpClient();
            StreamReader sr = new StreamReader(client.GetStream());
            while (true)
            {
                string line = sr.ReadLine();
                listBox1.Invoke(new Action(() => listBox1.Items.Add(line + " ")));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread server = new Thread(Receive);
            server.Start();
            button1.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.ExitThread();
            Environment.Exit(0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TcpClient client = new TcpClient(textBox1.Text, 1235);
            byte[] messageByte = Encoding.UTF8.GetBytes(textBox2.Text);
            listBox1.Items.Add(textBox2.Text);
            Stream s = client.GetStream();
            s.Write(messageByte, 0, messageByte.Length);
            client.Close();
        }
    }
}

Code for computer 2

using System;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Chat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Receive()
        {
            TcpListener list = new TcpListener(IPAddress.Any, 1235);
            list.Start();
            TcpClient client = list.AcceptTcpClient();
            StreamReader sr = new StreamReader(client.GetStream());
            while (true)
            {
                string line = sr.ReadLine();
                listBox1.Invoke(new Action(() => listBox1.Items.Add(line + " ")));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread server = new Thread(Receive);
            server.Start();
            button1.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.ExitThread();
            Environment.Exit(0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TcpClient client = new TcpClient(textBox1.Text, 1234);
            byte[] messageByte = Encoding.UTF8.GetBytes(textBox2.Text);
            listBox1.Items.Add(textBox2.Text);
            Stream s = client.GetStream();
            s.Write(messageByte, 0, messageByte.Length);
            client.Close();
        }
    }
}

As you can see, there are different ports when reading and writing text. Laptop 1 writes on 1234 and reads on 1235 Laptop 2 writes on 1235 and reads on 1234 Scanned each laptop with nmap, in case TcpListener can't create local server to listen. Laptop 1 cant find Laptop 2 and vice versa. Is this a network problem and not a programming one?

0

There are 0 answers