zoukankan      html  css  js  c++  java
  • UDP实现内网到网的点对点发送消息与转发消息

    UDP实现内网到网的点对点发送消息与转发消息 与上篇讲到的TCP连接有一点不一样 ,TCP可以一直保持着链接.然后进行通讯. 而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.IO;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;

    namespace p2p
    {
        public partial class Form1 : Form
        {
            private Thread th;
            private UdpClient tcpl;
            public bool listenerRun = true;
            Socket s;
            //listenerRun为true,表示可以接受连接请求,false则为结束程序

            public Form1()
            {
                InitializeComponent();
            

            }
            public void Stop()
            {
                tcpl.Close();
                th.Abort();//终止线程
                MessageBox.Show("结束监听....");
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
               th = new Thread(new ThreadStart(Listen));//新建一个用于监听的线程
               th.Start();//打开新线程
            }

            private void Listen()
            {
                try
                {
                    tcpl = new UdpClient(5656);//在5656端口新建一个TcpListener对象
                    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5656);
                    MessageBox.Show("正在监听中....");
                     while (listenerRun)//开始监听
                     {
                         Byte[] stream = new Byte[80];
                         stream = tcpl.Receive(ref groupEP);
                       
                         MessageBox.Show(groupEP.ToString() + System.Text.Encoding.UTF8.GetString(stream));               
                    }
                }
                catch (Exception ex)
                {
                 MessageBox.Show(ex.Message);
                }
                
            }

            private void button3_Click(object sender, EventArgs e)
            {
                th = new Thread(new ThreadStart(Send));//新建一个用于监听的线程
                th.Start();//打开新线程
            }

            public void Send()
            {
                try
                {
                     s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                     IPAddress broadcast = IPAddress.Parse("127.0.0.1");
                    byte[] sendbuf = Encoding.UTF8.GetBytes("中国人在哪里");
                    IPEndPoint ep = new IPEndPoint(broadcast, 5270);
                    s.Connect(ep);
                    s.SendTo(sendbuf, ep);
                    byte[] data = new byte[2048];
                    int rect = s.Receive(data);
                    byte[] chat = new byte[rect];
                    Buffer.BlockCopy(data, 0, chat, 0, rect);
                    MessageBox.Show(System.Text.Encoding.Default.GetString(chat));
                    s.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
                finally
                {
                  
                }
                
            }

            private void button2_Click(object sender, EventArgs e)
            {
                Stop();
            }

        }
    }

    服务端代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;

    namespace p2pserver
    {
        public partial class Form1 : Form
        {
            private Thread th;
            private UdpClient tcpl;
            public bool listenerRun = true;

            public Form1()
            {
                InitializeComponent();
                this.button2.Enabled = false;
            }
            private void Listen()
            {
                try
                {
                    tcpl = new UdpClient(5270);//在5270端口新建一个TcpListener对象
                    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5270);
                    while (listenerRun)//开始监听
                    {
                        string msg = "welcome";
                        Byte[] stream = new Byte[80];
                        stream = tcpl.Receive(ref groupEP);
                        MessageBox.Show(groupEP.ToString() + System.Text.Encoding.UTF8.GetString(stream));
                        byte[] bytesMsg = Encoding.Default.GetBytes(msg);
                        tcpl.Connect(groupEP);
                        tcpl.Send(bytesMsg, bytesMsg.Length);
                        tcpl.Close();
                        Listen();
                    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                   
                }
            }
            public void Stop()
            {
                try
                {
                    tcpl.Close();
                    th.Abort();//终止线程
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
          
            private void button1_Click(object sender, EventArgs e)
            {
                this.button1.Enabled = false;
                this.button2.Enabled = true;
                th = new Thread(new ThreadStart(Listen));//新建一个用于监听的线程
                th.Start();//打开新线程
            }

            private void button2_Click(object sender, EventArgs e)
            {
                this.button2.Enabled = false;
                this.button1.Enabled = true;
                Stop();
            }
        }
    }

  • 相关阅读:
    Jessica's Reading Problem POJ
    FatMouse and Cheese HDU
    How many ways HDU
    Humble Numbers HDU
    Doing Homework again
    Stacks of Flapjacks UVA
    Party Games UVA
    24. 两两交换链表中的节点
    面试题 03.04. 化栈为队
    999. 可以被一步捕获的棋子数
  • 原文地址:https://www.cnblogs.com/top5/p/1699490.html
Copyright © 2011-2022 走看看