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(); } } }
TcpListener
HttpListener
sudo mono yourprogram.exe
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)