UDP网路会议室的代码
UDP网络会议室视频已经录制好,这里贴上代码仅供参考
MainWindow代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets; namespace NetWork
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
IPAddress ip;
public MainWindow()
{ IPAddress [] ips=Dns .GetHostAddresses (Dns.GetHostName());
foreach (var v in ips )
{
if ( v .AddressFamily ==AddressFamily.InterNetwork )
{
ip = v;
}
}
InitializeComponent();
} private void button1_Click(object sender, RoutedEventArgs e)
{ Client c1 = new Client();
c1.Title = "客户端1";
c1.Local = new IPEndPoint(ip, );
c1.username = "张三";
c1.Show(); Client c2 = new Client();
c2.Title = "客户端2";
c2.Local = new IPEndPoint(ip, );
c2.username = "李四";
c2.Show(); Client c3 = new Client();
c3.Title = "客户端3";
c3.Local = new IPEndPoint(ip, );
c3.username = "王五";
c3.Show();
}
}
}
Client代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Threading; namespace NetWork
{
/// <summary>
/// Client.xaml 的交互逻辑
/// </summary>
public partial class Client : Window
{
public IPEndPoint Local ;
public IPEndPoint Remote=null;
public UdpClient udpclient;
public string username {
get { return textBox1 .Text; }
set { textBox1.Text = value; }
}
private IPAddress multicastAddress = IPAddress.Parse("224.0.1.10");
private bool isExit = false;
public Client()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
button3.IsEnabled = false;
udpclient = new UdpClient(Local);
udpclient.JoinMulticastGroup(multicastAddress); }
public void ReceiveMessage( )
{
while (isExit ==false)
{
try
{
byte [] result = udpclient.Receive(ref Remote);
string message = Encoding.Unicode.GetString(result);
string[] array = message.Split(',');
string command = array[]; switch (command)
{
case "Login":
string nm = array[];
AddUser(nm );
break;
case "Logout":
RemoveUser(array [] );
break;
case "Say":
AddSay(array [] +":"+array []);
break;
default:
break;
}
}
catch
{
break;
}
}
} private void button3_Click(object sender, RoutedEventArgs e)
{
SendMessage("Say,"+username+","+textBox2 .Text );
} private void button1_Click(object sender, RoutedEventArgs e)
{ Thread t1 = new Thread(ReceiveMessage);
t1.Start();
SendMessage("Login," + username); button1.IsEnabled = false;
button3.IsEnabled = true; }
public void SendMessage(string message)
{
byte[] bytes = Encoding.Unicode.GetBytes(message); for (int i = ; i < ; i++)
{
udpclient.Send(bytes, bytes.Length, multicastAddress.ToString(), i);
} }
private void RemoveUser(string name)
{
Action act = delegate()
{
listBox1.Items.Remove(name); };
listBox1.Dispatcher.Invoke(act );
}
private void AddUser( string name1)
{
Action act = delegate()
{
listBox1.Items.Add(name1);
};
listBox1.Dispatcher.Invoke(act); }
private void AddSay(string message )
{
Action act = delegate()
{
textBlock1.Text += message+"\n";
};
textBlock1.Dispatcher.Invoke(act); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
isExit = true;
SendMessage("Logout,"+username);
udpclient.DropMulticastGroup(multicastAddress);
udpclient.Close(); } }
}
一个普通的本科大学生,平常的时候无所事事不学习,临近考试却拼命的补习.几天时间就能学半学期的知识,早干嘛去了.悲哀.
UDP网路会议室的代码的更多相关文章
- 【Java TCP/IP Socket】UDP Socket(含代码)
UDP的Java支持 UDP协议提供的服务不同于TCP协议的端到端服务,它是面向非连接的,属不可靠协议,UDP套接字在使用前不需要进行连接.实际上,UDP协议只实现了两个功能: 1)在IP协议的基础上 ...
- java UDP网路编程
大家都知道java中的socket网络编程,而其采用的协议分别有tcp和udp协议两种. 通常的理解tcp协议类似于打电话,udp类似于发短信.前者是线程安全的,但是效率比较低.后者则刚好相反. 今天 ...
- Java UDP实现聊天功能代码
我以前经常写的是基于TCP的网络编程,由于TCP建立连接鼻血要经过三次握手连接,服务器端需要阻塞式等待客户端的连接.而UDP则是可以直接向目的地址的目的端口上发送数据包,由于它只负责发送出去就好,不管 ...
- Java UDP实现聊天功能代码【转】
感谢大佬大佬!!!:https://www.cnblogs.com/woshijpf/p/3735684.html 我以前经常写的是基于TCP的网络编程,由于TCP建立连接鼻血要经过三次握手连接,服务 ...
- Udp打洞原理和源代码。
所谓udp打洞就是指客户端A通过udp协议向服务器发送数据包,服务器收到后,获取数据包,并且 可获取客户端A地址和端口号.同样在客户端B发送给服务器udp数据包后,服务器同样在收到B发送过来 的数据包 ...
- UDP传输
@@@基于UDP的客服端代码 public class Service { // 服务器 public static void main(String[] args) { DatagramPacket ...
- 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战
概述 本文演示的是一个Android客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo. 当前由于NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能. ...
- 【原创】NIO框架入门(三):iOS与MINA2、Netty4的跨平台UDP双向通信实战
前言 本文将演示一个iOS客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo.服务端将分别用MINA2和Netty4进行实现,而通信时服务端你只需选其一就行了.同 ...
- java 25 - 4 网络编程之 UDP协议传输思路
UDP传输 两个类:DatagramSocket与DatagramPacket(具体看API) A:建立发送端,接收端. B:建立数据包. C:调用Socket的发送接收方法. D:关闭Socket. ...
随机推荐
- Atcoder C - Vacation ( DP )
C - Vacation Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement Taro' ...
- BZOJ2199[Usaco2011 Jan]奶牛议会——2-SAT+tarjan缩点
题目描述 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 (1 <= M ...
- 快乐的Lambda表达式(二)
转载:http://www.cnblogs.com/jesse2013/p/happylambda-part2.html 快乐的Lambda表达式 上一篇 背后的故事之 - 快乐的Lambda表达式( ...
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- 线段树分治总结(线段树分治,线段树,并查集,树的dfn序,二分图染色)
闲话 stO猫锟学长,满脑子神仙DS 网上有不少Dalao把线段树分治也归入CDQ分治? 还是听听YCB巨佬的介绍: 狭义:只计算左边对右边的贡献. 广义:只计算外部对内部的贡献. 看来可以理解为广义 ...
- Android 第三课 构建简单的用户界面
构建简单的用户界面 上一课下一课 该课程教你 创建线性布局 添加文本框 添加字符串资源 添加按钮 使输入框宽度充满整个屏幕 你也应该阅读 布局 Android的图形用户界面通过 View 和 View ...
- python操作oracle实战
import cx_Oracle conn = cx_Oracle.connect('ua_test/ua_test@192.32.98.15/oracledb') cur1 = conn.curso ...
- 【poj2396】 Budget
http://poj.org/problem?id=2396 (题目链接) 题意 给出一个矩阵,给出每一行每一列的和,以及若干限制条件,限制了其中每一个元素的上下界,求一种可行的方案使得每一行每一列数 ...
- 搭建gulp脚手架
前段时间刚好在开发公司的首页,使用的是gulp工作流,gulp相对于webpack而言,配置简单,也更加直观(很符合直觉),日常开发一些静态页面.html5专题也足够,这里把遇到的坑与实践经验记录一下 ...
- linux已开机时间 系统信息
linux 查看系统运行时间 (从开机当现在的开机时间) 1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0. ...