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. ...
随机推荐
- Java7和8在虚拟机上的差异:Perm Generation vs. Metaspace
- What is the best Java email address validation method?
https://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method htt ...
- VUE的语法笔记
v-model = 'content' {{contents}} //vue 双向视图的绑定 v-text 只能返回一个文本内容 v-html 不仅可以返回文本内容还可以返回html标签 v-for ...
- Sqlite,libevent,openssl,mosquito交叉编译
一.设置交叉编译环境 在makefile所在目录(或源代码根目录)打开终端. 在终端中设置交叉编译所需的临时环境变量(也可写到配置文件中设置为全局环境变量),其中交叉编译工具链的名称和目录需要根据实际 ...
- [转帖]Linux内核为大规模支持100Gb/s网卡准备好了吗?并没有
Linux内核为大规模支持100Gb/s网卡准备好了吗?并没有 之前用 千兆的机器 下载速度 一般只能到 50MB 左右 没法更高 万兆的话 可能也就是 200MB左右的速度 很难更高 不知道后续的服 ...
- [日常工作] Linux与Windows的连接访问以及数据共享等方法 vncserver smb xshell xftp winscp mount等
日常办公机器是用 windows, 但是越来越多的测试和工作需求需要使用linux. 这里以最常用的系统centos为例进行说明 1. 远程连接 ssh的方式 建议使用xmange 系列的 xshel ...
- From 简书 转帖一下如何安装k8s1.10 改天做下实验. https://www.jianshu.com/p/9c7e1c957752
centos7.3 kubernetes/k8s 1.10 离线安装 老菜_misa 关注 2018.04.25 23:57 字数 1243 阅读 266评论 1喜欢 3 本文介绍在centos7.3 ...
- Oracle数据库SQLPLUS 连接显示 ??? 的解决
linux下 安装了中文版本的,造成sqlplus 连接时出现了乱码 如图 一开始以为是LANG 变量的问题 后来发现是NLS_LANG的问题 解决方法: export NLS_LANG=" ...
- Angular 简单的Post
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- NodeJS中的require和import
ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...