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

资源简介

C#基于TCP的Socket多线程通信(包含服务端和客户端) 代码都有注释,方便新手阅读

资源截图

代码片段和文件信息

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


namespace IOTClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            TextBox.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_Load(object sender EventArgs e)
        {

        }

        Socket sokClient = null;//负责与服务端通信的套接字
        Thread threadClient = null;//负责 监听 服务端发送来的消息的线程
        bool isRec = true;//是否循环接收服务端数据

        private void btnConnect_Click(object sender EventArgs e)
        {
            //实例化 套接字
            sokClient = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            //创建 ip对象
            IPAddress address = IPAddress.Parse(txtIP.Text.Trim());
            //创建网络节点对象 包含 ip和port
            IPEndPoint endpoint = new IPEndPoint(address int.Parse(txtPort.Text.Trim()));
            //连接 服务端监听套接字
            sokClient.Connect(endpoint);

            //创建负责接收 服务端发送来数据的 线程
            threadClient = new Thread(ReceiveMsg);
            threadClient.IsBackground = true;
            //如果在win7下要通过 某个线程 来调用 文件选择框的代码,就需要设置如下
            threadClient.SetApartmentState(ApartmentState.STA);
            threadClient.Start();
        }

        /// 
        /// 接收服务端发送来的消息数据
        /// 

        void ReceiveMsg()
        {
            while (isRec)
            {
                byte[] msgArr = new byte[1024 * 1024 * 1];//接收到的消息的缓冲区
                int length = 0;
                //接收服务端发送来的消息数据
                length = sokClient.Receive(msgArr);//Receive会阻断线程
                if (msgArr[0] == 0)//发送来的是文字
                {
                    string strMsg = System.Text.Encoding.UTF8.GetString(msgArr 1 length - 1);
                    txtShow.AppendText(strMsg + “\r\n“);
                }
                else if (msgArr[0] == 1)
                { //发送来的是文件
                    SaveFileDialog sfd = new SaveFileDialog();
                    //弹出文件保存选择框
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        //创建文件流
                        using (FileStream fs = new FileStream(sfd.FileName FileMode.OpenOrCreate))
                        {
                            fs.Write(msgArr 1 length - 1);
                            MessageBox.Show(“文件保存成功!“);
                        }
                    }
                }
                else if (msgArr[0] == 2)
                {
                    ShakeWindow();
                }
            }
        }

        /// 
        /// 闪屏
        /// 

        private void ShakeWindow()
        {
            Rand

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

     文件      20480  2012-04-11 20:39  IOTClient\IOTClient\bin\Debug\IOTClient.exe

     文件      22016  2012-04-11 20:39  IOTClient\IOTClient\bin\Debug\IOTClient.pdb

     文件       5632  2005-12-08 14:51  IOTClient\IOTClient\bin\Debug\IOTClient.vshost.exe

     文件       4194  2012-04-11 20:39  IOTClient\IOTClient\Form1.cs

     文件       5578  2012-04-11 20:39  IOTClient\IOTClient\Form1.Designer.cs

     文件       5814  2012-04-11 20:39  IOTClient\IOTClient\Form1.resx

     文件       3225  2012-04-11 20:22  IOTClient\IOTClient\IOTClient.csproj

     文件        842  2012-04-11 20:39  IOTClient\IOTClient\obj\Debug\IOTClient.csproj.GenerateResource.Cache

     文件      20480  2012-04-11 20:39  IOTClient\IOTClient\obj\Debug\IOTClient.exe

     文件        180  2012-04-11 20:39  IOTClient\IOTClient\obj\Debug\IOTClient.Form1.resources

     文件      22016  2012-04-11 20:39  IOTClient\IOTClient\obj\Debug\IOTClient.pdb

     文件        180  2012-04-11 20:22  IOTClient\IOTClient\obj\Debug\IOTClient.Properties.Resources.resources

     文件        282  2012-04-11 20:50  IOTClient\IOTClient\obj\IOTClient.csproj.FileList.txt

     文件        468  2012-04-11 20:17  IOTClient\IOTClient\Program.cs

     文件       1190  2012-04-11 20:17  IOTClient\IOTClient\Properties\AssemblyInfo.cs

     文件       2872  2012-04-11 20:17  IOTClient\IOTClient\Properties\Resources.Designer.cs

     文件       5612  2012-04-11 20:17  IOTClient\IOTClient\Properties\Resources.resx

     文件       1092  2012-04-11 20:17  IOTClient\IOTClient\Properties\Settings.Designer.cs

     文件        249  2012-04-11 20:17  IOTClient\IOTClient\Properties\Settings.settings

     文件        916  2012-04-11 20:17  IOTClient\IOTClient.sln

    ..A..H.     13312  2012-04-11 20:56  IOTClient\IOTClient.suo

     文件      24576  2012-04-11 20:49  IOTServer\IOTServer\bin\Debug\IOTServer.exe

     文件      28160  2012-04-11 20:49  IOTServer\IOTServer\bin\Debug\IOTServer.pdb

     文件       5632  2005-12-08 14:51  IOTServer\IOTServer\bin\Debug\IOTServer.vshost.exe

     文件       4830  2012-04-11 20:12  IOTServer\IOTServer\ConnectionClient.cs

     文件       6054  2012-04-11 20:48  IOTServer\IOTServer\IOTServer.cs

     文件       3292  2012-04-11 20:00  IOTServer\IOTServer\IOTServer.csproj

     文件       8079  2012-04-11 20:49  IOTServer\IOTServer\IOTServer.Designer.cs

     文件       5814  2012-04-11 20:48  IOTServer\IOTServer\IOTServer.resx

     文件        904  2012-04-11 20:48  IOTServer\IOTServer\obj\Debug\IOTServer.csproj.GenerateResource.Cache

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

评论

共有 条评论