|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Send message like Yahoo Messenger??How can I send from myApp, over the internet with c#.net, a message to
another instance of myApp?? Like Yahoo or MSN? Thanks, Trint Hi,
Two options that I can think at the moment: 1) Using a socket connection, so you have a destination IP using a certain port, The other application have to create a socket and listen to that port. Everytime a message arrives the destination application can read the object and do whatever you like. This option can allow you better control 2) Using remoting, you can use HTTP or TCP remoting to send messages between applications, the good thing is that you can use port 80 if you use HTTP so no new ports are opened improving the security. Hope this helps Salva Show quote "trint" wrote: > How can I send from myApp, over the internet with c#.net, a message to > another instance of myApp?? Like Yahoo or MSN? > Thanks, > Trint > > Ok, I think I get it now, but is this kinda the road to go?:
<server> using System; using System.Collections; using System.Threading; using System.Windows.Forms; using System.Net.Sockets; public class frmMain : System.Windows.Forms.Form { #region " Windows Form Designer generated code " /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new frmMain()); } public frmMain() { //This call is required by the Windows Form Designer. InitializeComponent(); //Add any initialization after the InitializeComponent() call // So that we only need to set the title of the application once, // we use the AssemblyInfo class (defined in the AssemblyInfo.cs file) // to read the AssemblyTitle attribute. AssemblyInfo ainfo = new AssemblyInfo(); this.Text = ainfo.Title; this.mnuAbout.Text = string.Format("&About {0} ...", ainfo.Title); } //Form overrides dispose to clean up the component list. protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } //Required by the Windows Form Designer private System.ComponentModel.IContainer components = null; //NOTE: The following procedure is required by the Windows Form Designer //It can be modified using the Windows Form Designer. //Do not modify it using the code editor. private System.Windows.Forms.MainMenu mnuMain; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuExit; private System.Windows.Forms.MenuItem mnuHelp; private System.Windows.Forms.MenuItem mnuAbout; private System.Windows.Forms.ListBox lstStatus; private System.Windows.Forms.Button btnBroadcast; private System.Windows.Forms.TextBox txtBroadcast; private System.Windows.Forms.Label lblInstructions; private System.Windows.Forms.Label Label1; private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain)); this.mnuMain = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuExit = new System.Windows.Forms.MenuItem(); this.mnuHelp = new System.Windows.Forms.MenuItem(); this.mnuAbout = new System.Windows.Forms.MenuItem(); this.lstStatus = new System.Windows.Forms.ListBox(); this.txtBroadcast = new System.Windows.Forms.TextBox(); this.btnBroadcast = new System.Windows.Forms.Button(); this.lblInstructions = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // mnuMain // this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuHelp}); this.mnuMain.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("mnuMain.RightToLeft"))); // // mnuFile // this.mnuFile.Enabled = ((bool)(resources.GetObject("mnuFile.Enabled"))); this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuExit}); this.mnuFile.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuFile.Shortcut"))); this.mnuFile.ShowShortcut = ((bool)(resources.GetObject("mnuFile.ShowShortcut"))); this.mnuFile.Text = resources.GetString("mnuFile.Text"); this.mnuFile.Visible = ((bool)(resources.GetObject("mnuFile.Visible"))); // // mnuExit // this.mnuExit.Enabled = ((bool)(resources.GetObject("mnuExit.Enabled"))); this.mnuExit.Index = 0; this.mnuExit.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuExit.Shortcut"))); this.mnuExit.ShowShortcut = ((bool)(resources.GetObject("mnuExit.ShowShortcut"))); this.mnuExit.Text = resources.GetString("mnuExit.Text"); this.mnuExit.Visible = ((bool)(resources.GetObject("mnuExit.Visible"))); this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click); // // mnuHelp // this.mnuHelp.Enabled = ((bool)(resources.GetObject("mnuHelp.Enabled"))); this.mnuHelp.Index = 1; this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuAbout}); this.mnuHelp.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuHelp.Shortcut"))); this.mnuHelp.ShowShortcut = ((bool)(resources.GetObject("mnuHelp.ShowShortcut"))); this.mnuHelp.Text = resources.GetString("mnuHelp.Text"); this.mnuHelp.Visible = ((bool)(resources.GetObject("mnuHelp.Visible"))); // // mnuAbout // this.mnuAbout.Enabled = ((bool)(resources.GetObject("mnuAbout.Enabled"))); this.mnuAbout.Index = 0; this.mnuAbout.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuAbout.Shortcut"))); this.mnuAbout.ShowShortcut = ((bool)(resources.GetObject("mnuAbout.ShowShortcut"))); this.mnuAbout.Text = resources.GetString("mnuAbout.Text"); this.mnuAbout.Visible = ((bool)(resources.GetObject("mnuAbout.Visible"))); this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click); // // lstStatus // this.lstStatus.AccessibleDescription = resources.GetString("lstStatus.AccessibleDescription"); this.lstStatus.AccessibleName = resources.GetString("lstStatus.AccessibleName"); this.lstStatus.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("lstStatus.Anchor"))); this.lstStatus.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("lstStatus.BackgroundImage"))); this.lstStatus.ColumnWidth = ((int)(resources.GetObject("lstStatus.ColumnWidth"))); this.lstStatus.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("lstStatus.Dock"))); this.lstStatus.Enabled = ((bool)(resources.GetObject("lstStatus.Enabled"))); this.lstStatus.Font = ((System.Drawing.Font)(resources.GetObject("lstStatus.Font"))); this.lstStatus.HorizontalExtent = ((int)(resources.GetObject("lstStatus.HorizontalExtent"))); this.lstStatus.HorizontalScrollbar = ((bool)(resources.GetObject("lstStatus.HorizontalScrollbar"))); this.lstStatus.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("lstStatus.ImeMode"))); this.lstStatus.IntegralHeight = ((bool)(resources.GetObject("lstStatus.IntegralHeight"))); this.lstStatus.ItemHeight = ((int)(resources.GetObject("lstStatus.ItemHeight"))); this.lstStatus.Location = ((System.Drawing.Point)(resources.GetObject("lstStatus.Location"))); this.lstStatus.Name = "lstStatus"; this.lstStatus.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("lstStatus.RightToLeft"))); this.lstStatus.ScrollAlwaysVisible = ((bool)(resources.GetObject("lstStatus.ScrollAlwaysVisible"))); this.lstStatus.Size = ((System.Drawing.Size)(resources.GetObject("lstStatus.Size"))); this.lstStatus.TabIndex = ((int)(resources.GetObject("lstStatus.TabIndex"))); this.lstStatus.Visible = ((bool)(resources.GetObject("lstStatus.Visible"))); // // txtBroadcast // this.txtBroadcast.AccessibleDescription = resources.GetString("txtBroadcast.AccessibleDescription"); this.txtBroadcast.AccessibleName = resources.GetString("txtBroadcast.AccessibleName"); this.txtBroadcast.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("txtBroadcast.Anchor"))); this.txtBroadcast.AutoSize = ((bool)(resources.GetObject("txtBroadcast.AutoSize"))); this.txtBroadcast.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("txtBroadcast.BackgroundImage"))); this.txtBroadcast.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("txtBroadcast.Dock"))); this.txtBroadcast.Enabled = ((bool)(resources.GetObject("txtBroadcast.Enabled"))); this.txtBroadcast.Font = ((System.Drawing.Font)(resources.GetObject("txtBroadcast.Font"))); this.txtBroadcast.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("txtBroadcast.ImeMode"))); this.txtBroadcast.Location = ((System.Drawing.Point)(resources.GetObject("txtBroadcast.Location"))); this.txtBroadcast.MaxLength = ((int)(resources.GetObject("txtBroadcast.MaxLength"))); this.txtBroadcast.Multiline = ((bool)(resources.GetObject("txtBroadcast.Multiline"))); this.txtBroadcast.Name = "txtBroadcast"; this.txtBroadcast.PasswordChar = ((char)(resources.GetObject("txtBroadcast.PasswordChar"))); this.txtBroadcast.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("txtBroadcast.RightToLeft"))); this.txtBroadcast.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("txtBroadcast.ScrollBars"))); this.txtBroadcast.Size = ((System.Drawing.Size)(resources.GetObject("txtBroadcast.Size"))); this.txtBroadcast.TabIndex = ((int)(resources.GetObject("txtBroadcast.TabIndex"))); this.txtBroadcast.Text = resources.GetString("txtBroadcast.Text"); this.txtBroadcast.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("txtBroadcast.TextAlign"))); this.txtBroadcast.Visible = ((bool)(resources.GetObject("txtBroadcast.Visible"))); this.txtBroadcast.WordWrap = ((bool)(resources.GetObject("txtBroadcast.WordWrap"))); // // btnBroadcast // this.btnBroadcast.AccessibleDescription = resources.GetString("btnBroadcast.AccessibleDescription"); this.btnBroadcast.AccessibleName = resources.GetString("btnBroadcast.AccessibleName"); this.btnBroadcast.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("btnBroadcast.Anchor"))); this.btnBroadcast.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnBroadcast.BackgroundImage"))); this.btnBroadcast.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("btnBroadcast.Dock"))); this.btnBroadcast.Enabled = ((bool)(resources.GetObject("btnBroadcast.Enabled"))); this.btnBroadcast.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("btnBroadcast.FlatStyle"))); this.btnBroadcast.Font = ((System.Drawing.Font)(resources.GetObject("btnBroadcast.Font"))); this.btnBroadcast.Image = ((System.Drawing.Image)(resources.GetObject("btnBroadcast.Image"))); this.btnBroadcast.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnBroadcast.ImageAlign"))); this.btnBroadcast.ImageIndex = ((int)(resources.GetObject("btnBroadcast.ImageIndex"))); this.btnBroadcast.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("btnBroadcast.ImeMode"))); this.btnBroadcast.Location = ((System.Drawing.Point)(resources.GetObject("btnBroadcast.Location"))); this.btnBroadcast.Name = "btnBroadcast"; this.btnBroadcast.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("btnBroadcast.RightToLeft"))); this.btnBroadcast.Size = ((System.Drawing.Size)(resources.GetObject("btnBroadcast.Size"))); this.btnBroadcast.TabIndex = ((int)(resources.GetObject("btnBroadcast.TabIndex"))); this.btnBroadcast.Text = resources.GetString("btnBroadcast.Text"); this.btnBroadcast.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnBroadcast.TextAlign"))); this.btnBroadcast.Visible = ((bool)(resources.GetObject("btnBroadcast.Visible"))); this.btnBroadcast.Click += new System.EventHandler(this.btnBroadcast_Click); // // lblInstructions // this.lblInstructions.AccessibleDescription = resources.GetString("lblInstructions.AccessibleDescription"); this.lblInstructions.AccessibleName = resources.GetString("lblInstructions.AccessibleName"); this.lblInstructions.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("lblInstructions.Anchor"))); this.lblInstructions.AutoSize = ((bool)(resources.GetObject("lblInstructions.AutoSize"))); this.lblInstructions.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("lblInstructions.Dock"))); this.lblInstructions.Enabled = ((bool)(resources.GetObject("lblInstructions.Enabled"))); this.lblInstructions.Font = ((System.Drawing.Font)(resources.GetObject("lblInstructions.Font"))); this.lblInstructions.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192))); this.lblInstructions.Image = ((System.Drawing.Image)(resources.GetObject("lblInstructions.Image"))); this.lblInstructions.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lblInstructions.ImageAlign"))); this.lblInstructions.ImageIndex = ((int)(resources.GetObject("lblInstructions.ImageIndex"))); this.lblInstructions.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("lblInstructions.ImeMode"))); this.lblInstructions.Location = ((System.Drawing.Point)(resources.GetObject("lblInstructions.Location"))); this.lblInstructions.Name = "lblInstructions"; this.lblInstructions.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("lblInstructions.RightToLeft"))); this.lblInstructions.Size = ((System.Drawing.Size)(resources.GetObject("lblInstructions.Size"))); this.lblInstructions.TabIndex = ((int)(resources.GetObject("lblInstructions.TabIndex"))); this.lblInstructions.Text = resources.GetString("lblInstructions.Text"); this.lblInstructions.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lblInstructions.TextAlign"))); this.lblInstructions.Visible = ((bool)(resources.GetObject("lblInstructions.Visible"))); // // Label1 // this.Label1.AccessibleDescription = resources.GetString("Label1.AccessibleDescription"); this.Label1.AccessibleName = resources.GetString("Label1.AccessibleName"); this.Label1.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("Label1.Anchor"))); this.Label1.AutoSize = ((bool)(resources.GetObject("Label1.AutoSize"))); this.Label1.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("Label1.Dock"))); this.Label1.Enabled = ((bool)(resources.GetObject("Label1.Enabled"))); this.Label1.Font = ((System.Drawing.Font)(resources.GetObject("Label1.Font"))); this.Label1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192))); this.Label1.Image = ((System.Drawing.Image)(resources.GetObject("Label1.Image"))); this.Label1.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("Label1.ImageAlign"))); this.Label1.ImageIndex = ((int)(resources.GetObject("Label1.ImageIndex"))); this.Label1.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("Label1.ImeMode"))); this.Label1.Location = ((System.Drawing.Point)(resources.GetObject("Label1.Location"))); this.Label1.Name = "Label1"; this.Label1.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("Label1.RightToLeft"))); this.Label1.Size = ((System.Drawing.Size)(resources.GetObject("Label1.Size"))); this.Label1.TabIndex = ((int)(resources.GetObject("Label1.TabIndex"))); this.Label1.Text = resources.GetString("Label1.Text"); this.Label1.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("Label1.TextAlign"))); this.Label1.Visible = ((bool)(resources.GetObject("Label1.Visible"))); // // frmMain // this.AccessibleDescription = resources.GetString("$this.AccessibleDescription"); this.AccessibleName = resources.GetString("$this.AccessibleName"); this.AutoScaleBaseSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScaleBaseSize"))); this.AutoScroll = ((bool)(resources.GetObject("$this.AutoScroll"))); this.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMargin"))); this.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMinSize"))); this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage"))); this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize"))); this.Controls.Add(this.Label1); this.Controls.Add(this.lblInstructions); this.Controls.Add(this.btnBroadcast); this.Controls.Add(this.txtBroadcast); this.Controls.Add(this.lstStatus); this.Enabled = ((bool)(resources.GetObject("$this.Enabled"))); this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font"))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("$this.ImeMode"))); this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location"))); this.MaximizeBox = false; this.MaximumSize = ((System.Drawing.Size)(resources.GetObject("$this.MaximumSize"))); this.Menu = this.mnuMain; this.MinimumSize = ((System.Drawing.Size)(resources.GetObject("$this.MinimumSize"))); this.Name = "frmMain"; this.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("$this.RightToLeft"))); this.StartPosition = ((System.Windows.Forms.FormStartPosition)(resources.GetObject("$this.StartPosition"))); this.Text = resources.GetString("$this.Text"); this.Closing += new System.ComponentModel.CancelEventHandler(this.frmMain_Closing); this.Load += new System.EventHandler(this.frmMain_Load); this.ResumeLayout(false); } #endregion #region " Standard Menu Code " // This code simply shows the About form. private void mnuAbout_Click(object sender, System.EventArgs e) { // Open the About form in Dialog Mode frmAbout frm = new frmAbout(); frm.ShowDialog(this); frm.Dispose(); } // This code will close the form. private void mnuExit_Click(object sender, System.EventArgs e) { // Close the current form this.Close(); } #endregion const int PORT_NUM = 10000; private Hashtable clients = new Hashtable(); private TcpListener listener; private Thread listenerThread; // This subroutine sends a message to all attached clients private void Broadcast(string strMessage) { UserConnection client; // All entries in the clients Hashtable are UserConnection so it is possible // to assign it safely. foreach(DictionaryEntry entry in clients) { client = (UserConnection) entry.Value; client.SendData(strMessage); } } // This subroutine sends the contents of the Broadcast textbox to all clients, if // it is not empty, and clears the textbox private void btnBroadcast_Click(object sender, System.EventArgs e) { if (txtBroadcast.Text != "") { UpdateStatus("Broadcasting: " + txtBroadcast.Text); Broadcast("BROAD|" + txtBroadcast.Text); txtBroadcast.Text = string.Empty; } } // This subroutine checks to see if username already exists in the clients // Hashtable. if it does, send a REFUSE message, otherwise confirm with a JOIN. private void ConnectUser(string userName, UserConnection sender) { if (clients.Contains(userName)) { ReplyToSender("REFUSE", sender); } else { sender.Name = userName; UpdateStatus(userName + " has joined the chat."); clients.Add(userName, sender); // Send a JOIN to sender, and notify all other clients that sender joined ReplyToSender("JOIN", sender); SendToClients("CHAT|" + sender.Name + " has joined the chat.", sender); } } // This subroutine notifies other clients that sender left the chat, and removes // the name from the clients Hashtable private void DisconnectUser(UserConnection sender) { UpdateStatus(sender.Name + " has left the chat."); SendToClients("CHAT|" + sender.Name + " has left the chat.", sender); clients.Remove(sender.Name); } // This subroutine is used a background listener thread to allow reading incoming // messages without lagging the user interface. private void DoListen() { try { // Listen for new connections. listener = new TcpListener(System.Net.IPAddress.Any, PORT_NUM); listener.Start(); do { // Create a new user connection using TcpClient returned by // TcpListener.AcceptTcpClient() UserConnection client = new UserConnection(listener.AcceptTcpClient()); // Create an event handler to allow the UserConnection to communicate // with the window. client.LineReceived += new LineReceive(OnLineReceived); //AddHandler client.LineReceived, AddressOf OnLineReceived; UpdateStatus("new connection found: waiting for log-in"); }while(true); } catch{ } } // When the window closes, stop the listener. private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e) //base.Closing; { listener.Stop(); } // Start the background listener thread. private void frmMain_Load(object sender, System.EventArgs e) { listenerThread = new Thread(new ThreadStart(DoListen)); listenerThread.Start(); UpdateStatus("Listener started"); } // Concatenate all the client names and send them to the user who requested user list private void ListUsers(UserConnection sender) { UserConnection client; string strUserList; UpdateStatus("Sending " + sender.Name + " a list of users online."); strUserList = "LISTUSERS"; // All entries in the clients Hashtable are UserConnection so it is possible // to assign it safely. foreach(DictionaryEntry entry in clients) { client = (UserConnection) entry.Value; strUserList = strUserList + "|" + client.Name; } // Send the list to the sender. ReplyToSender(strUserList, sender); } // This is the event handler for the UserConnection when it receives a full line. // Parse the cammand and parameters and take appropriate action. private void OnLineReceived(UserConnection sender, string data) { string[] dataArray; // Message parts are divided by "|" Break the string into an array accordingly. dataArray = data.Split((char) 124); // dataArray(0) is the command. switch( dataArray[0]) { case "CONNECT": ConnectUser(dataArray[1], sender); break; case "CHAT": SendChat(dataArray[1], sender); break; case "DISCONNECT": DisconnectUser(sender); break; case "REQUESTUSERS": ListUsers(sender); break; default: UpdateStatus("Unknown message:" + data); break; } } // This subroutine sends a response to the sender. private void ReplyToSender(string strMessage, UserConnection sender) { sender.SendData(strMessage); } // Send a chat message to all clients except sender. private void SendChat(string message, UserConnection sender) { UpdateStatus(sender.Name + ": " + message); SendToClients("CHAT|" + sender.Name + ": " + message, sender); } // This subroutine sends a message to all attached clients except the sender. private void SendToClients(string strMessage, UserConnection sender) { UserConnection client; // All entries in the clients Hashtable are UserConnection so it is possible // to assign it safely. foreach(DictionaryEntry entry in clients) { client = (UserConnection) entry.Value; // Exclude the sender. if (client.Name != sender.Name) { client.SendData(strMessage); } } } // This subroutine adds line to the Status listbox private void UpdateStatus(string statusMessage) { lstStatus.Items.Add(statusMessage); } } <client> using System; using System.IO; using System.Windows.Forms; using System.Net.Sockets; using System.Text; public class frmMain : System.Windows.Forms.Form { #region " Windows Form Designer generated code " /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new frmMain()); } public frmMain() { //This call is required by the Windows Form Designer. InitializeComponent(); //Add any initialization after the InitializeComponent() call // So that we only need to set the title of the application once, // we use the AssemblyInfo class (defined in the AssemblyInfo.cs file) // to read the AssemblyTitle attribute. AssemblyInfo ainfo = new AssemblyInfo(); this.Text = ainfo.Title; this.mnuAbout.Text = string.Format("&About {0} ...", ainfo.Title); } //Form overrides dispose to clean up the component list. protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } //Required by the Windows Form Designer private System.ComponentModel.IContainer components = null; //NOTE: The following procedure is required by the Windows Form Designer //It can be modified using the Windows Form Designer. //Do not modify it using the code editor. private System.Windows.Forms.MainMenu mnuMain; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuExit; private System.Windows.Forms.MenuItem mnuHelp; private System.Windows.Forms.MenuItem mnuAbout; private System.Windows.Forms.TabControl TabControl1; private System.Windows.Forms.TextBox txtDisplay; private System.Windows.Forms.TextBox txtSend; private System.Windows.Forms.Button btnSend; private System.Windows.Forms.TabPage tpChatPage; private System.Windows.Forms.TabPage tpListUsers; private System.Windows.Forms.ListBox lstUsers; private System.Windows.Forms.Button btnListUsers; private System.Windows.Forms.Label lblInstructions; private System.Windows.Forms.Label Label1; private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain)); this.mnuMain = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuExit = new System.Windows.Forms.MenuItem(); this.mnuHelp = new System.Windows.Forms.MenuItem(); this.mnuAbout = new System.Windows.Forms.MenuItem(); this.TabControl1 = new System.Windows.Forms.TabControl(); this.tpChatPage = new System.Windows.Forms.TabPage(); this.lblInstructions = new System.Windows.Forms.Label(); this.btnSend = new System.Windows.Forms.Button(); this.txtSend = new System.Windows.Forms.TextBox(); this.txtDisplay = new System.Windows.Forms.TextBox(); this.tpListUsers = new System.Windows.Forms.TabPage(); this.Label1 = new System.Windows.Forms.Label(); this.btnListUsers = new System.Windows.Forms.Button(); this.lstUsers = new System.Windows.Forms.ListBox(); this.TabControl1.SuspendLayout(); this.tpChatPage.SuspendLayout(); this.tpListUsers.SuspendLayout(); this.SuspendLayout(); // // mnuMain // this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuHelp}); this.mnuMain.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("mnuMain.RightToLeft"))); // // mnuFile // this.mnuFile.Enabled = ((bool)(resources.GetObject("mnuFile.Enabled"))); this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuExit}); this.mnuFile.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuFile.Shortcut"))); this.mnuFile.ShowShortcut = ((bool)(resources.GetObject("mnuFile.ShowShortcut"))); this.mnuFile.Text = resources.GetString("mnuFile.Text"); this.mnuFile.Visible = ((bool)(resources.GetObject("mnuFile.Visible"))); // // mnuExit // this.mnuExit.Enabled = ((bool)(resources.GetObject("mnuExit.Enabled"))); this.mnuExit.Index = 0; this.mnuExit.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuExit.Shortcut"))); this.mnuExit.ShowShortcut = ((bool)(resources.GetObject("mnuExit.ShowShortcut"))); this.mnuExit.Text = resources.GetString("mnuExit.Text"); this.mnuExit.Visible = ((bool)(resources.GetObject("mnuExit.Visible"))); this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click); // // mnuHelp // this.mnuHelp.Enabled = ((bool)(resources.GetObject("mnuHelp.Enabled"))); this.mnuHelp.Index = 1; this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuAbout}); this.mnuHelp.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuHelp.Shortcut"))); this.mnuHelp.ShowShortcut = ((bool)(resources.GetObject("mnuHelp.ShowShortcut"))); this.mnuHelp.Text = resources.GetString("mnuHelp.Text"); this.mnuHelp.Visible = ((bool)(resources.GetObject("mnuHelp.Visible"))); // // mnuAbout // this.mnuAbout.Enabled = ((bool)(resources.GetObject("mnuAbout.Enabled"))); this.mnuAbout.Index = 0; this.mnuAbout.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("mnuAbout.Shortcut"))); this.mnuAbout.ShowShortcut = ((bool)(resources.GetObject("mnuAbout.ShowShortcut"))); this.mnuAbout.Text = resources.GetString("mnuAbout.Text"); this.mnuAbout.Visible = ((bool)(resources.GetObject("mnuAbout.Visible"))); this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click); // // TabControl1 // this.TabControl1.AccessibleDescription = resources.GetString("TabControl1.AccessibleDescription"); this.TabControl1.AccessibleName = resources.GetString("TabControl1.AccessibleName"); this.TabControl1.Alignment = ((System.Windows.Forms.TabAlignment)(resources.GetObject("TabControl1.Alignment"))); this.TabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("TabControl1.Anchor"))); this.TabControl1.Appearance = ((System.Windows.Forms.TabAppearance)(resources.GetObject("TabControl1.Appearance"))); this.TabControl1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("TabControl1.BackgroundImage"))); this.TabControl1.Controls.Add(this.tpChatPage); this.TabControl1.Controls.Add(this.tpListUsers); this.TabControl1.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("TabControl1.Dock"))); this.TabControl1.Enabled = ((bool)(resources.GetObject("TabControl1.Enabled"))); this.TabControl1.Font = ((System.Drawing.Font)(resources.GetObject("TabControl1.Font"))); this.TabControl1.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("TabControl1.ImeMode"))); this.TabControl1.ItemSize = ((System.Drawing.Size)(resources.GetObject("TabControl1.ItemSize"))); this.TabControl1.Location = ((System.Drawing.Point)(resources.GetObject("TabControl1.Location"))); this.TabControl1.Name = "TabControl1"; this.TabControl1.Padding = ((System.Drawing.Point)(resources.GetObject("TabControl1.Padding"))); this.TabControl1.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("TabControl1.RightToLeft"))); this.TabControl1.SelectedIndex = 0; this.TabControl1.ShowToolTips = ((bool)(resources.GetObject("TabControl1.ShowToolTips"))); this.TabControl1.Size = ((System.Drawing.Size)(resources.GetObject("TabControl1.Size"))); this.TabControl1.TabIndex = ((int)(resources.GetObject("TabControl1.TabIndex"))); this.TabControl1.Text = resources.GetString("TabControl1.Text"); this.TabControl1.Visible = ((bool)(resources.GetObject("TabControl1.Visible"))); // // tpChatPage // this.tpChatPage.AccessibleDescription = resources.GetString("tpChatPage.AccessibleDescription"); this.tpChatPage.AccessibleName = resources.GetString("tpChatPage.AccessibleName"); this.tpChatPage.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("tpChatPage.Anchor"))); this.tpChatPage.AutoScroll = ((bool)(resources.GetObject("tpChatPage.AutoScroll"))); this.tpChatPage.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("tpChatPage.AutoScrollMargin"))); this.tpChatPage.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("tpChatPage.AutoScrollMinSize"))); this.tpChatPage.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("tpChatPage.BackgroundImage"))); this.tpChatPage.Controls.Add(this.lblInstructions); this.tpChatPage.Controls.Add(this.btnSend); this.tpChatPage.Controls.Add(this.txtSend); this.tpChatPage.Controls.Add(this.txtDisplay); this.tpChatPage.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("tpChatPage.Dock"))); this.tpChatPage.Enabled = ((bool)(resources.GetObject("tpChatPage.Enabled"))); this.tpChatPage.Font = ((System.Drawing.Font)(resources.GetObject("tpChatPage.Font"))); this.tpChatPage.ImageIndex = ((int)(resources.GetObject("tpChatPage.ImageIndex"))); this.tpChatPage.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("tpChatPage.ImeMode"))); this.tpChatPage.Location = ((System.Drawing.Point)(resources.GetObject("tpChatPage.Location"))); this.tpChatPage.Name = "tpChatPage"; this.tpChatPage.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("tpChatPage.RightToLeft"))); this.tpChatPage.Size = ((System.Drawing.Size)(resources.GetObject("tpChatPage.Size"))); this.tpChatPage.TabIndex = ((int)(resources.GetObject("tpChatPage.TabIndex"))); this.tpChatPage.Text = resources.GetString("tpChatPage.Text"); this.tpChatPage.ToolTipText = resources.GetString("tpChatPage.ToolTipText"); this.tpChatPage.Visible = ((bool)(resources.GetObject("tpChatPage.Visible"))); // // lblInstructions // this.lblInstructions.AccessibleDescription = resources.GetString("lblInstructions.AccessibleDescription"); this.lblInstructions.AccessibleName = resources.GetString("lblInstructions.AccessibleName"); this.lblInstructions.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("lblInstructions.Anchor"))); this.lblInstructions.AutoSize = ((bool)(resources.GetObject("lblInstructions.AutoSize"))); this.lblInstructions.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("lblInstructions.Dock"))); this.lblInstructions.Enabled = ((bool)(resources.GetObject("lblInstructions.Enabled"))); this.lblInstructions.Font = ((System.Drawing.Font)(resources.GetObject("lblInstructions.Font"))); this.lblInstructions.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192))); this.lblInstructions.Image = ((System.Drawing.Image)(resources.GetObject("lblInstructions.Image"))); this.lblInstructions.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lblInstructions.ImageAlign"))); this.lblInstructions.ImageIndex = ((int)(resources.GetObject("lblInstructions.ImageIndex"))); this.lblInstructions.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("lblInstructions.ImeMode"))); this.lblInstructions.Location = ((System.Drawing.Point)(resources.GetObject("lblInstructions.Location"))); this.lblInstructions.Name = "lblInstructions"; this.lblInstructions.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("lblInstructions.RightToLeft"))); this.lblInstructions.Size = ((System.Drawing.Size)(resources.GetObject("lblInstructions.Size"))); this.lblInstructions.TabIndex = ((int)(resources.GetObject("lblInstructions.TabIndex"))); this.lblInstructions.Text = resources.GetString("lblInstructions.Text"); this.lblInstructions.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lblInstructions.TextAlign"))); this.lblInstructions.Visible = ((bool)(resources.GetObject("lblInstructions.Visible"))); // // btnSend // this.btnSend.AccessibleDescription = resources.GetString("btnSend.AccessibleDescription"); this.btnSend.AccessibleName = resources.GetString("btnSend.AccessibleName"); this.btnSend.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("btnSend.Anchor"))); this.btnSend.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnSend.BackgroundImage"))); this.btnSend.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("btnSend.Dock"))); this.btnSend.Enabled = ((bool)(resources.GetObject("btnSend.Enabled"))); this.btnSend.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("btnSend.FlatStyle"))); this.btnSend.Font = ((System.Drawing.Font)(resources.GetObject("btnSend.Font"))); this.btnSend.Image = ((System.Drawing.Image)(resources.GetObject("btnSend.Image"))); this.btnSend.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnSend.ImageAlign"))); this.btnSend.ImageIndex = ((int)(resources.GetObject("btnSend.ImageIndex"))); this.btnSend.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("btnSend.ImeMode"))); this.btnSend.Location = ((System.Drawing.Point)(resources.GetObject("btnSend.Location"))); this.btnSend.Name = "btnSend"; this.btnSend.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("btnSend.RightToLeft"))); this.btnSend.Size = ((System.Drawing.Size)(resources.GetObject("btnSend.Size"))); this.btnSend.TabIndex = ((int)(resources.GetObject("btnSend.TabIndex"))); this.btnSend.Text = resources.GetString("btnSend.Text"); this.btnSend.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnSend.TextAlign"))); this.btnSend.Visible = ((bool)(resources.GetObject("btnSend.Visible"))); this.btnSend.Click += new System.EventHandler(this.btnSend_Click); // // txtSend // this.txtSend.AccessibleDescription = resources.GetString("txtSend.AccessibleDescription"); this.txtSend.AccessibleName = resources.GetString("txtSend.AccessibleName"); this.txtSend.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("txtSend.Anchor"))); this.txtSend.AutoSize = ((bool)(resources.GetObject("txtSend.AutoSize"))); this.txtSend.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("txtSend.BackgroundImage"))); this.txtSend.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("txtSend.Dock"))); this.txtSend.Enabled = ((bool)(resources.GetObject("txtSend.Enabled"))); this.txtSend.Font = ((System.Drawing.Font)(resources.GetObject("txtSend.Font"))); this.txtSend.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("txtSend.ImeMode"))); this.txtSend.Location = ((System.Drawing.Point)(resources.GetObject("txtSend.Location"))); this.txtSend.MaxLength = ((int)(resources.GetObject("txtSend.MaxLength"))); this.txtSend.Multiline = ((bool)(resources.GetObject("txtSend.Multiline"))); this.txtSend.Name = "txtSend"; this.txtSend.PasswordChar = ((char)(resources.GetObject("txtSend.PasswordChar"))); this.txtSend.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("txtSend.RightToLeft"))); this.txtSend.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("txtSend.ScrollBars"))); this.txtSend.Size = ((System.Drawing.Size)(resources.GetObject("txtSend.Size"))); this.txtSend.TabIndex = ((int)(resources.GetObject("txtSend.TabIndex"))); this.txtSend.Text = resources.GetString("txtSend.Text"); this.txtSend.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("txtSend.TextAlign"))); this.txtSend.Visible = ((bool)(resources.GetObject("txtSend.Visible"))); this.txtSend.WordWrap = ((bool)(resources.GetObject("txtSend.WordWrap"))); // // txtDisplay // this.txtDisplay.AccessibleDescription = resources.GetString("txtDisplay.AccessibleDescription"); this.txtDisplay.AccessibleName = resources.GetString("txtDisplay.AccessibleName"); this.txtDisplay.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("txtDisplay.Anchor"))); this.txtDisplay.AutoSize = ((bool)(resources.GetObject("txtDisplay.AutoSize"))); this.txtDisplay.BackColor = System.Drawing.SystemColors.Window; this.txtDisplay.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("txtDisplay.BackgroundImage"))); this.txtDisplay.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("txtDisplay.Dock"))); this.txtDisplay.Enabled = ((bool)(resources.GetObject("txtDisplay.Enabled"))); this.txtDisplay.Font = ((System.Drawing.Font)(resources.GetObject("txtDisplay.Font"))); this.txtDisplay.ForeColor = System.Drawing.SystemColors.WindowText; this.txtDisplay.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("txtDisplay.ImeMode"))); this.txtDisplay.Location = ((System.Drawing.Point)(resources.GetObject("txtDisplay.Location"))); this.txtDisplay.MaxLength = ((int)(resources.GetObject("txtDisplay.MaxLength"))); this.txtDisplay.Multiline = ((bool)(resources.GetObject("txtDisplay.Multiline"))); this.txtDisplay.Name = "txtDisplay"; this.txtDisplay.PasswordChar = ((char)(resources.GetObject("txtDisplay.PasswordChar"))); this.txtDisplay.ReadOnly = true; this.txtDisplay.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("txtDisplay.RightToLeft"))); this.txtDisplay.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("txtDisplay.ScrollBars"))); this.txtDisplay.Size = ((System.Drawing.Size)(resources.GetObject("txtDisplay.Size"))); this.txtDisplay.TabIndex = ((int)(resources.GetObject("txtDisplay.TabIndex"))); this.txtDisplay.Text = resources.GetString("txtDisplay.Text"); this.txtDisplay.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("txtDisplay.TextAlign"))); this.txtDisplay.Visible = ((bool)(resources.GetObject("txtDisplay.Visible"))); this.txtDisplay.WordWrap = ((bool)(resources.GetObject("txtDisplay.WordWrap"))); // // tpListUsers // this.tpListUsers.AccessibleDescription = resources.GetString("tpListUsers.AccessibleDescription"); this.tpListUsers.AccessibleName = resources.GetString("tpListUsers.AccessibleName"); this.tpListUsers.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("tpListUsers.Anchor"))); this.tpListUsers.AutoScroll = ((bool)(resources.GetObject("tpListUsers.AutoScroll"))); this.tpListUsers.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("tpListUsers.AutoScrollMargin"))); this.tpListUsers.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("tpListUsers.AutoScrollMinSize"))); this.tpListUsers.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("tpListUsers.BackgroundImage"))); this.tpListUsers.Controls.Add(this.Label1); this.tpListUsers.Controls.Add(this.btnListUsers); this.tpListUsers.Controls.Add(this.lstUsers); this.tpListUsers.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("tpListUsers.Dock"))); this.tpListUsers.Enabled = ((bool)(resources.GetObject("tpListUsers.Enabled"))); this.tpListUsers.Font = ((System.Drawing.Font)(resources.GetObject("tpListUsers.Font"))); this.tpListUsers.ImageIndex = ((int)(resources.GetObject("tpListUsers.ImageIndex"))); this.tpListUsers.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("tpListUsers.ImeMode"))); this.tpListUsers.Location = ((System.Drawing.Point)(resources.GetObject("tpListUsers.Location"))); this.tpListUsers.Name = "tpListUsers"; this.tpListUsers.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("tpListUsers.RightToLeft"))); this.tpListUsers.Size = ((System.Drawing.Size)(resources.GetObject("tpListUsers.Size"))); this.tpListUsers.TabIndex = ((int)(resources.GetObject("tpListUsers.TabIndex"))); this.tpListUsers.Text = resources.GetString("tpListUsers.Text"); this.tpListUsers.ToolTipText = resources.GetString("tpListUsers.ToolTipText"); this.tpListUsers.Visible = ((bool)(resources.GetObject("tpListUsers.Visible"))); // // Label1 // this.Label1.AccessibleDescription = resources.GetString("Label1.AccessibleDescription"); this.Label1.AccessibleName = resources.GetString("Label1.AccessibleName"); this.Label1.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("Label1.Anchor"))); this.Label1.AutoSize = ((bool)(resources.GetObject("Label1.AutoSize"))); this.Label1.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("Label1.Dock"))); this.Label1.Enabled = ((bool)(resources.GetObject("Label1.Enabled"))); this.Label1.Font = ((System.Drawing.Font)(resources.GetObject("Label1.Font"))); this.Label1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192))); this.Label1.Image = ((System.Drawing.Image)(resources.GetObject("Label1.Image"))); this.Label1.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("Label1.ImageAlign"))); this.Label1.ImageIndex = ((int)(resources.GetObject("Label1.ImageIndex"))); this.Label1.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("Label1.ImeMode"))); this.Label1.Location = ((System.Drawing.Point)(resources.GetObject("Label1.Location"))); this.Label1.Name = "Label1"; this.Label1.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("Label1.RightToLeft"))); this.Label1.Size = ((System.Drawing.Size)(resources.GetObject("Label1.Size"))); this.Label1.TabIndex = ((int)(resources.GetObject("Label1.TabIndex"))); this.Label1.Text = resources.GetString("Label1.Text"); this.Label1.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("Label1.TextAlign"))); this.Label1.Visible = ((bool)(resources.GetObject("Label1.Visible"))); // // btnListUsers // this.btnListUsers.AccessibleDescription = resources.GetString("btnListUsers.AccessibleDescription"); this.btnListUsers.AccessibleName = resources.GetString("btnListUsers.AccessibleName"); this.btnListUsers.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("btnListUsers.Anchor"))); this.btnListUsers.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnListUsers.BackgroundImage"))); this.btnListUsers.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("btnListUsers.Dock"))); this.btnListUsers.Enabled = ((bool)(resources.GetObject("btnListUsers.Enabled"))); this.btnListUsers.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("btnListUsers.FlatStyle"))); this.btnListUsers.Font = ((System.Drawing.Font)(resources.GetObject("btnListUsers.Font"))); this.btnListUsers.Image = ((System.Drawing.Image)(resources.GetObject("btnListUsers.Image"))); this.btnListUsers.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnListUsers.ImageAlign"))); this.btnListUsers.ImageIndex = ((int)(resources.GetObject("btnListUsers.ImageIndex"))); this.btnListUsers.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("btnListUsers.ImeMode"))); this.btnListUsers.Location = ((System.Drawing.Point)(resources.GetObject("btnListUsers.Location"))); this.btnListUsers.Name = "btnListUsers"; this.btnListUsers.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("btnListUsers.RightToLeft"))); this.btnListUsers.Size = ((System.Drawing.Size)(resources.GetObject("btnListUsers.Size"))); this.btnListUsers.TabIndex = ((int)(resources.GetObject("btnListUsers.TabIndex"))); this.btnListUsers.Text = resources.GetString("btnListUsers.Text"); this.btnListUsers.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnListUsers.TextAlign"))); this.btnListUsers.Visible = ((bool)(resources.GetObject("btnListUsers.Visible"))); this.btnListUsers.Click += new System.EventHandler(this.btnListUsers_Click); // // lstUsers // this.lstUsers.AccessibleDescription = resources.GetString("lstUsers.AccessibleDescription"); this.lstUsers.AccessibleName = resources.GetString("lstUsers.AccessibleName"); this.lstUsers.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("lstUsers.Anchor"))); this.lstUsers.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("lstUsers.BackgroundImage"))); this.lstUsers.ColumnWidth = ((int)(resources.GetObject("lstUsers.ColumnWidth"))); this.lstUsers.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("lstUsers.Dock"))); this.lstUsers.Enabled = ((bool)(resources.GetObject("lstUsers.Enabled"))); this.lstUsers.Font = ((System.Drawing.Font)(resources.GetObject("lstUsers.Font"))); this.lstUsers.HorizontalExtent = ((int)(resources.GetObject("lstUsers.HorizontalExtent"))); this.lstUsers.HorizontalScrollbar = ((bool)(resources.GetObject("lstUsers.HorizontalScrollbar"))); this.lstUsers.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("lstUsers.ImeMode"))); this.lstUsers.IntegralHeight = ((bool)(resources.GetObject("lstUsers.IntegralHeight"))); this.lstUsers.ItemHeight = ((int)(resources.GetObject("lstUsers.ItemHeight"))); this.lstUsers.Location = ((System.Drawing.Point)(resources.GetObject("lstUsers.Location"))); this.lstUsers.Name = "lstUsers"; this.lstUsers.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("lstUsers.RightToLeft"))); this.lstUsers.ScrollAlwaysVisible = ((bool)(resources.GetObject("lstUsers.ScrollAlwaysVisible"))); this.lstUsers.Size = ((System.Drawing.Size)(resources.GetObject("lstUsers.Size"))); this.lstUsers.TabIndex = ((int)(resources.GetObject("lstUsers.TabIndex"))); this.lstUsers.Visible = ((bool)(resources.GetObject("lstUsers.Visible"))); // // frmMain // this.AccessibleDescription = resources.GetString("$this.AccessibleDescription"); this.AccessibleName = resources.GetString("$this.AccessibleName"); this.AutoScaleBaseSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScaleBaseSize"))); this.AutoScroll = ((bool)(resources.GetObject("$this.AutoScroll"))); this.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMargin"))); this.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMinSize"))); this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage"))); this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize"))); this.Controls.Add(this.TabControl1); this.Enabled = ((bool)(resources.GetObject("$this.Enabled"))); this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font"))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("$this.ImeMode"))); this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location"))); this.MaximizeBox = false; this.MaximumSize = ((System.Drawing.Size)(resources.GetObject("$this.MaximumSize"))); this.Menu = this.mnuMain; this.MinimumSize = ((System.Drawing.Size)(resources.GetObject("$this.MinimumSize"))); this.Name = "frmMain"; this.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("$this.RightToLeft"))); this.StartPosition = ((System.Windows.Forms.FormStartPosition)(resources.GetObject("$this.StartPosition"))); this.Text = resources.GetString("$this.Text"); this.Closing += new System.ComponentModel.CancelEventHandler(this.frmMain_Closing); this.Load += new System.EventHandler(this.frmMain_Load); this.TabControl1.ResumeLayout(false); this.tpChatPage.ResumeLayout(false); this.tpListUsers.ResumeLayout(false); this.ResumeLayout(false); } #endregion #region " Standard Menu Code " // This code simply shows the About form. private void mnuAbout_Click(object sender, System.EventArgs e) { // Open the About form in Dialog Mode frmAbout frm = new frmAbout(); frm.ShowDialog(this); frm.Dispose(); } // This code will close the form. private void mnuExit_Click(object sender, System.EventArgs e) { // Close the current form this.Close(); } #endregion const int READ_BUFFER_SIZE = 255; const int PORT_NUM = 10000; private TcpClient client; private byte[] readBuffer = new byte[READ_BUFFER_SIZE]; // Pop up a Connect user dialog and send a message requesting user to log in to chat. void AttemptLogin() { frmConnectUser frmConnectUser = new frmConnectUser(); frmConnectUser.StartPosition = FormStartPosition.CenterParent; frmConnectUser.ShowDialog(this); SendData("CONNECT|" + frmConnectUser.txtUserLogin.Text); frmConnectUser.Dispose(); } // Clear the Users listbox, and request the server to send the list of users. private void btnListUsers_Click(object sender, System.EventArgs e) { lstUsers.Items.Clear(); SendData("REQUESTUSERS"); } // Send the contents of the Send textbox if it isn't blank. private void btnSend_Click(object sender, System.EventArgs e) { if (txtSend.Text != "") { DisplayText("You say: " + txtSend.Text + (char) 13 + (char) 10); SendData("CHAT|" + txtSend.Text); txtSend.Text = string.Empty; } } // Writes text to the output textbox. private void DisplayText(string text) { txtDisplay.AppendText(text); } // This is the callback function for TcpClient.GetStream.Begin to get an // asynchronous read. private void DoRead(IAsyncResult ar) { int BytesRead; string strMessage; try { // Finish asynchronous read into readBuffer and return number of bytes read. BytesRead = client.GetStream().EndRead(ar); if (BytesRead < 1) { // if no bytes were read server has close. Disable input window. MarkAsDisconnected(); return; } // Convert the byte array the message was saved into, minus two for the // Chr(13) and Chr(10) strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2); ProcessCommands(strMessage); // Start a new asynchronous read into readBuffer. client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DoRead), null); } catch( Exception e) { MarkAsDisconnected(); } } // Send the server a disconnect message private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e) //base.Closing; { // Send only if server is still running. if (btnSend.Enabled == true) { SendData("DISCONNECT"); } } // When the form starts, this subroutine will connect to the server and attempt to // log in. private void frmMain_Load(object sender, System.EventArgs e) { frmConnectUser frmConnectUser = new frmConnectUser(); try { // The TcpClient is a subclass of Socket, providing higher level // functionality like streaming. client = new TcpClient("localhost", PORT_NUM); // Start an asynchronous read invoking DoRead to avoid lagging the user // interface. client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DoRead), null); // Make sure the window is showing before popping up connection dialog. this.Show(); AttemptLogin(); } catch( Exception Ex) { MessageBox.Show("Server is not active. Please start server and try again.", this.Text,MessageBoxButtons.OK , MessageBoxIcon.Exclamation); this.Dispose(); } } // This subroutine adds a list of users to listbox. private void ListUsers(string[] users) { int I; for (I = 1; I <= (users.Length - 1); I++) { lstUsers.Items.Add(users[I]); } } // When the server disconnects, prevent further chat messages from being sent. private void MarkAsDisconnected() { txtSend.ReadOnly = true; btnSend.Enabled = false; } // Process the command received from the server, and take appropriate action. private void ProcessCommands(string strMessage) { string[] dataArray; // Message parts are divided by "|" Break the string into an array accordingly. dataArray = strMessage.Split((char) 124); // dataArray(0) is the command. switch( dataArray[0]) { case "JOIN": // Server acknowledged login. DisplayText("You have joined the chat" + (char) 13 + (char) 10); break; case "CHAT": // Received chat message, display it. DisplayText(dataArray[1] + (char) 13 + (char) 10); break; case "REFUSE": // Server refused login with this user name, try to log in with another. AttemptLogin(); break; case "LISTUSERS": // Server sent a list of users. ListUsers(dataArray); break; case "BROAD": // Server sent a broadcast message DisplayText("ServerMessage: " + dataArray[1] + (char) 13 + (char) 10); break; } } // Use a StreamWriter to send a message to server. private void SendData(string data) { StreamWriter writer = new StreamWriter(client.GetStream()); writer.Write(data + (char) 13); writer.Flush(); } } Trint Hi,
Two options that I can think at the moment: 1) Using a socket connection, so you have a destination IP using a certain port, The other application have to create a socket and listen to that port. Everytime a message arrives the destination application can read the object and do whatever you like. This option can allow you better control 2) Using remoting, you can use HTTP or TCP remoting to send messages between applications, the good thing is that you can use port 80 if you use HTTP so no new ports are opened improving the security. Hope this helps Salva Show quote "trint" wrote: > How can I send from myApp, over the internet with c#.net, a message to > another instance of myApp?? Like Yahoo or MSN? > Thanks, > Trint > > There are a lot of parts to this type of solution: connect, write, read.
I have including only the most critical code to accomplish the tasks, but you would want to use error trapping and seperate methods for each part. Also would want to seriously consider using threads to execute the methods so that you don't lock your main (interface) thread. * To create a connection private string mHostname; private int mPort; private TcpClient mSock = new TcpClient(); private NetworkStream mSockStream; try { if (mSock==null) mSock = new TcpClient(); mSock.NoDelay=true; mSock.Connect(mHostname, mPort); mSockStream = mSock.GetStream(); // Mark here that you are connected } catch (Exception ex) { // Mark as not connected and handle error } * To write to the stream you would issue a command like: byte [] buff = System.Text.UTF8Encoding.UTF8.GetBytes("String to send"); mSockStream.Write(buff, 0, buff.Length); * To read from the stream you would issue a command like: (P.S. This is designed to be called repeatedly would want this is a function call with error handling.) (You don't want to lock your main thread waiting on bytes to be received.) int lTotalBytesRead = 0; int lBytesRead = 0; Thread.Sleep(5); if (mSockStream.DataAvailable) { byte[] buff = new byte[1024]; lBytesRead = mSockStream.Read(buff, 0, 1024); lTotalBytesRead+=lBytesRead; ProcessData(buff); } // You would use a command like follows to get the bytes back to a string return Encoding.ASCII.GetString(buff, 0, lBytesRead) -Greg McCallum (MCSD) |
|||||||||||||||||||||||