• 大小: 0.05M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-05-03
  • 语言: C#
  • 标签: Socket  tcp  

资源简介

使用Socket及TcpClient发送数据。

资源截图

代码片段和文件信息

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.Tasks;
using System.Windows.Forms;

namespace SEND
{
    public partial class Form1 : Form
    {
        //创建TcpClient实例
        private TcpClient tcpsender;

        public Form1()
        {
            InitializeComponent();
        }

        bool connected
        {
            get { return _connected; }
            set
            {
                _connected = value;
                button1.Text = value ? “断开“ : “链接“;
                label1.Text = value ? “连接“ : “断开“;
            }
        }
        bool _connected = false;

        private void button1_Click(object sender EventArgs e)
        {
            if (connected)
            {
                tcpsender.Close();
                connected = false;
            }
            else
            {
                //初始化TcpClient实例,建立一个指定IP和端口的新连接
                tcpsender = new TcpClient(textBox1.Text Convert.ToInt32(textBox2.Text));
                connected = true;
            }
        }

        private void button2_Click(object sender EventArgs e)
        {
            if (textBox3.Text == ““)
            {
                MessageBox.Show(“输入信息“);
                return;
            }
            try
            {
                //对发送字符进行编码
                byte[] bytes = Encoding.UTF8.GetBytes(textBox3.Text);
                //创建一个Socket对象用以发送数据
                Socket msock = tcpsender.Client;
                //发送消息
                msock.Send(bytes SocketFlags.None);
                textBox3.Text = ““;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         981  2016-03-22 09:22  SEND.sln
     文件       37376  2020-11-18 08:49  SEND.v12.suo
     目录           0  2020-12-07 10:07  SEND\
     文件         187  2016-03-22 09:22  SEND\App.config
     目录           0  2016-12-13 08:13  SEND\bin\
     目录           0  2020-12-07 10:08  SEND\bin\Debug\
     文件       10240  2020-12-07 10:08  SEND\bin\Debug\SEND.exe
     文件         187  2016-03-22 09:22  SEND\bin\Debug\SEND.exe.config
     文件       22016  2020-12-07 10:08  SEND\bin\Debug\SEND.pdb
     文件       24224  2020-12-07 10:07  SEND\bin\Debug\SEND.vshost.exe
     文件         187  2016-03-22 09:22  SEND\bin\Debug\SEND.vshost.exe.config
     文件         490  2019-03-19 12:46  SEND\bin\Debug\SEND.vshost.exe.manifest
     文件        2055  2020-12-07 10:07  SEND\Form1.cs
     文件        4694  2020-12-07 10:07  SEND\Form1.Designer.cs
     文件        5817  2020-12-07 10:07  SEND\Form1.resx
     目录           0  2016-12-13 08:13  SEND\obj\
     目录           0  2020-12-07 10:08  SEND\obj\Debug\
     文件        1453  2016-03-22 10:07  SEND\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7087  2020-11-17 10:31  SEND\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件        2836  2020-12-07 10:08  SEND\obj\Debug\SEND.csproj.FileListAbsolute.txt
     文件         977  2020-12-07 10:08  SEND\obj\Debug\SEND.csproj.GenerateResource.Cache
     文件        2211  2020-12-07 10:08  SEND\obj\Debug\SEND.csprojResolveAssemblyReference.cache
     文件       10240  2020-12-07 10:08  SEND\obj\Debug\SEND.exe
     文件         180  2020-12-07 10:08  SEND\obj\Debug\SEND.Form1.resources
     文件       22016  2020-12-07 10:08  SEND\obj\Debug\SEND.pdb
     文件         180  2020-12-07 10:08  SEND\obj\Debug\SEND.Properties.Resources.resources
     文件           0  2016-03-22 09:22  SEND\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2016-03-22 09:22  SEND\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2016-03-22 09:22  SEND\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     目录           0  2016-03-22 09:22  SEND\obj\Debug\TempPE\
     文件         516  2016-03-22 09:22  SEND\Program.cs
............此处省略7个文件信息

评论

共有 条评论