局域网UDP聊天程序桌面版
2010-12-13 22:07
337 查看
说明
最简单的聊天程序:采用UDP实现。学习用,无实用价值。注释很详细就不再说明了,监听的时候使用死循环,注意用到了跨线程操作控件、强制结束线程。
改进
实用委托实现线程安全的方式操作控件,等待线程结束。代码
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsApplication2
{
publicpartialclass Form1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid textBox1_TextChanged(object sender, EventArgs e)
{
}
privatevoid Form1_Load(object sender, EventArgs e)
{
richTextBox2.Focus();
}
privatevoid richTextBox2_TextChanged(object sender, EventArgs e)
{
}
Thread th;
privatevoid button1_Click(object sender, EventArgs e)
{
th =new Thread(new ThreadStart(this.ServerStart));
th.Start();
messageinfo +="聊天程序启动!\r\n";
richTextBox1.Text = messageinfo;
}
//保存聊天记录(因为没有找到跨线程取控件值的方法。)
staticstring messageinfo ="";
///<summary>
/// 发送聊天记录。
///</summary>
///<param name="message">聊天记录</param>
///<param name="toanyone">发给对方的IP</param>
///<param name="port">端口号。</param>
void SendMessage(string message, string toanyone,int port)
{
//UDP连接对象
UdpClient udpclient =new UdpClient(toanyone, port);
//发送的消息
byte[] bytes = Encoding.UTF8.GetBytes(message);
//发送消息
udpclient.Send(bytes, bytes.Length);
messageinfo +="您说:"+ message +""+ DateTime.Now.ToString() +"\r\n";
this.richTextBox1.Text +="您说:"+ message +""+ DateTime.Now.ToString() +"\r\n";
//Console.WriteLine("您说:" + message + " " + DateTime.Now.ToString());
//Console.WriteLine("-------------------------------");
}
///<summary>
/// 开始监听
///</summary>
void ServerStart()
{
Console.WriteLine("-----------开始监听------------");
//定义UDP监听
UdpClient udplistener =new UdpClient(8090);
//这里定义监听到的客户端的信息,IP
IPEndPoint guest =new IPEndPoint(IPAddress.Any, 8090);
//注意这里由于是死循环,必须用子线程启动
while (true)
{
string Recevied ="";
//接收监听到的信息
byte[] bytes = udplistener.Receive(ref guest);
Recevied ="信息来自:"+ guest.Address.ToString() +"说:"+ Encoding.UTF8.GetString(bytes)+""+DateTime.Now.ToString()+"\r\n";
//this.richTextBox1.Text += Recevied;
//Recevied = GetText() +"\r\n"+ Recevied;
messageinfo += Recevied;
//跨线程操作控件
SetText( messageinfo);
}
}
privatevoid button2_Click(object sender, EventArgs e)
{
if (richTextBox2.Text.Trim() =="")
{
MessageBox.Show("不容许发送空记录!");
return;
}
string toanyone =this.textBox1.Text;
int port =int.Parse(this.textBox2.Text);
SendMessage(this.richTextBox2.Text,toanyone,port);
richTextBox2.Text ="";
}
///<summary>
/// 跨线程操作控件
///</summary>
///<param name="text"></param>
delegatevoid SetTextCallback(string text);
privatevoid 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.richTextBox1.InvokeRequired)
{
SetTextCallback d =new SetTextCallback(SetText);
this.Invoke(d, newobject[] { text });
}
else
{
this.richTextBox1.Text = text;
}
}
privatevoid Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//th.Abort();
//强制退出。有子线程可能没结束。(监听的线程)
System.Environment.Exit(0);
Application.Exit();
}
privatevoid button3_Click(object sender, EventArgs e)
{
messageinfo ="";
richTextBox1.Text ="";
}
privatevoid richTextBox1_TextChanged(object sender, EventArgs e)
{
//使得聊天窗口总是显示最新的消息
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.Focus();
//使得输入聊天信息 的文本框获得焦点,便于用户输入
richTextBox2.Focus();
}
}
}
由于又要忙了,留下来做记录。
相关文章推荐
- 基于UDP的MFC局域网聊天程序
- 局域网广播(聊天)程序UDP
- Java 网络 UDP 一个局域网下的聊天小程序
- 用UDP实现局域网内聊天程序
- 用UDP实现局域网内聊天程序
- 用Delphi编写局域网中的UDP聊天程序 (收藏)
- 嵌入式开发第35天(UDP初级应用:局域网聊天)
- VB 利用UDP制作简单的点对点聊天程序
- C# UDP 网络聊天程序
- 简单的局域网聊天程序
- VC局域网聊天(UDP)
- linux c 基于UDP的多人聊天程序
- Java基础-UDP 键盘录入数据 、聊天程序
- 黑马程序员 用UDP协议在dos命令行里模拟一个聊天程序
- QTcpServer、QTcpSocket、QUdpSocket在聊天程序上的应用
- 基于TCP/UDP的网络聊天程序
- 在.net中使用Udp协议创建简单的聊天程序
- Netmsg 局域网聊天程序
- 基于Udp的Socket网络编程聊天程序
- 局域网聊天程序作品+源代码