using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; namespace chatCli { public partial class Client : Form { private bool bConnected; static MyQueue sms; static Thread listenerThread; static TcpListener listener; // constructor public Client() { InitializeComponent(); bConnected = false; sms = new MyQueue(); listener = null; } protected void ListenerThread() { if (bConnected) return; int port = 0; string server = tb_dns_ip.Text; if (port_c.Text == "") { MessageBox.Show("You must enter a port number.", "Port Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } while (port < 2000 || port > 0xfffe) { try { port = Convert.ToInt32(port_c.Text); if (port < 2000 || port > 0xfffe) { MessageBox.Show("Port number must be:\n1999 < n < 0xfffe", "Port Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); port_c.Text = ""; port = 0; } } catch { labelConnected.Text = "Port is incorrect"; return; } } TcpClient client = new TcpClient(); NetworkStream stream = null; try { client.Connect(server, port); stream = client.GetStream(); string syn = "$Birdie 2 nest" + "$" + tb_nick.Text; byte[] buffer = new System.Text.UTF8Encoding(true).GetBytes(syn); stream.Write(buffer, 0, syn.Length); stream.Flush(); stream.Close(); // userMsg.Text = Encoding.Unicode.GetString(buffer); } catch { labelConnected.Text = "Conn failed"; return; } } private void buttonConnect_Click(object sender, EventArgs e) { listenerThread = new Thread(new ThreadStart(ListenerThread)); listenerThread.IsBackground = true; listenerThread.Name = "Listener"; listenerThread.Start(); } } // EOC // ---------------------------- internal class MyQueue : Queue { public override string ToString() { string result = ""; foreach (string mystr in this) result += mystr; return result; } } }