• 大小: 100KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: C#
  • 标签: c#  Socket  聊天  

资源简介

C# Socket编程,实现客户端与服务端发送信息(聊天),需要的朋友可以下载学习。亲自编码完成(测试成功)。使用VS2012编码工具完成。喝水不忘挖井人,感谢北盟网(www.bamm.cn)。。。

资源截图

代码片段和文件信息

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

namespace Client
{
    public partial class client : Form
    {
        public client()
        {
            InitializeComponent();
        }
        //创建全局变量
        TcpClient tcpClient;

        private void button2_Click(object sender EventArgs e)
        {
            tcpClient = new TcpClient();
            try
            {
                tcpClient.Connect(this.textBox3.Text Convert.ToInt32(this.textBox4.Text));
                this.textBox1.Text = “连接成功“ + DateTime.Now.ToShortTimeString() + “\r\n“ + this.textBox1.Text;
                this.backgroundWorker1.RunWorkerAsync();
            }
            catch (Exception ex)
            {

                MessageBox.Show(“连接失败“ + ex.Message);
            }
        }

        private void button1_Click(object sender EventArgs e)
        {
            this.textBox1.Text = “发送:“ +this.textBox2.Text+ DateTime.Now.ToShortTimeString() + “\r\n“ + this.textBox1.Text;
            NetworkStream stream = tcpClient.GetStream();byte[] byteArray = Encoding.Unicode.GetBytes(this.textBox2.Text);
            stream.Write(byteArray 0 byteArray.Length);//发送字节数组
        }

        private void backgroundWorker1_DoWork(object sender DoWorkEventArgs e)
        {
            NetworkStream stream = tcpClient.GetStream();
            byte[] byteArray = new byte[1024];
            while (true)
            {
                int lengh = stream.Read(byteArray 0 1024);//读取字节数组
                //lengh其实就是客户端发送字节的数组长度
                string str = Encoding.Unicode.GetString(byteArray 0 lengh);
                this.textBox1.Text = “接收到:“ + str + DateTime.Now.ToShortTimeString() + “\r\n“ + this.textBox1.Text;
            }
        }
    }
}

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

     文件        187  2019-05-26 21:13  C#Socket编程\Client\Client\App.config

     文件      10752  2019-05-26 22:56  C#Socket编程\Client\Client\bin\Debug\Client.exe

     文件        187  2019-05-26 21:13  C#Socket编程\Client\Client\bin\Debug\Client.exe.config

     文件      26112  2019-05-26 22:56  C#Socket编程\Client\Client\bin\Debug\Client.pdb

     文件      22984  2019-05-26 21:13  C#Socket编程\Client\Client\bin\Debug\Client.vshost.exe

     文件        187  2019-05-26 21:13  C#Socket编程\Client\Client\bin\Debug\Client.vshost.exe.config

     文件        490  2018-09-15 15:29  C#Socket编程\Client\Client\bin\Debug\Client.vshost.exe.manifest

     文件       2101  2019-05-26 22:58  C#Socket编程\Client\Client\client.cs

     文件       3796  2019-05-26 22:56  C#Socket编程\Client\Client\Client.csproj

     文件       6027  2019-05-26 22:56  C#Socket编程\Client\Client\client.Designer.cs

     文件       6022  2019-05-26 22:56  C#Socket编程\Client\Client\client.resx

     文件        180  2019-05-26 22:56  C#Socket编程\Client\Client\obj\Debug\Client.client.resources

     文件        609  2019-05-26 22:56  C#Socket编程\Client\Client\obj\Debug\Client.csproj.FileListAbsolute.txt

     文件       1034  2019-05-26 22:56  C#Socket编程\Client\Client\obj\Debug\Client.csproj.GenerateResource.Cache

     文件      10752  2019-05-26 22:56  C#Socket编程\Client\Client\obj\Debug\Client.exe

     文件      26112  2019-05-26 22:56  C#Socket编程\Client\Client\obj\Debug\Client.pdb

     文件        180  2019-05-26 22:56  C#Socket编程\Client\Client\obj\Debug\Client.Properties.Resources.resources

     文件        863  2019-05-26 21:13  C#Socket编程\Client\Client\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6983  2019-05-26 22:56  C#Socket编程\Client\Client\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2019-05-26 21:13  C#Socket编程\Client\Client\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2019-05-26 21:13  C#Socket编程\Client\Client\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2019-05-26 21:13  C#Socket编程\Client\Client\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        519  2019-05-26 22:56  C#Socket编程\Client\Client\Program.cs

     文件       1332  2019-05-26 21:13  C#Socket编程\Client\Client\Properties\AssemblyInfo.cs

     文件       2864  2019-05-26 21:13  C#Socket编程\Client\Client\Properties\Resources.Designer.cs

     文件       5612  2019-05-26 21:13  C#Socket编程\Client\Client\Properties\Resources.resx

     文件       1093  2019-05-26 21:13  C#Socket编程\Client\Client\Properties\Settings.Designer.cs

     文件        249  2019-05-26 21:13  C#Socket编程\Client\Client\Properties\Settings.settings

     文件        908  2019-05-26 21:13  C#Socket编程\Client\Client.sln

    ..A..H.     29184  2019-05-26 22:59  C#Socket编程\Client\Client.v11.suo

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

评论

共有 条评论