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

资源简介

简单通信程序

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace TCPClient
{
    public partial class frmSyncTCPClient : Form
    {
        #region 变量
        // 申明变量
        private TcpClient tcpClient = null;
        private NetworkStream networkStream = null;
        private BinaryReader reader;
        private BinaryWriter writer;
        
        // 申明委托
        // 显示消息
        private delegate void ShowMessage(string str);
        private ShowMessage showMessageCallback;

        // 显示状态
        private delegate void ShowStatus(string str);
        private ShowStatus showStatusCallBack;
        
        // 清空消息
        private delegate void ResetMessage();
        private ResetMessage resetMessageCallBack;

        #endregion 

        public frmSyncTCPClient()
        {
            InitializeComponent();

            #region 实例化委托
            // 显示消息
            showMessageCallback = new ShowMessage(showMessage);

            // 显示状态
            showStatusCallBack = new ShowStatus(showStatus);       

            // 重置消息
            resetMessageCallBack = new ResetMessage(resetMessage);
            #endregion               
        }

        #region 定义回调函数

        // 显示消息
        private void showMessage(string str)
        {
            lstbxMessageView.Items.Add(tcpClient.Client.RemoteEndPoint);
            lstbxMessageView.Items.Add(str);
            lstbxMessageView.TopIndex = lstbxMessageView.Items.Count - 1;
        }

        // 显示状态
        private void showStatus(string str)
        {
            toolStripStatusInfo.Text = str;
        }
         
        // 清空消息
        private void resetMessage()
        {
            tbxMessage.Text = ““;
            tbxMessage.Focus();
        }

        #endregion 

        #region 点击事件方法
        private void btnConnect_Click(object sender EventArgs e)
        {
            // 通过一个线程发起请求多线程
            Thread connectThread = new Thread(ConnectToServer);
            connectThread.Start();
        }

        // 连接服务器方法建立连接的过程
        private void ConnectToServer()
        {
            try
            {
                // 调用委托
                statusStripInfo.Invoke(showStatusCallBack “正在连接...“);
                if (tbxserverIp.Text == string.Empty || tbxPort.Text == string.Empty)
                {
                    MessageBox.Show(“请先输入服务器的IP地址和端口号“);
                }

                IPAddress ipaddress = IPAddress.Parse(tbxserverIp.Text);
                tcpClient = new TcpClient();             
                tcpClient.Connect(ipaddress int.Parse(tbxPort.Text));               
                
                // 延时操作
                Thread.Sleep(1000);
             

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

     文件      14848  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\bin\Debug\TCPClient.exe

     文件      28160  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\bin\Debug\TCPClient.pdb

     文件      24224  2015-09-07 14:17  简单通信程序-tcp\TCPClient\TCPClient\bin\Debug\TCPClient.vshost.exe

     文件        490  2015-07-10 19:01  简单通信程序-tcp\TCPClient\TCPClient\bin\Debug\TCPClient.vshost.exe.manifest

     文件       7271  2015-09-07 15:20  简单通信程序-tcp\TCPClient\TCPClient\Form1.cs

     文件      10257  2015-09-07 14:09  简单通信程序-tcp\TCPClient\TCPClient\Form1.Designer.cs

     文件       6193  2015-09-07 15:20  简单通信程序-tcp\TCPClient\TCPClient\Form1.resx

     文件       2873  2015-09-07 14:19  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7177  2015-09-07 14:16  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1651  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\SyncTCPClient.csproj.FileListAbsolute.txt

     文件        977  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\SyncTCPClient.csproj.GenerateResource.Cache

     文件       9180  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\SyncTCPClient.csprojResolveAssemblyReference.cache

     文件      14848  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\TCPClient.exe

     文件        180  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\TCPClient.frmSyncTCPClient.resources

     文件      28160  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\TCPClient.pdb

     文件        180  2015-09-07 14:41  简单通信程序-tcp\TCPClient\TCPClient\obj\x86\Debug\TCPClient.Properties.Resources.resources

     文件        512  2012-08-25 15:57  简单通信程序-tcp\TCPClient\TCPClient\Program.cs

     文件       1438  2012-08-25 13:17  简单通信程序-tcp\TCPClient\TCPClient\Properties\AssemblyInfo.cs

     文件       2848  2012-08-25 13:17  简单通信程序-tcp\TCPClient\TCPClient\Properties\Resources.Designer.cs

     文件       5612  2012-08-25 13:17  简单通信程序-tcp\TCPClient\TCPClient\Properties\Resources.resx

     文件       1096  2012-08-25 13:17  简单通信程序-tcp\TCPClient\TCPClient\Properties\Settings.Designer.cs

     文件        249  2012-08-25 13:17  简单通信程序-tcp\TCPClient\TCPClient\Properties\Settings.settings

     文件       3677  2015-08-28 16:03  简单通信程序-tcp\TCPClient\TCPClient\SyncTCPClient.csproj

     文件        877  2012-08-25 17:48  简单通信程序-tcp\TCPClient\TCPClient.sln

    ..A..H.     18432  2012-08-25 17:48  简单通信程序-tcp\TCPClient\TCPClient.suo

    ..A..H.     39936  2015-09-07 14:09  简单通信程序-tcp\TCPClient\TCPClient.v11.suo

    ..A..H.     40960  2015-09-07 15:20  简单通信程序-tcp\TCPClient\TCPClient.v12.suo

     文件      14848  2015-09-07 15:08  简单通信程序-tcp\TCPServer\TCPServer\bin\Debug\TCPServer.exe

     文件      34304  2015-09-07 15:08  简单通信程序-tcp\TCPServer\TCPServer\bin\Debug\TCPServer.pdb

     文件      24224  2015-09-07 15:11  简单通信程序-tcp\TCPServer\TCPServer\bin\Debug\TCPServer.vshost.exe

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

评论

共有 条评论

相关资源