• 局域网UDP聊天程序桌面版


    说明

    最简单的聊天程序:采用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.Net;
    using System.Net.Sockets;
    using System.Threading;

    namespace WindowsApplication2
    {
    publicpartialclass Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    privatevoid textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    privatevoid Form1_Load(object sender, EventArgs e)
    {
    richTextBox2.Focus();
    }

    privatevoid richTextBox2_TextChanged(object sender, EventArgs e)
    {

    }
    Thread th;
    privatevoid button1_Click(object sender, EventArgs e)
    {

    th
    =new Thread(new ThreadStart(this.ServerStart));
    th.Start();
    messageinfo
    +="聊天程序启动!\r\n";
    richTextBox1.Text
    = messageinfo;
    }
    //保存聊天记录(因为没有找到跨线程取控件值的方法。)
    staticstring messageinfo ="";

    ///<summary>
    /// 发送聊天记录。
    ///</summary>
    ///<param name="message">聊天记录</param>
    ///<param name="toanyone">发给对方的IP</param>
    ///<param name="port">端口号。</param>
    void SendMessage(string message, string toanyone,int port)
    {
    //UDP连接对象
    UdpClient udpclient =new UdpClient(toanyone, port);
    //发送的消息
    byte[] bytes = Encoding.UTF8.GetBytes(message);
    //发送消息
    udpclient.Send(bytes, bytes.Length);

    messageinfo
    +="您说:"+ message +""+ DateTime.Now.ToString() +"\r\n";
    this.richTextBox1.Text +="您说:"+ message +""+ DateTime.Now.ToString() +"\r\n";
    //Console.WriteLine("您说:" + message + " " + DateTime.Now.ToString());
    //Console.WriteLine("-------------------------------");
    }
    ///<summary>
    /// 开始监听
    ///</summary>
    void ServerStart()
    {
    Console.WriteLine(
    "-----------开始监听------------");
    //定义UDP监听
    UdpClient udplistener =new UdpClient(8090);

    //这里定义监听到的客户端的信息,IP
    IPEndPoint guest =new IPEndPoint(IPAddress.Any, 8090);
    //注意这里由于是死循环,必须用子线程启动
    while (true)
    {
    string Recevied ="";

    //接收监听到的信息
    byte[] bytes = udplistener.Receive(ref guest);
    Recevied
    ="信息来自:"+ guest.Address.ToString() +"说:"+ Encoding.UTF8.GetString(bytes)+""+DateTime.Now.ToString()+"\r\n";
    //this.richTextBox1.Text += Recevied;
    //Recevied = GetText() +"\r\n"+ Recevied;

    messageinfo
    += Recevied;
    //跨线程操作控件
    SetText( messageinfo);
    }
    }
    privatevoid button2_Click(object sender, EventArgs e)
    {
    if (richTextBox2.Text.Trim() =="")
    {
    MessageBox.Show(
    "不容许发送空记录!");
    return;
    }
    string toanyone =this.textBox1.Text;
    int port =int.Parse(this.textBox2.Text);
    SendMessage(
    this.richTextBox2.Text,toanyone,port);
    richTextBox2.Text
    ="";
    }
    ///<summary>
    /// 跨线程操作控件
    ///</summary>
    ///<param name="text"></param>
    delegatevoid SetTextCallback(string text);
    privatevoid SetText(string text)
    {
    // InvokeRequired required compares the thread ID of the
    // calling thread to the thread ID of the creating thread.
    // If these threads are different, it returns true.
    if (this.richTextBox1.InvokeRequired)
    {
    SetTextCallback d
    =new SetTextCallback(SetText);
    this.Invoke(d, newobject[] { text });
    }
    else
    {
    this.richTextBox1.Text = text;
    }
    }


    privatevoid Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    //th.Abort();
    //强制退出。有子线程可能没结束。(监听的线程)
    System.Environment.Exit(0);
    Application.Exit();
    }

    privatevoid button3_Click(object sender, EventArgs e)
    {
    messageinfo
    ="";
    richTextBox1.Text
    ="";
    }

    privatevoid richTextBox1_TextChanged(object sender, EventArgs e)
    {
    //使得聊天窗口总是显示最新的消息
    richTextBox1.SelectionStart = richTextBox1.Text.Length;
    richTextBox1.Focus();
    //使得输入聊天信息 的文本框获得焦点,便于用户输入
    richTextBox2.Focus();
    }
    }
    }

    由于又要忙了,留下来做记录。

  • 相关阅读:
    根据View找控制器
    ScrollView双击图片定点放大
    iOS消息推送原理和实现总结
    iOS完整学习路线图
    获取设备版本
    UIView与CALayer的区别,很详细
    iOS开发网络数据之AFNetworking使用
    (已实践)PLSQL本地还原Oracle数据库dmp文件
    所遇Oracle错误代码
    Dapper基本使用
  • 原文地址:https://www.cnblogs.com/shya/p/1905068.html
Copyright © 2020-2023  润新知