网络编程方法提取以及简介

Posted Gooodoood

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络编程方法提取以及简介相关的知识,希望对你有一定的参考价值。

一 UDP

1.获取本地IP地址

private void getLocalIP()
        
            //获得IP地址容器
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in ips)
            
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                
                    address = ip;
                    info.Text += "服务器的IP地址为:" + ip.ToString();
                    break;
                
            
            if (address == null)
            
                info.Text += "无法获得服务器IP地址" + "\\n";
            
        

2.启动接收线程

ThreadStart start = new ThreadStart(Receive);
Thread t1 = new Thread(start);
            t1.IsBackground = true;
            t1.Start();

3.接收方法(UDP)

private void Receive()
        
            //接受
            byte[] buffer = new byte[1024];
            //注册对面主机的终结点
            EndPoint remoteEP = new IPEndPoint(IPAddress.None, 8001);
            int num = cs.ReceiveFrom(buffer, ref remoteEP);


            //解析
            while (num > 0)
            
                //解码:以UTF-8字符串的方式获得传入buffer的数据
                String s = Encoding.UTF8.GetString(buffer, 0, num);
                
                //通过Dispathcer.Invoke()取得对这个文本线程的访问权限,并用解码后的字 
                  符串进行更改
                log.Dispatcher.Invoke(() =>  log.Text = log.Text + "消息为" +s 
                 
                     );

                //进行循环的监听
                num = cs.ReceiveFrom(buffer, ref remoteEP);
            

            //禁用套接字的发送和接受
            cs.Shutdown(SocketShutdown.Both);
            //关闭套接字
            cs.Close();
        

4.发送方法(UDP)

 private void send()
        
            //实例化目的终结点
            IPAddress endAdreess = IPAddress.Parse("127.0.0.1");

            EndPoint remoteEP = new IPEndPoint(endAdreess,8001);
            String s = "你好";

            //将要传输的字符串进行编码
            byte[] buffer = Encoding.UTF8.GetBytes(s);

            //使用套接字的SenTo方法进行发送
            cs.SendTo(buffer, toEP);
        

5.将本地终结点与套接字绑定

private void BindLocal()
        
            //获取套接字对象UDP
            cs = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //它表示将侦听器 Socket 绑定到的本地终结点。
            EndPoint localEP = new IPEndPoint(address, 8001);
            cs.Bind(localEP)
        

成功实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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;

namespace UDP

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    
        IPAddress address;
        Socket cs;
        public MainWindow()
        
            InitializeComponent();
        

        private void Window_Initialized(object sender, EventArgs e)
        
            getLocalIP();
            BindLocal();
            ThreadStart start = new ThreadStart(Receive);
            Thread t1 = new Thread(start);
            t1.IsBackground = true;
            t1.Start();
        

        private void Send()
        
            EndPoint remoteEP = new IPEndPoint(address,8002);
            String s = "你好";
            byte[] buffer = Encoding.UTF8.GetBytes(s);
            cs.SendTo(buffer,remoteEP);
        

        //将本地终结点与套接字绑定
        private void BindLocal()
        
            cs = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            EndPoint localEp = new IPEndPoint(address, 8001);
            cs.Bind(localEp);
        

        private void Receive()
        
            //接受
            byte[] buffer = new byte[1024];
            //注册对面主机的终结点
            EndPoint remoteEP = new IPEndPoint(IPAddress.None, 8002);
            int num = cs.ReceiveFrom(buffer, ref remoteEP);

            //解析
            while (num > 0)
            
                //解码:以UTF-8字符串的方式获得传入buffer的数据
                String s = Encoding.UTF8.GetString(buffer, 0, num);

                //通过Dispathcer.Invoke()取得对这个文本线程的访问权限,并用解码后的字符串进行更改
                
                msgText.Dispatcher.Invoke(() => 
                    msgText.Text = msgText.Text + "消息为" + s;
                     );

                //进行循环的监听
                num = cs.ReceiveFrom(buffer, ref remoteEP);
            

            //禁用套接字的发送和接受
            cs.Shutdown(SocketShutdown.Both);
            //关闭套接字
            cs.Close();
        

        private void getLocalIP()
        
            //获得IP地址容器
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in ips)
            
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                
                    address = ip;
                    msgText.Text += "服务器的IP地址为:" + ip.ToString();
                    break;
                
            
            if (address == null)
            
                msgText.Text += "无法获得服务器IP地址" + "\\n";
            
        

        private void Button_Click(object sender, RoutedEventArgs e)
        
            Send();
        
    

以上是关于网络编程方法提取以及简介的主要内容,如果未能解决你的问题,请参考以下文章

动态代码生成技术在 Presto 中使用简介

动态代码生成技术在 Presto 中使用简介

使用从循环内的代码片段中提取的函数避免代码冗余/计算开销

PythonHospital ward dynamic contact network网络数据集图信息提取

PythonHospital ward dynamic contact network网络数据集图信息提取

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段