0

My Client Source Code :

public partial class Form1 : Form
{

    string serverip = "localHost";
    int port = 160;


    public Form1()
    {
        InitializeComponent();
    }

    private void Submit_Click(object sender, EventArgs e)
    {

        TcpClient client = new TcpClient(serverip, port);

        int byteCount = Encoding.ASCII.GetByteCount(Message.Text);

        byte[] sendData = new byte[byteCount];

        sendData = Encoding.ASCII.GetBytes(Message.Text);

        NetworkStream stream = client.GetStream();

        stream.Write(sendData, 0, sendData.Length);

        stream.Close();
        client.Close();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

My server

class Program
{
    static void Main(string[] args)
    {
        IPAddress ip = Dns.GetHostEntry("localHost").AddressList[0];
        TcpListener server = new TcpListener(ip, 160);
        TcpClient client = default(TcpClient);

        try
        {
            server.Start();
            Console.WriteLine("The server has started successfully");

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadLine();
        }

        while (true)
        {
            client = server.AcceptTcpClient();

            byte[] receivedBuffer = new byte[100];
            NetworkStream stream = client.GetStream();

            stream.Read(receivedBuffer, 0, receivedBuffer.Length);

            StringBuilder msg = new StringBuilder();

            foreach (byte b in receivedBuffer)
            {
                if (b.Equals(59))
                {
                    break;
                }
                else
                {
                    msg.Append(Convert.ToChar(b).ToString());
                }
            }

            Console.WriteLine(msg.ToString() + msg.Length);

        }
    }
}

Essentially i want make it so i can have multiple clients on the server sending a message from different ip addresses of course. I have been using c# a year now mostly in school and I am above average at best at it. First time asking question so sorry if its in wrong format

Rasul
  • 161
  • 1
  • 5
  • 4
    Possible duplicate of [how do i get TcpListener to accept multiple connections and work with each one individually?](https://stackoverflow.com/questions/5339782/how-do-i-get-tcplistener-to-accept-multiple-connections-and-work-with-each-one-i) – Hackerman Dec 13 '17 at 13:25
  • Consider using sockets instead. – Mrg Gek Dec 13 '17 at 15:26

0 Answers0