• 大小: 134KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: 其他
  • 标签: Tcp  

资源简介

TcpListener类与TcpClient类的使用

资源截图

代码片段和文件信息

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

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

        private void button3_Click(object sender EventArgs e)
        {
            Thread sendMsg = new Thread(SendMessage);
            sendMsg.IsBackground = true;
            sendMsg.Start();

            if (textBox3.Text.Equals(“$END$“))
            {
                button1.Enabled = true;
                txtIP.Enabled = true;
                txtPort.Enabled = true;
                textBox3.Enabled = false;
                button3.Enabled = false;
                label3.Text = “已关闭连接!\n“;

            }
        }

        TcpClient client = null;

        private void button1_Click(object sender EventArgs e)
        {
            client = new TcpClient(txtIP.Text.Trim() Convert.ToInt32(txtPort.Text));
            string msg = string.Format(“已连接入服务器:{0}端口号:{1}\n“ txtIP.Text txtPort.Text);
            label3.Text = msg;
            button1.Enabled = false;
            txtIP.Enabled = false;
            txtPort.Enabled = false;
            textBox3.Enabled = true;
            button3.Enabled = true;
        }

        private void SendMessage()
        {
            string msg = textBox3.Text;
            if (client == null) return;
            if (client.Connected == false) return;

            byte[] data = Encoding.UTF8.GetBytes(msg);

            int len = data.Length;
            byte[] buffer = BitConverter.GetBytes(len);

            client.GetStream().Write(buffer 0 4);
            client.GetStream().Write(data 0 data.Length);
        }
    }
}

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

     文件        679  2017-02-09 14:27  TcpListen\.git\config

     文件         73  2017-02-09 14:27  TcpListen\.git\description

     文件         23  2017-02-09 14:27  TcpListen\.git\HEAD

     文件        177  2017-02-09 14:27  TcpListen\.git\hooks\README.sample

     文件       2416  2017-02-09 14:27  TcpListen\.git\index

     文件        113  2017-02-09 14:27  TcpListen\.git\info\exclude

     文件        336  2017-02-09 14:27  TcpListen\.git\logs\HEAD

     文件        336  2017-02-09 14:27  TcpListen\.git\logs\refs\heads\master

     文件        150  2017-02-09 14:27  TcpListen\.git\ms-persist.xml

     文件        573  2017-02-09 14:27  TcpListen\.git\objects\05\f6c9a7245d463fa58e4398a0202df8cb58c48e

     文件        309  2017-02-09 14:27  TcpListen\.git\objects\09\baee9e0bb2640ba2288045b7d7d0e4198315a8

     文件        317  2017-02-09 14:27  TcpListen\.git\objects\0c\0bad7b2e00f531454691a08bc426b14fed5a40

     文件        258  2017-02-09 14:27  TcpListen\.git\objects\17\e96a3afb423cd0997b9ef3b4680a90bbbdba01

     文件       1665  2017-02-09 14:27  TcpListen\.git\objects\1a\f7de150c99c12dd67a509fe57c10d63e4eeb04

     文件        751  2017-02-09 14:27  TcpListen\.git\objects\1f\f0c423042b46cb1d617b81efb715defbe8054d

     文件        188  2017-02-09 14:27  TcpListen\.git\objects\2f\737d0868e2a8dd90e24554e795842429203dd6

     文件        718  2017-02-09 14:27  TcpListen\.git\objects\36\b93f5a19e14fdd6ece467fba12de3142db156a

     文件        186  2017-02-09 14:27  TcpListen\.git\objects\39\645652af62950ebf3b28ec3a5400dcec30b1c4

     文件       1883  2017-02-09 14:27  TcpListen\.git\objects\3a\2238d6bfb9a40c1fdf8d76d3e53855b1d57118

     文件       1277  2017-02-09 14:27  TcpListen\.git\objects\3b\9d34d40c6ee220e9a9a66702b1d17474aee7c9

     文件        138  2017-02-09 14:27  TcpListen\.git\objects\3d\bff35f484e2710a41c17e8c0ac67ff80f48f35

     文件        157  2017-02-09 14:27  TcpListen\.git\objects\4e\b67874f59f81b4c5692a3a437b4463c3a5df7a

     文件        192  2017-02-09 14:27  TcpListen\.git\objects\50\bb9a033dd2615c96fa249dae34d7f5f506c5f9

     文件       1180  2017-02-09 14:27  TcpListen\.git\objects\53\fa48b4964e95775ff6619ebb8219d135875513

     文件        925  2017-02-09 14:27  TcpListen\.git\objects\68\6222d5cf597f4c8fd910d432b44c77fba951fc

     文件         89  2017-02-09 14:27  TcpListen\.git\objects\6b\cb267e12e2bfadb6cdd41ba155764bb043ccc1

     文件        153  2017-02-09 14:27  TcpListen\.git\objects\88\fa4027bda397de6bf19f0940e5dd6026c877f9

     文件       1264  2017-02-09 14:27  TcpListen\.git\objects\8d\549afdf8227dd5de267ec99bd6bd7162aea512

     文件        191  2017-02-09 14:27  TcpListen\.git\objects\96\a79789ecb8be5f4506f6117a7bc6be6b61ade7

     文件        185  2017-02-09 14:27  TcpListen\.git\objects\98\0634148358c61b1bad068804bcc1b3abca7f15

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

评论

共有 条评论