• 大小: 167KB
    文件类型: .rar
    金币: 2
    下载: 2 次
    发布日期: 2021-06-17
  • 语言: C#
  • 标签: C#  TCP/IP  多线程  窗体  

资源简介

基于VS开发环境,使用C#开发语言。C#窗体TCP/IP的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

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

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

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

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

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

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

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

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

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

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

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

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

    ..A..H.     13312  2012-04-11 20:56  TCPIP\Client\Backup\IOTClient.v12.suo

     文件      11776  2018-04-14 10:15  TCPIP\Client\IOTClient\bin\Debug\IOTClient.exe

     文件      30208  2018-04-14 10:15  TCPIP\Client\IOTClient\bin\Debug\IOTClient.pdb

     文件      22704  2018-04-14 10:17  TCPIP\Client\IOTClient\bin\Debug\IOTClient.vshost.exe

     文件        490  2016-03-09 13:48  TCPIP\Client\IOTClient\bin\Debug\IOTClient.vshost.exe.manifest

     文件        254  2018-04-07 21:45  TCPIP\Client\IOTClient\ConnectionClient.cs

     文件       4846  2018-04-14 10:15  TCPIP\Client\IOTClient\Form1.cs

     文件       6488  2018-04-12 21:07  TCPIP\Client\IOTClient\Form1.Designer.cs

     文件       5814  2018-04-12 20:51  TCPIP\Client\IOTClient\Form1.resx

     文件       3580  2018-04-07 21:44  TCPIP\Client\IOTClient\IOTClient.csproj

     文件       1231  2018-04-07 21:38  TCPIP\Client\IOTClient\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6076  2018-04-12 20:32  TCPIP\Client\IOTClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       4226  2018-04-14 10:17  TCPIP\Client\IOTClient\obj\Debug\IOTClient.csproj.FileListAbsolute.txt

     文件        847  2018-04-12 20:51  TCPIP\Client\IOTClient\obj\Debug\IOTClient.csproj.GenerateResource.Cache

     文件       1341  2018-03-29 15:49  TCPIP\Client\IOTClient\obj\Debug\IOTClient.csprojResolveAssemblyReference.cache

     文件      11776  2018-04-14 10:15  TCPIP\Client\IOTClient\obj\Debug\IOTClient.exe

     文件        180  2018-04-12 20:51  TCPIP\Client\IOTClient\obj\Debug\IOTClient.Form1.resources

     文件      30208  2018-04-14 10:15  TCPIP\Client\IOTClient\obj\Debug\IOTClient.pdb

     文件        180  2018-04-07 21:44  TCPIP\Client\IOTClient\obj\Debug\IOTClient.Properties.Resources.resources

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

评论

共有 条评论