zoukankan      html  css  js  c++  java
  • <C#>最简单的NetMeeting

          怎样制造小画家软件了,怎样实现小画家在互联电脑间显示update了?其实,应用的方法很简单。我们可用Form中的一些事件来浅浅介绍一下:在客户端用上Form中的MouseDown和MouseUp两个事件做出小画家来,通过UDP协议将小画家中信息即时发送去服务端。客户端的代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    
    namespace Client3
    {
        public partial class Form1 : Form
        {
            int startX, startY,stopX,stopY;
            Graphics g;
    
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                if (Enabled == false)
                    s.Close();
            }
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                startX = e.X;
                startY = e.Y;
    
                byte[] mybyte1 = Encoding.Default.GetBytes(Convert.ToString(startX));
                byte[] mybyte2 = Encoding.Default.GetBytes(Convert.ToString(startY));
                s.SendTo(mybyte1, ipep);
                s.SendTo(mybyte2, ipep);
    
            }
    
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                g = this.CreateGraphics();
                Pen p = new Pen(Color.Red, 1);
                g.DrawLine(p, startX, startY, e.X, e.Y);
    
                stopX = e.X;
                stopY = e.Y;
    
                byte[] mybyte3 = Encoding.Default.GetBytes(Convert.ToString(stopX));
                byte[] mybyte4 = Encoding.Default.GetBytes(Convert.ToString(stopY));
                s.SendTo(mybyte3, ipep);
                s.SendTo(mybyte4, ipep);
    
    
            }
            //    s.Close();     
        }
    }

    在服务器端用Form中的Paint事件将客户端接受来的信息显示到Form中。它的代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    
    namespace Service3
    {
        public partial class Form1 : Form
        {
            int startX, startY, stopX, stopY;
            Graphics g;
    
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                if (Enabled == false)
                    s.Close();
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                s.Bind(ipep);
    
                while (true)
                {
                    byte[] data1 = new byte[1024];
                    IPEndPoint send1 = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint Remote1 = (EndPoint)(send1);
                    int length1 = s.ReceiveFrom(data1, ref Remote1);
                    startX = Convert.ToInt32(Encoding.Default.GetString(data1, 0, length1));
    
                    byte[] data2 = new byte[1024];
                    IPEndPoint send2 = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint Remote2 = (EndPoint)(send2);
                    int length2 = s.ReceiveFrom(data2, ref Remote2);
                    startY = Convert.ToInt32(Encoding.Default.GetString(data2, 0, length2));
    
                    byte[] data3 = new byte[1024];
                    IPEndPoint send3 = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint Remote3 = (EndPoint)(send3);
                    int length3 = s.ReceiveFrom(data3, ref Remote3);
                    stopX = Convert.ToInt32(Encoding.Default.GetString(data3, 0, length3));
    
                    byte[] data4 = new byte[1024];
                    IPEndPoint send4 = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint Remote4 = (EndPoint)(send4);
                    int length4 = s.ReceiveFrom(data4, ref Remote4);
                    stopY = Convert.ToInt32(Encoding.Default.GetString(data4, 0, length4));
    
                    g = e.Graphics;
                    Pen pen1 = new Pen(Color.Blue, 1);
                    g.DrawLine(pen1, startX, startY, stopX, stopY);
                }
                
                //s.Close();
            }
    
        }
    }
  • 相关阅读:
    Slider
    好久没更新了,发照片.....
    第二天、SQL*PLUS基本使用
    二进制方式存储图片(数据库)
    PagingBulletedList
    第四天、PL/SQL基础
    第八天、安全管理
    MutuallyExclusiveCheckBox
    第六天、视图,同义词,序列
    Rating
  • 原文地址:https://www.cnblogs.com/virgil/p/2715435.html
Copyright © 2011-2022 走看看