Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
C#
namespace ServerSide
{

    public partial class Form1 : Form
    {
        static TcpListener tcpListener = new TcpListener(11);

        public Form1()
        {

            InitializeComponent();
            tcpListener.Start();
            //Console.WriteLine("************This is Server program************");
            //Console.WriteLine("************START************");
            txt_IP.Text = Dns.GetHostAddresses("localhost").ToString();
            Thread newThread = new Thread(new ThreadStart(Listeners));
            newThread.Start();
            txt_Stream.Enabled = false;


        }


        /////////////////
        void Listeners()
        {

            Socket socketForClient = tcpListener.AcceptSocket();
            string ipaddress = socketForClient.RemoteEndPoint.ToString();
            string ipdispaddress = socketForClient.LocalEndPoint.ToString();
            //this.lbl_DisplayAddress.Text = ipaddress.ToString();
            if (socketForClient.Connected)
            {
                //Console.WriteLine("Client:" + socketForClient.RemoteEndPoint + " now connected to server.");
                NetworkStream networkStream = new NetworkStream(socketForClient);
                System.IO.StreamWriter streamWriter =
                new System.IO.StreamWriter(networkStream);
                System.IO.StreamReader streamReader =
                new System.IO.StreamReader(networkStream);
                //here we recieve client's text if any.
                while (true)
                {
                    string theString = streamReader.ReadLine();
                    //Console.WriteLine("Message recieved by client:" + theString);
                    string msg = theString;
                    AppendTextBox(msg);
                    AppendIPLabel(ipaddress);
                    AppendDispIPLabel(ipdispaddress);
                    //this.txt_Stream.Text = theString.ToString();
                    streamWriter.WriteLine("Got it");
                    streamWriter.Flush();
                    if (theString == "exit")
                        break;
                }

                streamReader.Close();
                streamWriter.Close();
                networkStream.Close();


            }
            socketForClient.Close();
            //Console.WriteLine("Press any key to exit from server program");
            //Console.ReadKey();
        }

        public void AppendTextBox( string theString)
        {
            //string text = txt_Stream.Text.ToString();
            //theString = text + System.Environment.NewLine + theString;
            if (InvokeRequired)
            {
                this.Invoke(new Action<string>(AppendTextBox), new object[] { theString });
                return;
            }

            this.lbl_BayNo.Text = theString.ToString();
        }

        public void AppendDispIPLabel(string ipDispaddress)
        {
            //string text = txt_Stream.Text.ToString();
            //theString = text + System.Environment.NewLine + theString;
            if (InvokeRequired)
            {
                this.Invoke(new Action<string>(AppendDispIPLabel), new object[] { ipDispaddress });
                return;
            }
            this.lbl_DisplayAddress.Text = ipDispaddress.ToString();
            
        }
                public void AppendIPLabel(string ipaddress)
                {
                    //string text = txt_Stream.Text.ToString();
                    //theString = text + System.Environment.NewLine + theString;
                    if (InvokeRequired)
                    {
                        this.Invoke(new Action<string>(AppendIPLabel), new object[] { ipaddress });
                        return;
                    }
                    this.lbl_ConnectedDeviceId.Text = ipaddress.ToString();

                }





    }
}
Posted
Updated 23-Jul-15 20:53pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Jul-15 0:38am    
On what platforms does it work? What's the Mono version? Normally, you can even compile the project on .NET, it should run on all platforms, but only if your project does not go beyond Mono compatibility. There is a compatibility tool which comes with Mono. Did you use it?
—SA

1 solution

When you are using network library like TcpListener or HttpListener code you need to have permissions so run the following :
sudo mono yourprogram.exe
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900