Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very simple UDP chat application not working !!!

Status
Not open for further replies.

karmafree

Programmer
May 10, 2001
107
GB
Hi,

I have a very, very simple UDP chat applicaton written in C# .NET that sends and receives messages to myself or to any other computer. However, it doesn't seem to be working properly; - I can send messages (I think) but can't receive them. This may be a long shot, but if you could, do you reckon you can take a quick look at it?! I would be very grateful!

Thank you,
Steve.



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Chat
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Chat : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBoxMyIPAddress;
private System.Windows.Forms.TextBox textBoxMyPort;
private System.Windows.Forms.TextBox textBoxRemoteIPAddress;
private System.Windows.Forms.TextBox textBoxRemotePort;
private System.Windows.Forms.Label labelMyIPAddress;
private System.Windows.Forms.Label labelMyPort;
private System.Windows.Forms.Label labelRemoteIPAddress;
private System.Windows.Forms.Label labelRemotePort;
private System.Windows.Forms.Label labelUsername;
private System.Windows.Forms.TextBox textBoxUsername;
private System.Windows.Forms.RichTextBox richTextBoxConversation;
private System.Windows.Forms.RichTextBox richTextBoxMessage;
private System.Windows.Forms.Button buttonSend;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private System.Net.Sockets.UdpClient udpClient;

private System.Text.UnicodeEncoding unicodeEncoding;

private System.Threading.ThreadStart threadStart;
private System.Threading.Thread thread;

private bool isChatting;

public Chat()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

this.udpClient = new System.Net.Sockets.UdpClient();

this.unicodeEncoding = new System.Text.UnicodeEncoding();

this.threadStart = new System.Threading.ThreadStart(Listen);
this.thread = new System.Threading.Thread(this.threadStart);
this.thread.Start();

this.isChatting = false;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


#region Windows Form Designer generated code


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Chat());
}

protected void Listen()
{
try
{
while (this.isChatting)
{
System.Net.IPEndPoint ipEndPoint;

if ((this.textBoxMyIPAddress.Text.Trim().Equals(string.Empty)) || ((this.textBoxMyPort.Text.Trim().Equals(string.Empty))))
{
ipEndPoint = null;
}
else
{
ipEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(this.textBoxMyIPAddress.Text.Trim()), System.Convert.ToInt32(this.textBoxMyPort.Text.Trim()));
}

byte[] data = this.udpClient.Receive(ref ipEndPoint);

this.richTextBoxConversation.Text += this.unicodeEncoding.GetString(data) + &quot;\r\n&quot;;
}
}
catch (System.Exception exception)
{
System.Windows.Forms.MessageBox.Show(&quot;Listen()&quot; + &quot;\n&quot; + exception.Message);
}
}

private void buttonSend_Click(object sender, System.EventArgs e)
{
try
{
System.Net.IPEndPoint ipEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(this.textBoxRemoteIPAddress.Text.Trim()), System.Convert.ToInt32(this.textBoxRemotePort.Text.Trim()));

byte[] data = this.unicodeEncoding.GetBytes(this.textBoxUsername.Text + &quot;:&quot; + &quot; &quot; + this.richTextBoxMessage.Text);

this.udpClient.Send(data, data.Length, ipEndPoint);

this.isChatting = true;

this.richTextBoxMessage.Clear();
this.richTextBoxMessage.Focus();
}
catch (System.Exception exception)
{
System.Windows.Forms.MessageBox.Show(&quot;Incorrect Remote IP Address or Remote Port Number.&quot; + &quot;\n\n&quot; + &quot;buttonSend_Click()&quot; + &quot;\n&quot; + exception.Message);
}
}

private void Chat_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.isChatting)
{
this.isChatting = false;

this.udpClient.Close();
}
}
}
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top