直接上代码吧!
在窗体上调用的类:
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 实时视频传输系统 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } /// <summary> /// 声明Camera类 /// </summary> Camera ca = null; //创建一个Thread实例 private Thread thread1; //创建一个UdpClient实例 private UdpClient udpReceive; private UdpClient udpSend; private byte[] bytes; //private DialogResult result; private void btnPlay_Click(object sender, EventArgs e) { btnPlay.Enabled = false; btnStop.Enabled = true; ca = new Camera(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height); try { ca.StartWebCam(); } catch (Exception) { MessageBox.Show("未能初始化摄像头!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); btnPlay.Enabled = true; btnStop.Enabled = false; ca = null; } } private void btnStop_Click(object sender, EventArgs e) { try { ca.CloseWebcam(); btnPlay.Enabled = true; btnStop.Enabled = false; } catch (Exception) { MessageBox.Show("摄像头关闭失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); btnPlay.Enabled = true; btnStop.Enabled = false; ca = null; } } private void btnClose_Click(object sender, EventArgs e) { try { ca.CloseWebcam(); btnPlay.Enabled = true; btnStop.Enabled = false; } catch (Exception) { MessageBox.Show("摄像头关闭失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); btnPlay.Enabled = true; btnStop.Enabled = false; ca = null; } //关闭程序 this.Close(); Application.Exit(); } private IPEndPoint ipLocalPoint; private EndPoint RemotePoint; private Socket mySocket; private bool RunningFlag = false; private void btnGetIP_Click(object sender, EventArgs e) { //string GetIP; //IPAddress[] AddressList = Dns.GetHostEntry(Dns.GetHostEntry()).AddressList; //if (AddressList.Length < 1) //{ // return ""; //} //return AddressList[0].ToString(); //this.tbGetIP.Text = Convert.ToInt32(AddressList); } //IP与端口号有效验证 private int getValidPort(string port) { int lport; //测试端口号是否有效 try { //是否为空 if (port == "") { throw new ArgumentException("端口号无效,不能启动UDP"); } lport = System.Convert.ToInt32(port); } catch(Exception e) { Console.WriteLine("无效的端口号"+ e.ToString()); this.tbGetPort.AppendText("无效的端口号"+ e.ToString()+"\n"); return -1; } return lport; } private IPAddress getValidIP(string ip) { IPAddress lip = null; //测试IP是否有效 try { //是否为空 if (!IPAddress.TryParse(ip, out lip)) { throw new ArgumentException("IP无效,不能启动UDP"); } } catch(Exception e) { Console.WriteLine("无效的IP:"+ e.ToString()); this.tbGetIP.AppendText("无效的IP:"+ e.ToString() +"\n"); return null; } return lip; } private void MainForm_Load(object sender, EventArgs e) { thread1 = new Thread(new ThreadStart()); thread1.Start(); } private void btnReceive_Click(object sender, EventArgs e) { //在本机指定的端口接收 udpReceive = new UdpClient(8001); //将套接字加入组播组 udpReceive.JoinMulticastGroup(IPAddress.Parse("224.100.0.10"), 50); //接收从远程主机发送过来的信息 IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0); while (true) { //ref表示引用类型的 IPPoint实例接收消息 try { bytes = udpReceive.Receive(ref iep); } catch { DialogResult result = MessageBox.Show("发送失败!"); } string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length); string message = "来自" + iep.ToString() + "的消息"; //显示消息 并以message为消息的标题 DialogResult res = MessageBox.Show(str, message); } } private void btnSend_Click_1(object sender, EventArgs e) { //初始化UdpClient udpSend = new UdpClient(); //实际使用时应将127.0.0.1改为服务器的远程IP IPAddress remoteIPAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint remoteIPEndPoint = new IPEndPoint(remoteIPAddress, 8002); //将发送内容转换为字节数组 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(); //设置重传次数 int retry = 0; while (true) { try { udpSend.Send(bytes, bytes.Length, remoteIPEndPoint); break; } catch { if (retry < 3) { retry++; continue; } else { DialogResult result = MessageBox.Show("发送失败!"); break; } } } } } }