public partial class Form1 : Form { int counter = 0; public Form1() { InitializeComponent(); } Socket clsock; private void button1_Click(object sender, EventArgs e) { Thread th = new Thread(() => StartClient()); th.Start(); } public void StartClient() { try { clsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int port = Convert.ToInt32(txtport.Text); IPAddress ip = IPAddress.Parse(txtip.Text); clsock.Connect(ip, port); counter += 1; AppendlblData(Convert.ToString(counter) + " Server Connected " + clsock.RemoteEndPoint + Environment.NewLine); Thread th = new Thread(() => ReceiveData()); th.Start(); } catch { } } public void ReceiveData() { try { for (int i = 0; i >= 0; i++) { byte[] data = new byte[128]; clsock.Receive(data); string dataReceived = Encoding.ASCII.GetString(data); char[] c = { '*', '$', '!', '@' }; if (dataReceived != null) { string res = dataReceived.TrimStart(c); MessageBox.Show(res); // SendKeys.SendWait(res); } } } catch(Exception es) { if (clsock.Connected !=true) { StopClient(); AppendlblData("Connection closed from Server"); } } } private void Form1_Load(object sender, EventArgs e) { MaximizeBox = false; } public void AppendlblData(string myval) { if (InvokeRequired) { this.Invoke(new Action<string>(AppendlblData), new object[] { myval }); return; } richtxtbox.Text += myval; } private void button2_Click(object sender, EventArgs e) { StopClient(); } public void StopClient() { try { AppendlblData("Disconnected from Server -" + clsock.RemoteEndPoint + Environment.NewLine); clsock.Close(); } catch { } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)