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

资源简介

C#使用Sockets实现服务器与多个客户端进行通讯,简单的winform界面,有客户端和服务器端代码。

资源截图

代码片段和文件信息

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

namespace Client
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }

        Socket socketClient = null;
        Thread threadClient = null;

        private void Form1_Load(object sender EventArgs e)
        {

        }
        /// 
        /// 客户端启动服务方法:
        /// 1.申请一个套接字监听服务
        /// 2.连接服务器(指明ip和端口号)
        /// 

        private void startconn_Click(object sender EventArgs e)
        {
            socketClient = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            IPAddress ipaddress = IPAddress.Parse(iptext.Text.Trim());
            IPEndPoint ipendpoint = new IPEndPoint(ipaddress int.Parse(porttext.Text.Trim()));
            socketClient.Connect(ipendpoint);

            threadClient = new Thread(RecMsg);

            threadClient.IsBackground = true;
            threadClient.Start(); 

        }

        private void RecMsg()
        {
            while (true)//持续监听从服务端发送过来的消息
            {
                byte[] arrRecMsg = new byte[1024 * 1024];
                int length = socketClient.Receive(arrRecMsg);

                string strRecMsg = Encoding.UTF8.GetString(arrRecMsg 0 length);

                this.Invoke(new Action(() =>
                {
                    receivemsg.AppendText(“服务器:“ + GetTime() + “\r\n“ + strRecMsg + “\r\n“);
                } ));


            }
        }

        private void SendMsg(string sendMsg)
        {
            byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg);
            socketClient.Send(arrSendMsg);

            this.Invoke(new Action(() =>
            {

                receivemsg.AppendText(“客户端:“ + GetTime() + “\r\n“ + sendMsg + “\r\n“);
            }));

            Clean(sendmsg.Text.Trim());
        }

        private DateTime GetTime()
        {
            DateTime gettime = new DateTime();
            gettime = DateTime.Now;
            return gettime;
        }

        private void sendok_Click(object sender EventArgs e)
        {
            SendMsg(sendmsg.Text.Trim());
        }

        private void Clean(string str)
        {
            sendmsg.Text = null;
        }

        private void button3_Click(object sender EventArgs e)
        {
            Clean(sendmsg.Text.Trim());
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.    123392  2019-12-04 10:11  SocketText\.vs\SocketText\v16\.suo

     文件          0  2019-12-04 09:28  SocketText\.vs\SocketText\v16\Server\sqlite3\db.lock

     文件     622592  2019-12-04 10:11  SocketText\.vs\SocketText\v16\Server\sqlite3\storage.ide

     文件        187  2016-02-18 08:34  SocketText\Client\App.config

     文件      11776  2019-12-04 10:06  SocketText\Client\bin\Debug\Client.exe

     文件        187  2016-02-18 08:34  SocketText\Client\bin\Debug\Client.exe.config

     文件      26112  2019-12-04 10:06  SocketText\Client\bin\Debug\Client.pdb

     文件      24224  2016-02-20 08:37  SocketText\Client\bin\Debug\Client.vshost.exe

     文件        187  2016-02-18 08:34  SocketText\Client\bin\Debug\Client.vshost.exe.config

     文件        490  2015-10-30 15:19  SocketText\Client\bin\Debug\Client.vshost.exe.manifest

     文件       3792  2016-02-18 16:58  SocketText\Client\Client.csproj

     文件       2905  2019-12-04 10:03  SocketText\Client\Form1.cs

     文件       7776  2016-02-20 08:28  SocketText\Client\Form1.Designer.cs

     文件       5817  2016-02-20 08:28  SocketText\Client\Form1.resx

     文件       1805  2019-12-04 09:29  SocketText\Client\obj\Debug\Client.csproj.FileListAbsolute.txt

     文件       1012  2019-12-04 10:06  SocketText\Client\obj\Debug\Client.csproj.GenerateResource.cache

     文件        424  2019-12-04 10:06  SocketText\Client\obj\Debug\Client.csprojAssemblyReference.cache

     文件       2211  2016-02-19 08:30  SocketText\Client\obj\Debug\Client.csprojResolveAssemblyReference.cache

     文件      11776  2019-12-04 10:06  SocketText\Client\obj\Debug\Client.exe

     文件        180  2019-12-04 09:29  SocketText\Client\obj\Debug\Client.Form1.resources

     文件      26112  2019-12-04 10:06  SocketText\Client\obj\Debug\Client.pdb

     文件        180  2019-12-04 09:29  SocketText\Client\obj\Debug\Client.Properties.Resources.resources

     文件       1423  2019-12-04 09:37  SocketText\Client\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7189  2019-12-04 09:31  SocketText\Client\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2016-02-18 08:34  SocketText\Client\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2016-02-18 08:34  SocketText\Client\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2016-02-18 08:34  SocketText\Client\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        518  2016-02-18 08:34  SocketText\Client\Program.cs

     文件       1336  2016-02-18 08:34  SocketText\Client\Properties\AssemblyInfo.cs

     文件       2864  2016-02-18 08:34  SocketText\Client\Properties\Resources.Designer.cs

............此处省略143个文件信息

评论

共有 条评论