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

资源简介

类似于QQ的简易聊天工具,QQ的主要通信功能以实现,适合新手练习的,

资源截图

代码片段和文件信息

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;

namespace Client
{
    public partial class ClientMain : Form
    {
        public ClientMain()
        {
            InitializeComponent();
        }
        private IPEndPoint ServerInfo;
        private Socket ClientSocket;
        //信息接收缓存
        private Byte[] MsgBuffer;
        //信息发送存储
        private Byte[] MsgSend;
        private void ClientMain_Load(object sender EventArgs e)
        {
            this.CmdSend.Enabled = false;
            this.CmdExit.Enabled = false;
            //定义一个IPV4,TCP模式的Socket
            ClientSocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            MsgBuffer = new Byte[65535];
            MsgSend = new Byte[65535];
            //允许子线程刷新数据
            CheckForIllegalCrossThreadCalls = false;
            this.RecieveMsg.Text = Environment.MachineName;
        }

        private void CmdEnter_Click(object sender EventArgs e)
        {
            //服务端IP和端口信息设定这里的IP可以是127.0.0.1,可以是本机局域网IP,也可以是本机网络IP
            ServerInfo = new IPEndPoint(IPAddress.Parse(this.ServerIP.Text) Convert.ToInt32(this.ServerPort.Text));

            try
            {
                //客户端连接服务端指定IP端口,Sockket
                ClientSocket.Connect(ServerInfo);
                //将用户登录信息发送至服务器,由此可以让其他客户端获知
                ClientSocket.Send(Encoding.Unicode.GetBytes(“用户: “ + this.RecieveMsg.Text + “ 进入系统!\n“));
                //开始从连接的Socket异步读取数据。接收来自服务器,其他客户端转发来的信息
                //AsyncCallback引用在异步操作完成时调用的回调方法
                ClientSocket.BeginReceive(MsgBuffer 0 MsgBuffer.Length SocketFlags.None new AsyncCallback(ReceiveCallBack) null);

                this.SysMsg.Text += “登录服务器成功!\n“;
                this.CmdSend.Enabled = true;
                this.CmdEnter.Enabled = false;
                this.CmdExit.Enabled = true;
            }
            catch
            {
                MessageBox.Show(“登录服务器失败,请确认服务器是否正常工作!“);
            }

        }

        private void ReceiveCallBack(IAsyncResult AR)
        {
            try
            {
                //结束挂起的异步读取,返回接收到的字节数。 AR,它存储此异步操作的状态信息以及所有用户定义数据
                int REnd = ClientSocket.EndReceive(AR);

                lock (this.RecieveMsg)
                {                    
                    this.RecieveMsg.AppendText(Encoding.Unicode.GetString(MsgBuffer 0 REnd));
                }
                ClientSocket.BeginReceive(MsgBuffer 0 MsgBuffer.Length 0 new AsyncCallback(ReceiveCallBack) null);

            }
            catch
            {
                MessageBox.Show(“已经与服务器断开连接!“);
                this.Close();
            }

        }
        //点击发送之后没有直接添加到信息列表中,而是传到服务器,由服务器转发给每个客户端
    

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

     文件      28958  2012-09-28 11:00  5.Socket\C# Socket .txt

     文件      12800  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.exe

     文件      24064  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.pdb

     文件      11600  2017-07-25 16:14  5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.vshost.exe

     文件        490  2015-06-04 12:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.vshost.exe.manifest

     文件       3696  2012-09-24 17:53  5.Socket\Prj_Socket_Chat_聊天通信\Client\Client.csproj

     文件       5195  2012-09-28 11:52  5.Socket\Prj_Socket_Chat_聊天通信\Client\ClientMain.cs

     文件       8198  2012-09-24 17:58  5.Socket\Prj_Socket_Chat_聊天通信\Client\ClientMain.Designer.cs

     文件       5817  2012-09-24 17:58  5.Socket\Prj_Socket_Chat_聊天通信\Client\ClientMain.resx

     文件        180  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.ClientMain.resources

     文件       2002  2017-07-25 16:14  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.csproj.FileListAbsolute.txt

     文件      12800  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.exe

     文件      24064  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.pdb

     文件        180  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.Properties.Resources.resources

     文件       2152  2012-09-28 11:17  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6426  2017-07-25 16:14  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        578  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\GenerateResource.read.1.tlog

     文件       1346  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\GenerateResource.write.1.tlog

     文件        492  2012-09-24 17:35  5.Socket\Prj_Socket_Chat_聊天通信\Client\Program.cs

     文件       1364  2012-09-24 17:34  5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\AssemblyInfo.cs

     文件       2862  2012-09-24 17:34  5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Resources.Designer.cs

     文件       5612  2012-09-24 17:34  5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Resources.resx

     文件       1091  2012-09-24 17:34  5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Settings.Designer.cs

     文件        249  2012-09-24 17:34  5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Settings.settings

     文件       1300  2012-09-24 17:39  5.Socket\Prj_Socket_Chat_聊天通信\Prj_Socket.sln

    ..A..H.     37376  2012-09-28 11:52  5.Socket\Prj_Socket_Chat_聊天通信\Prj_Socket.suo

     文件      11776  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Server\bin\Debug\Server.exe

     文件      34304  2012-09-24 20:48  5.Socket\Prj_Socket_Chat_聊天通信\Server\bin\Debug\Server.pdb

     文件      11600  2012-09-24 17:45  5.Socket\Prj_Socket_Chat_聊天通信\Server\bin\Debug\Server.vshost.exe

     文件       2152  2012-09-24 17:33  5.Socket\Prj_Socket_Chat_聊天通信\Server\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

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

评论

共有 条评论