• 大小: 70.34 KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-11-09
  • 语言: C#
  • 标签: C#  TCP  16进制  

资源简介

基于TCP实现的局域网通信,这个软件是我给别人写的一个小Demo,他们是和硬件通信,所以有些16进制转换之类的!呵呵!

资源截图

代码片段和文件信息

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

namespace Client
{
    public partial class FormClient : Form
    {
        public FormClient()
        {
            InitializeComponent();
            this.hostName = this.textBoxIP.Text;
            this.port = int.Parse(this.textBoxPort.Text);
        }
        TcpClient client;
        string hostName;
        int port;

        private void buttonSend_Click(object sender EventArgs e)
        {
            try
            {
                using (this.client = new TcpClient(hostName port))
                {
                    using (NetworkStream stream = client.GetStream())
                    {
                        string[] numbers = this.textBoxMessage.Text.Trim().Split(‘ ‘);
                        List buffer = new List();
                        for (int i = 0; i < numbers.Length; i++)
                        {
                            byte bit;
                            if (this.IsByte(numbers[i] out bit))
                            {
                                buffer.Add(bit);
                            }
                        }
                        stream.Write(buffer.ToArray() 0 buffer.Count);
                    }
                }
            }
            catch (IOException)
            {
                MessageBox.Show(“无法发送数据,远程主机已经关闭!“);
            }
            catch (SocketException)
            {
                MessageBox.Show(“服务端未开启!“);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FormClient_FormClosing(object sender FormClosingEventArgs e)
        {
            if (this.client != null)
            {
                this.client.Close();
            }
        }

        private bool IsByte(string s out byte bit)
        {
            try
            {
                bit = byte.Parse(s System.Globalization.Numberstyles.AllowHexSpecifier);
                return true;
            }
            catch
            {
                bit = default(byte);
                return false;
            }
        }
    }
}

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

     文件      20480  2010-01-22 14:52  TCP_Demo\Application\Client.exe

     文件      22016  2010-01-22 14:52  TCP_Demo\Application\Client.pdb

     文件       5632  2005-12-08 14:51  TCP_Demo\Application\Client.vshost.exe

     文件      24576  2010-01-22 14:39  TCP_Demo\Application\Server.exe

     文件      26112  2010-01-22 14:39  TCP_Demo\Application\Server.pdb

     文件       5632  2005-12-08 14:51  TCP_Demo\Application\Server.vshost.exe

     文件       3286  2010-01-22 14:11  TCP_Demo\Client\Client.csproj

     文件        536  2010-01-22 14:11  TCP_Demo\Client\Client.csproj.user

     文件       2476  2010-01-22 14:52  TCP_Demo\Client\FormClient.cs

     文件       5698  2010-01-22 14:37  TCP_Demo\Client\FormClient.Designer.cs

     文件       5814  2010-01-22 14:37  TCP_Demo\Client\FormClient.resx

     文件        306  2010-01-22 14:52  TCP_Demo\Client\obj\Client.csproj.FileList.txt

     文件        905  2010-01-22 14:37  TCP_Demo\Client\obj\Debug\Client.csproj.GenerateResource.Cache

     文件      20480  2010-01-22 14:52  TCP_Demo\Client\obj\Debug\Client.exe

     文件        180  2010-01-22 14:37  TCP_Demo\Client\obj\Debug\Client.FormClient.resources

     文件      22016  2010-01-22 14:52  TCP_Demo\Client\obj\Debug\Client.pdb

     文件        180  2010-01-22 10:01  TCP_Demo\Client\obj\Debug\Client.Properties.Resources.resources

     文件       4608  2010-01-22 14:10  TCP_Demo\Client\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        470  2010-01-22 11:37  TCP_Demo\Client\Program.cs

     文件       1184  2010-01-22 09:06  TCP_Demo\Client\Properties\AssemblyInfo.cs

     文件       2834  2010-01-22 14:10  TCP_Demo\Client\Properties\Resources.Designer.cs

     文件       5612  2010-01-22 09:06  TCP_Demo\Client\Properties\Resources.resx

     文件       1102  2010-01-22 14:10  TCP_Demo\Client\Properties\Settings.Designer.cs

     文件        249  2010-01-22 09:06  TCP_Demo\Client\Properties\Settings.settings

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

     文件       3954  2010-01-22 14:39  TCP_Demo\Server\FormServer.cs

     文件       6563  2010-01-22 14:37  TCP_Demo\Server\FormServer.Designer.cs

     文件       5814  2010-01-22 14:37  TCP_Demo\Server\FormServer.resx

     文件        905  2010-01-22 14:37  TCP_Demo\Server\obj\Debug\Server.csproj.GenerateResource.Cache

     文件      24576  2010-01-22 14:39  TCP_Demo\Server\obj\Debug\Server.exe

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

评论

共有 条评论