using System;
using System.Collections.Generic;//包含定义泛型集合的接口和类;泛型集合允许用户创建强类型的集合,这种集合在类型安全和性能上均优于非泛型强类型集合。
using System.ComponentModel;//提供用于实现组件和控件的运行时和设计时行为的类。此命名空间包括用于属性和类型转换器的实现、数据源绑定和组件授权的基类和接口。
using System.Data;
using System.Drawing;
using System.Text;//包含表示 ASCII、Unicode、UTF-7 和 UTF-8 字符编码的类;用于在字符块和字节块之间相互转换的抽象基类;以及不需要创建字符串的中间实例就可以操作和格式化字符串对象的帮助器类。
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using System.Collections;
using System.IO;
namespace chat
{
public partial class Form1 : Form
{
public User me;
public Form2 form2;
public ArrayList users = new ArrayList();
IPEndPoint serverTcp1;
IPEndPoint serverTcp2;
Socket socketTcp1;
Socket socketTcp2;
string chatIp;
int chatNo;
bool change = false;
int selectNo = 0;
bool connectYesNo = false;
bool hideYesNo = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
connectYesNo = false;
}
private void button2_Click(object sender, EventArgs e)
{
form2.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Form5 form5 = new Form5();
form5.form1 = this;
form5.Show();
}
private void 标识用户ToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
Form5 form = new Form5();
form.Show();
}
private void 标识用户ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 form = new Form4();
form.Show();
}
private void button4_Click(object sender, EventArgs e)
{
if (hideYesNo == false)
{
button4.Text = "上线";
hideYesNo = true;
}
else
{
button4.Text = "隐身";
hideYesNo = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
Thread thread = new Thread(new ThreadStart(this.waitTcp));
thread.IsBackground = true;
thread.Start();
}catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
treeViewShow();
Thread threadUdp = new Thread(new ThreadStart(this.getUdp));
threadUdp.IsBackground = true;
threadUdp.Start();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
form2.Close();
}
//1秒udp发一次在线信息
private void timer1_Tick(object sender, EventArgs e)
{
if (hideYesNo == false)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 8050);
//设置Broadcast值为1表示允许套接字发送广播信息,该值默认为0(不允许)
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
//将发送内容转换为字节数组
Byte[] bytes = System.Text.Encoding.Unicode.GetBytes(me.UserName);
//向子网发送信息
socket.SendTo(bytes, iep);
socket.Close();
}
}
//获得udp广播
private void getUdp()
{
bool have = false;
while (true)
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 8050);
Byte[] bytes = new byte[1024];
socket.Bind(iep);
EndPoint ep = (EndPoint)iep;
socket.ReceiveFrom(bytes, ref ep);
string name = System.Text.Encoding.Unicode.GetString(bytes);
iep = (IPEndPoint)ep;
String ip = iep.Address.ToString();
if (users != null)
{
have = false;
for (int i = 0; i < users.Count; i++)
{
if (((User)users[i]).UserIp.CompareTo(ip) == 0)
{
have = true;//列表中是否已有该用户
if (((User)users[i]).UserName.CompareTo(name) != 0)
{
((User)users[i]).UserName = name;
change = true;
}
((User)users[i]).Online = 8;
}
}
if (!have)//加入新用户
{
User he = new User(name, ip);
users.Add(he);
}
}
socket.Close();
}
}
//三秒查看是否有下线或上线的用户,有的话,刷新列表
private void timer2_Tick(object sender, EventArgs e)
{
if (users.Count != treeView1.Nodes.Count)
{
change = true;
}
if (users != null)
{
for (int i = 0; i < users.Count; i++)
{
((User)users[i]).Online -= 5;
if (((User)users[i]).Online < 0)
{
change = true;
users.RemoveAt(i);
i--;
}
}
}
if (change == true)
{
treeViewShow();
change = false;
}
}
//刷新列表
private void treeViewShow()
{
this.treeView1.Nodes.Clear();
for (int i = 0; i < users.Count; i++)
{
TreeNode node = new TreeNode(((User)users[i]).UserName);
this.treeView1.Nodes.Add(node);
}
}
//等待连接
private void waitTcp()
{
serverTcp1 = new IPEndPoint(IPAddress.Parse(me.UserIp),8051);
while(true)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(serverTcp1);
socket.Listen(50);
socketTcp1= socket.Accept();
Thread thread=new Thread(new ThreadStart(this.chatTcp));
thread.IsBackground = true;
thread.Start();
socket.Close();
}
}
//请求连接服务器端线程
private void chatTcp()
{
Socket aaa = socketTcp1;
if (aaa.Connected)
{
while (true)
{
Byte[] byte
基于DirectX组件的IP电话程序设计
1星 需积分: 10 149 浏览量
2013-04-14
09:35:18
上传
评论 1
收藏 4KB RAR 举报
karzn
- 粉丝: 4
- 资源: 16
最新资源
- MQTT协议发温湿度到阿里云平台支持下发控制LED灯与继电器对接阿里云APP
- STM32F103ZET6+OV2640+TF卡存储
- 操作系统考试要点最新版本.doc
- 操作系统试题B卷.doc
- 移动机器人自主路径规划之RRT算法MATLAB实现代码
- Python使用 LSTM循环神经网络预测风力发电厂中风机产生的功率项目源码+数据集.zip
- 深入探究文件I/O-I.MX6U嵌入式Linux C应用编程学习笔记基于正点原子阿尔法开发板
- MQTT协议发温湿度电压数据到ONENET支持下发控制LED灯与继电器(新平台)
- 平抑风电波动的电-氢混合储能容量优化配置(注释完全,可直接运行)(文档加Matlab源码)
- Gigabyte.RX560.4g 1750mhz bios GAMING OC
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈