c# socket-udp通信聊天winform程序

本文介绍了一款使用C#编写的UDP Socket通信聊天应用,通过WinForm界面实现简单聊天、发送震动及文件传输功能。用户分别监听9000和9001端口进行双向通信,并通过局域网广播发现好友。登录后自动添加好友到列表,聊天窗体支持发送消息。提供了源码链接以供进一步学习和拓展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 之前一直想模仿飞秋(飞Q)写一个简易得通信程序练手,熟悉sokect通信。

主要实现功能:简易得聊天窗体,发送消息,发送震动,发送文件(可拓展)

话不多说,直接上代码吧。

登陆窗体:

二个用户分别监听不同得端口9000,9001来测试

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FeiQ
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_Name.Text))
            {
                MessageBox.Show("请输入用户名", "提示");
                return;
            }
            MainForm m = new MainForm(this.txt_Name.Text,int.Parse(this.txtlistenport.Text),int.Parse(this.txtsendport.Text));
            this.Hide();
            m.ShowDialog();
        }
    }
}

主窗体:

登陆后会向发送端口发送局域网广播通知扫描好友,收到后会自动回复并且添加到好友列表

using CommonClass;
using CommonClass.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using static FeiQ.Enums;

namespace FeiQ
{
    public partial class MainForm : Form
    {
        public  Users UserInfo;
        public  Dictionary<string, UserSokect> userdic = new Dictionary<string, UserSokect>();
        public  Dictionary<string, MyLinklable> userLinklable = new Dictionary<string, MyLinklable>();
        //private static string BindPort = ConfigurationManager.AppSettings["BindPort"];
        //private static string ScanPort = ConfigurationManager.AppSettings["ScanPort"]; 
        private int ListenUdpport = 9000; //监听接受消息接口
        private int ScanUdpport = 9001;   //发送消息接口
        public MainForm(string Id)
        {  
            CheckForIllegalCrossThreadCalls = false;
            UserInfo = new Users() { Userid = Id };
            InitializeComponent();
            this.lbl_userinfo.Text = Id + " port: " + ListenUdpport;
        }

        public MainForm(string Id,int listen,int sender)
        {
            CheckForIllegalCrossThreadCalls = false;

            UserInfo = new Users() { Userid = Id };
            ListenUdpport = listen;
            ScanUdpport = sender;

            InitializeComponent();
            this.lbl_userinfo.Text = Id + " port: " + ListenUdpport;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
            StartListener();
            //MessageBox.Show("StarListener");

            ScanClients();
            //MessageBox.Show("broadcast : " + Udpport); 
        }

        #region  scanip
        /// <summary>
        /// 广播扫描客服端
        /// </summary>
        private void ScanClients()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, Protoco
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值