• 大小: 98KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: C#
  • 标签: Socket  即时通讯  

资源简介

C# winform Socket 即时通讯,C# winform Socket 即时通讯

资源截图

代码片段和文件信息

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;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace Chat_Demo_Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
            Socket socketSend;//负责发送信息的socket
             //将远程连接的客户端的IP地址和Socket存入集合中  
             Dictionary dicSocket = new Dictionary();  


        /// 
        /// 监测
        /// 

        /// 
        /// 
        private void button1_Click(object sender EventArgs e)
        {
            try
            {
                //当点击开始监听的时候 在服务器端创建一个负责监听IP地址和端口号的Socket  
                //addressFamily: Socket:使用的寻址方案,socketType:Socket 的类型,protocolType:Socket 使用的协议。           
                Socket socketWatch = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
                IPAddress ip = IPAddress.Any;
                //创建服务器端网络端点对象  
                IPEndPoint point = new IPEndPoint(ip Convert.ToInt32(ProText.Text));
                //侦听Soket与本地终结点绑定  
                socketWatch.Bind(point);
                //设置为监听状态  
                socketWatch.Listen(10);
                ShowMsg(“监听成功“);
                //新建线程等待连接,创建通信Socket  
                Thread th = new Thread(Listen);
                th.IsBackground = true;//设置为后台线程  
                th.Start(socketWatch);
            }
            catch
            {
            }  
        }

        /// 
        /// 发送消息
        /// 

        /// 
        /// 
        private void button2_Click(object sender EventArgs e)
        {
            string str = chatBoxSend.Text.Trim();
            byte[] buf = Encoding.UTF8.GetBytes(str);
            List byteList = new List();
            //根据自定义的协议,发送的字节数组的第一位为0是表示发送的是文本  
            byteList.Add(0);
            byteList.AddRange(buf);
            //泛型集合转换为新的数组  
            byte[] newBuffer = byteList.ToArray();
            //获取用户在下拉框中的IP地址  
            try
            {
                string ip = cboUsers.SelectedItem.ToString();
                dicSocket[ip].Send(newBuffer);
                chatBoxSend.Text = ““;
            }
            catch
            {

            }        
        }

      
        //
        private void Listen(object o)
        {
            Socket socketWatch = o as Socket;
            //循环目的在于使得多个客户端可以连接服务器  
            while (true)
            {
                try
                {
                    //等待客户端连接由侦听的Socket创建一个负责与客户端通信的Socket  
                    socketSend = socketWatch.Accept();
       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-26 17:21  Chat_Demo_Server\
     目录           0  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\
     文件        3691  2017-10-26 17:39  Chat_Demo_Server\Chat_Demo_Server\Chat_Demo_Server.csproj
     文件        5364  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\Form1.Designer.cs
     文件        5577  2017-10-27 10:53  Chat_Demo_Server\Chat_Demo_Server\Form1.cs
     文件        5817  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\Form1.resx
     文件         497  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\Program.cs
     目录           0  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\Properties\
     文件        1382  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\Properties\AssemblyInfo.cs
     文件        2883  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\Properties\Resources.Designer.cs
     文件        5612  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\Properties\Resources.resx
     文件        1102  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\Properties\Settings.Designer.cs
     文件         249  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\Properties\Settings.settings
     目录           0  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\bin\
     目录           0  2017-10-26 17:39  Chat_Demo_Server\Chat_Demo_Server\bin\Debug\
     文件       11264  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\bin\Debug\Chat_Demo_Server.exe
     文件       26112  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\bin\Debug\Chat_Demo_Server.pdb
     文件       11600  2017-10-27 10:52  Chat_Demo_Server\Chat_Demo_Server\bin\Debug\Chat_Demo_Server.vshost.exe
     文件         490  2010-03-17 22:39  Chat_Demo_Server\Chat_Demo_Server\bin\Debug\Chat_Demo_Server.vshost.exe.manifest
     目录           0  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\obj\
     目录           0  2017-10-26 17:21  Chat_Demo_Server\Chat_Demo_Server\obj\x86\
     目录           0  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\
     文件         180  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\Chat_Demo_Server.Form1.resources
     文件         180  2017-10-26 17:39  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\Chat_Demo_Server.Properties.Resources.resources
     文件         787  2017-10-27 10:52  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\Chat_Demo_Server.csproj.FileListAbsolute.txt
     文件       11264  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\Chat_Demo_Server.exe
     文件       26112  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\Chat_Demo_Server.pdb
     文件        4501  2017-10-27 09:11  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6311  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         264  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\GenerateResource.read.1.tlog
     文件         662  2017-10-27 10:29  Chat_Demo_Server\Chat_Demo_Server\obj\x86\Debug\GenerateResource.write.1.tlog
............此处省略38个文件信息

评论

共有 条评论