• 大小: 90KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: C#
  • 标签: 聊天室  Socket  TCP  

资源简介

c#基于TCP协议的Socket通信完整实例(实现多人聊天室),通过C#winform实现多人聊天,同时服务器可以向指定客户端发送消息。非常实用哦!

资源截图

代码片段和文件信息

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 MainForm : Form
    {
        private IPEndPoint ServerInfo;
        private Socket ClientSocket;
        //信息接收缓存
        private Byte[] MsgBuffer;
        //信息发送存储
        private Byte[] MsgSend;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_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;
            UserName.Text = Environment.MachineName;
            ServerIP.Text = “127.0.0.1“;
        }

        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(8008));

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

                this.SysMsg.Text = “登录服务器成功!“;
                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) + “\n“);
                }
                ClientSocket.BeginReceive(MsgBuffer 0 MsgBuffer.Length 0 new AsyncCallback(ReceiveCallBack) null);

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

        }

        private void CmdExit_Click(object sender EventArgs 

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

     文件      13824  2011-08-25 10:29  Server\Server\bin\Debug\Server.exe

     文件      24064  2011-08-25 10:29  Server\Server\bin\Debug\Server.pdb

     文件      11608  2011-08-25 10:30  Server\Server\bin\Debug\Server.vshost.exe

     文件        490  2010-03-17 22:39  Server\Server\bin\Debug\Server.vshost.exe.manifest

     文件       5658  2011-08-25 10:29  Server\Server\MainForm.cs

     文件      11358  2011-08-25 10:19  Server\Server\MainForm.Designer.cs

     文件       5817  2011-08-25 10:19  Server\Server\MainForm.resx

     文件       5271  2011-08-25 10:29  Server\Server\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        360  2011-08-25 10:22  Server\Server\obj\x86\Debug\GenerateResource-ResGen.read.1.tlog

     文件        498  2011-08-25 10:22  Server\Server\obj\x86\Debug\GenerateResource-ResGen.write.1.tlog

     文件        591  2011-08-25 10:30  Server\Server\obj\x86\Debug\Server.csproj.FileListAbsolute.txt

     文件      13824  2011-08-25 10:29  Server\Server\obj\x86\Debug\Server.exe

     文件        180  2011-08-25 10:22  Server\Server\obj\x86\Debug\Server.MainForm.resources

     文件      24064  2011-08-25 10:29  Server\Server\obj\x86\Debug\Server.pdb

     文件        180  2011-08-25 10:10  Server\Server\obj\x86\Debug\Server.Properties.Resources.resources

     文件        470  2011-08-24 23:11  Server\Server\Program.cs

     文件       1344  2011-08-24 23:11  Server\Server\Properties\AssemblyInfo.cs

     文件       2862  2011-08-24 23:11  Server\Server\Properties\Resources.Designer.cs

     文件       5612  2011-08-24 23:11  Server\Server\Properties\Resources.resx

     文件       1091  2011-08-24 23:11  Server\Server\Properties\Settings.Designer.cs

     文件        249  2011-08-24 23:11  Server\Server\Properties\Settings.settings

     文件       3434  2011-08-24 23:12  Server\Server\Server.csproj

     文件        860  2011-08-24 23:11  Server\Server.sln

    ..A..H.     15872  2011-08-25 10:31  Server\Server.suo

     文件      12800  2011-08-25 10:29  Client\Client\bin\Debug\Client.exe

     文件      26112  2011-08-25 10:29  Client\Client\bin\Debug\Client.pdb

     文件      11600  2011-08-25 10:30  Client\Client\bin\Debug\Client.vshost.exe

     文件        490  2010-03-17 22:39  Client\Client\bin\Debug\Client.vshost.exe.manifest

     文件       3686  2011-08-12 11:05  Client\Client\Client.csproj

     文件       4628  2011-08-25 10:29  Client\Client\MainForm.cs

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

评论

共有 条评论