P2P UDP与TCP的监听与发送 代码笔记

来源:互联网 发布:中小企业融资数据统计 编辑:程序博客网 时间:2024/04/29 13:20

 在网上下载了p2p的理论代码之后,他是基于TCP的发送消息与监听模式.经过MSDN的资料阅读.写出了UDPclient模式下与tcplistener模式下的两种点对点发送消息机制.现在还是只能存在内网使用,不能踌网段发送消息.跨网段发送将要继续研究UDP打洞的原理

 

下面是基于TCP的代码:

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;

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

        public Form1()
        {
            InitializeComponent();
        

        }
        public void Stop()
        {
            tcpl.Stop();
            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 TcpListener(5656);//在5656端口新建一个TcpListener对象
                tcpl.Start();
                MessageBox.Show("正在监听中....");
                 while (listenerRun)//开始监听
                {
                    Socket s = tcpl.AcceptSocket();
                    string remote = s.RemoteEndPoint.ToString();
                    Byte[] stream = new Byte[80];
                    int i = s.Receive(stream);//接受连接请求的字节流
                    string msg = "<" + remote + ">" + System.Text.Encoding.UTF8.GetString(stream);
                    MessageBox.Show(msg);//在控制台显示字符串
                    this.textBox1.Text = msg;
                }
            }
            catch (Exception ex)
            {
             MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Send();
        }

        public void Send()
        {
            string stream = "";
            //获得要发送的信息
            stream = this.textBox2.Text.Trim();
            try
            {
                TcpClient tcpc = new TcpClient(this.textBox3.Text.Trim(), 5656);
                //在5656端口新建一个TcpClient对象
                NetworkStream tcpStream = tcpc.GetStream();

                StreamWriter reqStreamW = new StreamWriter(tcpStream);
                reqStreamW.Write(stream);
                reqStreamW.Flush();//发送信息
                tcpStream.Close();
                tcpc.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

 

    }
}

 

下面是基于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;
        //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)
        {
            Send();
        }

        public void Send()
        {
            try
            {
                Socket 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, 5656);
                s.SendTo(sendbuf, ep);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
           
        }

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

 

    }
}

 

经测试发现.byte[] sendbuf = Encoding.UTF8.GetBytes("中国人在哪里");发送处的这段代码中的utf8与接收处的编码方式要一样,否则会出现乱码问题!

 

继续研究UDP打洞,让点对点穿防火墙

原创粉丝点击