// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Drawing; using System.Windows.Forms; using System.Threading; namespace DefaultNamespace { public class MainForm : Form { private TextBox msgs; private TextBox sendmsg; private Button send; private TcpClient client; private StreamReader rdr; private StreamWriter wrt; public MainForm() { InitializeComponent(); client = new TcpClient("localhost", 1234); rdr = new StreamReader(client.GetStream()); wrt = new StreamWriter(client.GetStream()); (new Thread(new ThreadStart(Reader))).Start(); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } private void InitializeComponent() { sendmsg = new TextBox(); msgs = new TextBox(); send = new Button(); SuspendLayout(); msgs.Location = new Point(50, 50); msgs.Multiline = true; msgs.Size = new Size(500, 300); sendmsg.Location = new Point(50, 400); sendmsg.Size = new Size(500, 25); send.Location = new Point(50, 450); send.Size = new Size(500, 25); send.Text = "Send"; send.Click += new System.EventHandler(SendClick); ClientSize = new Size(600, 525); Controls.Add(sendmsg); Controls.Add(msgs); Controls.Add(send); Text = "Chat (TCP/IP client)"; ResumeLayout(false); } void SendClick(object sender, System.EventArgs e) { wrt.WriteLine(sendmsg.Text); wrt.Flush(); lock(msgs) { msgs.Text += ("mig: " + sendmsg.Text + "\r\n"); } } private void Reader() { string line; while((line = rdr.ReadLine()) != null) { lock(msgs) { msgs.Text += ("dig: " + line + "\r\n"); } } } } }