• 大小: 5.69MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-11-16
  • 语言: C#
  • 标签:

资源简介

C#开发的聊天小程序 利用了线程和socket方法 程序是本人自己的作品保证可以运行 下载者可放心使用。这需要用C#开发软件大开就可以。

资源截图

代码片段和文件信息

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

namespace Client
{
    public partial class frmClient : Form
    {
        private bool _starting = false;//volatile
        private string _ip;
        private int _port;
        private Socket _client;
        private Thread _thread;

        public delegate void UpdateDisplay(string message);

        public frmClient()
        {
            InitializeComponent();
        }

        private void frmClient_Load(object sender EventArgs e)
        {
            this.MaximizeBox = false;
            this.FormBorderstyle = FormBorderstyle.FixedSingle;
            this.StartPosition = FormStartPosition.CenterScreen;

            //IPAddress[] ipAddresses = Dns.GetHostAddresses(Dns.GetHostName());
            //this.txtSendIP.Text = ipAddresses[0].ToString();
            txtSendIP.Text = “127.0.0.1“;
            txtPort.Text = “1234“;
        }

        private void btnlink_Click(object sender EventArgs e)
        {
            if (TextIsEmpty())
            {
                return;
            }
            else
            {
                _ip = this.txtSendIP.Text;
                _port = Convert.ToInt32(this.txtPort.Text.Trim());
                InitClient();
                btnlink.Enabled = false;
                btnSend.Enabled = true;
            }
        }

        private void btnSend_Click(object sender EventArgs e)
        {
            if (txtSend.Text == ““)
            {
                MessageBox.Show(“不能发送空消息“);
                return;
            }
            else
            {
                try
                {
                    if (_client == null)
                        return;
                    byte[] data = Encoding.Unicode.GetBytes(this.txtSend.Text);
                    _client.Send(data data.Length SocketFlags.None);
                    txtReceive.AppendText(“客户端: “ + txtSend.Text + Environment.NewLine);
                    txtSend.Text = ““;
                }
                catch (Exception)
                {
                }
            }
        }

        private void frmClient_FormClosing(object sender FormClosingEventArgs e)
        {
            this.CloseAll();
        }

        //方法
        private bool TextIsEmpty()
        {
            if (txtSendIP.Text == ““ || txtSendIP.Text.Length == 0)
            {
                MessageBox.Show(“IP地址必须填写格式:xxx.xxx.xxx.xxx“);
                return true;
            }
            if (txtPort.Text == ““ || txtPort.Text.Length == 0)
            {
                MessageBox.Show(“请填写端口号建议1024-65535之间“);
                return true;
            }
            return false;
        }

        private void InitClient()
      

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

     文件      31744  2011-05-06 10:48  项目四聊天工具\C#跨线程.doc

     文件       1379  2011-05-04 22:49  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\ChatExp_SocketDemo.sln

    ..A..H.     25600  2011-05-11 11:42  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\ChatExp_SocketDemo.suo

     文件      24576  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\bin\Debug\Client.exe

     文件      26112  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\bin\Debug\Client.pdb

     文件       5632  2011-05-04 23:26  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\bin\Debug\Client.vshost.exe

     文件       3239  2011-05-04 22:52  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\Client.csproj

     文件       4702  2011-05-04 23:19  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\frmClient.cs

     文件       7531  2011-05-04 23:19  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\frmClient.Designer.cs

     文件       7751  2011-05-04 23:19  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\frmClient.resx

     文件        265  2011-05-11 09:39  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\obj\Client.csproj.FileList.txt

     文件        657  2011-05-04 23:26  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\obj\Client.csproj.FileListAbsolute.txt

     文件        846  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\obj\Debug\Client.csproj.GenerateResource.Cache

     文件      24576  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\obj\Debug\Client.exe

     文件        180  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\obj\Debug\Client.frmClient.resources

     文件      26112  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\obj\Debug\Client.pdb

     文件        180  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\obj\Debug\Client.Properties.Resources.resources

     文件        469  2011-05-04 22:51  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\Program.cs

     文件       1178  2011-05-04 22:48  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\Properties\AssemblyInfo.cs

     文件       2868  2011-05-04 22:48  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\Properties\Resources.Designer.cs

     文件       5612  2011-05-04 22:48  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\Properties\Resources.resx

     文件       1091  2011-05-04 22:48  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\Properties\Settings.Designer.cs

     文件        249  2011-05-04 22:48  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Client\Properties\Settings.settings

     文件      24576  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Server\bin\Debug\Server.exe

     文件      24064  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Server\bin\Debug\Server.pdb

     文件       5632  2011-05-04 23:34  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Server\bin\Debug\Server.vshost.exe

     文件       4723  2011-05-04 23:25  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Server\frmServer.cs

     文件       6534  2011-05-04 22:53  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Server\frmServer.Designer.cs

     文件       7758  2011-05-04 22:53  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Server\frmServer.resx

     文件        846  2011-05-11 08:32  项目四聊天工具\ChatExp_SocketDemo\ChatExp_SocketDemo\Server\obj\Debug\Server.csproj.GenerateResource.Cache

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

评论

共有 条评论