Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
C# GUI Chat Server and Client   Open Visual Studio C# 2010 Express and create a Windows Form project called ChatServerGUI.     This creates the empty Form1 window.     Enlarge the Form1 window and add a multi-line TextBox (called textBox1), a single-line TextBox (called textBox2) and a Button (called button1 with send as its text). When complete it should look like.     Double click on the button to open the code behind the button (the code that will be called when the button is clicked).     Modify the application�s code to the following:-   using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Threading;   namespace ChatServerGUI {     public partial class Form1 : Form     {         delegate void SetTextCallback(string text);         TcpListener listener;         TcpClient client;         NetworkStream ns;         Thread t = null;           public Form1()         {             InitializeComponent();               listener = new TcpListener(4545);             listener.Start();             client = listener.AcceptTcpClient();             ns = client.GetStream();             t = new Thread(DoWork);             t.Start();         }           private void button1_Click(object sender, EventArgs e)         {             String s = textBox2.Text;             byte[] byteTime = Encoding.ASCII.GetBytes(s);             ns.Write(byteTime, 0, byteTime.Length);         }           // This is run as a thread         public void DoWork()         {             byte[] bytes = new byte[1024];             while (true)             {                 int bytesRead = ns.Read(bytes, 0, bytes.Length);                 this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));                 //MessageBox.Show(Encoding.ASCII.GetString(bytes, 0, bytesRead));             }         }         private void SetText(string text)         {             // InvokeRequired required compares the thread ID of the             // calling thread to the thread ID of the creating thread.      // If these threads are different, it returns true.             if (this.textBox1.InvokeRequired)             {                 SetTextCallback d = new SetTextCallback(SetText);                 this.Invoke(d, new object[] { text });             }             else             {                 this.textBox1.Text = this.textBox1.Text + text;             }         }     } }   Run the application. You may have to allow the application through the Windows Firewall. Note that the Server�s GUI will not appear until the Client connects.       Minimise Visual Studio and start another instance of Visual Studio so you can create the Client. Create a new �Windows Form� project called ChatClientGUI.       Create exactly the same GUI as you did for the Server.     Double click on the button to open the code window.     Modify the code to the following:-   using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Threading;   namespace ChatClientGUI {     public partial class Form1 : Form     {         private const int portNum = 4545;         delegate void SetTextCallback(string text);           TcpClient client;         NetworkStream ns;         Thread t = null;         private const string hostName = "localhost";           public Form1()         {             InitializeComponent();             client = new TcpClient(hostName, portNum);             ns = client.GetStream();             String s = "Connected";             byte[] byteTime = Encoding.ASCII.GetBytes(s);             ns.Write(byteTime, 0, byteTime.Length);             t = new Thread(DoWork);             t.Start();         }           private void button1_Click(object sender, EventArgs e)         {             String s = textBox2.Text;             byte[] byteTime = Encoding.ASCII.GetBytes(s);             ns.Write(byteTime, 0, byteTime.Length);         }           // This is run as a thread         public void DoWork()         {             byte[] bytes = new byte[1024];             while (true)             {                 int bytesRead = ns.Read(bytes, 0, bytes.Length);                 this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));   //MessageBox.Show(Encoding.ASCII.GetString(bytes, 0, bytesRead));             }         }         private void SetText(string text)         {             // InvokeRequired required compares the thread ID of the      // calling thread to the thread ID of the creating thread.      // If these threads are different, it returns true.             if (this.textBox1.InvokeRequired)             {                 SetTextCallback d = new SetTextCallback(SetText);                 this.Invoke(d, new object[] { text });             }             else             {                 this.textBox1.Text = this.textBox1.Text + text;             }         }     } }   Run the client application. It should connect to the Server application so you can exchange messages.