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

资源简介

C# Socket通讯DEMO,包括服务端客户端,能够实现简单聊天功能。结构简单方便用来学习深度开发

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SocketClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //关闭对文本框的非法线程操作检查
            TextBox.CheckForIllegalCrossThreadCalls = false;
        }

        // 创建一个客户端套接字
        Socket clientSocket = null;
        // 创建一个监听服务端的线程
        Thread threadServer = null;
        private void btn_Click(object sender EventArgs e)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            if (string.IsNullOrEmpty(ipAddress.Text.ToString()))
            {
                MessageBox.Show(“监听ip地址不能为空!“);
                return;
            }
            if (string.IsNullOrEmpty(port.Text.ToString()))
            {
                MessageBox.Show(“监听端口不能为空!“);
                return;
            }
            IPAddress ip = IPAddress.Parse(ipAddress.Text.Trim());
            IPEndPoint endpoint = new IPEndPoint(ip int.Parse(port.Text.Trim()));
         
            try
            {   //这里客户端套接字连接到网络节点(服务端)用的方法是Connect 而不是Bind
                clientSocket.Connect(endpoint);
            }
            catch 
            {
                chatContent.AppendText(“连接失败!“);
               
            }
            
            // 创建一个线程监听服务端发来的消息
            threadServer = new Thread(recMsg);
            threadServer.IsBackground = true;
            threadServer.Start();
        }

        /// 
        ///  接收服务端发来的消息
        /// 

        private void recMsg() {

            while (true) //持续监听服务端发来的消息
            {
                //定义一个1M的内存缓冲区 用于临时性存储接收到的信息
                byte[] arrRecMsg = new byte[1024 * 1024];
                int length = 0;
                try
                {
                    //将客户端套接字接收到的数据存入内存缓冲区 并获取其长度
                     length = clientSocket.Receive(arrRecMsg);
                }
                catch 
                {
                    return;
                   
                }
             
                //将套接字获取到的字节数组转换为人可以看懂的字符串
                string strRecMsg = Encoding.UTF8.GetString(arrRecMsg 0 length);
                //将发送的信息追加到聊天内容文本框中
                chatContent.AppendText(“服务端(“ + GetCurrentTime() + “):“ + strRecMsg + “\r\n“);
            }
        }

        /// 
        /// 发送消息到服务端
        /// 

        /// 
        private void clientSendMsg(string msg)
        {
            byte[] sendMsg = Encoding.UTF8.GetBytes(msg);
            clientSocket.Send(sendMsg);
            chatContent.AppendText(“客户端(“ + GetCurrentTime() + “):“ + msg + “\r\n“);
        }
        /// 

        /// 获取当前系统时间的方法
        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-12 02:54  Socket-master\
     文件         132  2017-10-12 02:54  Socket-master\.gitignore
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\.vs\
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\.vs\SocketServer\
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\.vs\SocketServer\v15\
     文件       68096  2017-10-12 02:54  Socket-master\SocketServer\.vs\SocketServer\v15\.suo
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\.vs\SocketServer\v15\sqlite3\
     文件      593920  2017-10-12 02:54  Socket-master\SocketServer\.vs\SocketServer\v15\sqlite3\storage.ide
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\
     文件         182  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\App.config
     文件        6541  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Form1.Designer.cs
     文件        3870  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Form1.cs
     文件        5698  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Form1.resx
     文件         502  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Program.cs
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Properties\
     文件        1304  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Properties\AssemblyInfo.cs
     文件        2766  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Properties\Resources.Designer.cs
     文件        5496  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Properties\Resources.resx
     文件        1069  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Properties\Settings.Designer.cs
     文件         242  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\Properties\Settings.settings
     文件        3488  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\SocketClient.csproj
     文件           0  2017-10-12 02:54  Socket-master\SocketServer\SocketClient\reback.txt
     文件        1592  2017-10-12 02:54  Socket-master\SocketServer\SocketServer.sln
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\SocketServer\
     文件         182  2017-10-12 02:54  Socket-master\SocketServer\SocketServer\App.config
     文件        6522  2017-10-12 02:54  Socket-master\SocketServer\SocketServer\Form1.Designer.cs
     文件        4596  2017-10-12 02:54  Socket-master\SocketServer\SocketServer\Form1.cs
     文件        5698  2017-10-12 02:54  Socket-master\SocketServer\SocketServer\Form1.resx
     文件         502  2017-10-12 02:54  Socket-master\SocketServer\SocketServer\Program.cs
     目录           0  2017-10-12 02:54  Socket-master\SocketServer\SocketServer\Properties\
............此处省略28个文件信息

评论

共有 条评论