using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
namespace UDP聊天
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.StatusBar statusBar1;
private bool ReadFlag = true;
private Thread th;
private IPEndPoint remote;
private UdpClient udpclient;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(11, 6);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 23);
this.label1.TabIndex = 0;
this.label1.Text = "接收信息:";
this.listBox1.Location = new System.Drawing.Point(9, 28);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(318, 121);
this.listBox1.TabIndex = 1;
this.textBox1.Location = new System.Drawing.Point(72, 200);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(183, 20);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
this.label2.Location = new System.Drawing.Point(26, 203);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(91, 23);
this.label2.TabIndex = 4;
this.label2.Text = "信息:";
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(272, 200);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(42, 28);
this.button1.TabIndex = 5;
this.button1.Text = "发送";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(272, 160);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(42, 28);
this.button2.TabIndex = 6;
this.button2.Text = "接收";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.textBox2.Location = new System.Drawing.Point(73, 163);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(181, 20);
this.textBox2.TabIndex = 8;
this.textBox2.Text = "";
this.label3.Location = new System.Drawing.Point(14, 164);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(52, 23);
this.label3.TabIndex = 7;
this.label3.Text = "IP地址:";
this.statusBar1.Location = new System.Drawing.Point(0, 251);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(337, 22);
this.statusBar1.TabIndex = 9;
this.statusBar1.Text = "程序不处于数据接收状态";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(337, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar1,
this.textBox2,
this.label3,
this.button2,
this.button1,
this.textBox1,
this.listBox1,
this.label2,
this.label1});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "UDP聊天";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void read()
{
udpclient = new UdpClient(8000);
remote = null;
Encoding enc = Encoding.Unicode;
while (ReadFlag == true)
{
Byte[] data = udpclient.Receive(ref remote);
String strData = enc.GetString(data);
string remoteIP = remote.Address.ToString();
listBox1.Items.Add(remoteIP + ":" + strData);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
UdpClient SendUdp = new UdpClient();
IPAddress remoteIP;
try
{
remoteIP = IPAddress.Parse(textBox2.Text);
}
catch
{
MessageBox.Show("请输入正确的IP地址!", "错误");
return;
}
IPEndPoint remoteep = new IPEndPoint(remoteIP, 8000);
Byte[] buffer = null;
Encoding enc = Encoding.Unicode;
string str = textBox1.Text;
buffer = enc.GetBytes(str.ToCharArray());
SendUdp.Send(buffer, buffer.Length, remoteep);
textBox1.Clear();
SendUdp.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
th = new Thread(new ThreadStart(read));
th.Start();
statusBar1.Text = "程序处于数据接收状态!";
}
}
}