• 大小: 100KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-09
  • 语言: C#
  • 标签: C#  Socket  通信  Tcp协议  demo  

资源简介

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 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-11-17 19:55  TCP.Demo\
     目录           0  2011-08-25 10:31  TCP.Demo\Client\
     目录           0  2011-08-25 10:31  TCP.Demo\Client\Client\
     文件        3686  2011-08-12 11:05  TCP.Demo\Client\Client\Client.csproj
     文件        8981  2011-08-25 10:25  TCP.Demo\Client\Client\MainForm.Designer.cs
     文件        4628  2011-08-25 10:29  TCP.Demo\Client\Client\MainForm.cs
     文件        5817  2011-08-25 10:25  TCP.Demo\Client\Client\MainForm.resx
     文件         490  2011-08-12 10:58  TCP.Demo\Client\Client\Program.cs
     目录           0  2011-08-25 10:31  TCP.Demo\Client\Client\Properties\
     文件        1344  2011-08-12 10:53  TCP.Demo\Client\Client\Properties\AssemblyInfo.cs
     文件        2862  2011-08-12 10:53  TCP.Demo\Client\Client\Properties\Resources.Designer.cs
     文件        5612  2011-08-12 10:53  TCP.Demo\Client\Client\Properties\Resources.resx
     文件        1091  2011-08-12 10:53  TCP.Demo\Client\Client\Properties\Settings.Designer.cs
     文件         249  2011-08-12 10:53  TCP.Demo\Client\Client\Properties\Settings.settings
     目录           0  2011-08-25 10:31  TCP.Demo\Client\Client\bin\
     目录           0  2011-08-25 10:31  TCP.Demo\Client\Client\bin\Debug\
     文件       12800  2011-08-25 10:29  TCP.Demo\Client\Client\bin\Debug\Client.exe
     文件       26112  2011-08-25 10:29  TCP.Demo\Client\Client\bin\Debug\Client.pdb
     文件       11600  2011-08-25 10:30  TCP.Demo\Client\Client\bin\Debug\Client.vshost.exe
     文件         490  2010-03-17 22:39  TCP.Demo\Client\Client\bin\Debug\Client.vshost.exe.manifest
     目录           0  2018-11-17 19:56  TCP.Demo\Client\Client\bin\Release\
     目录           0  2011-08-25 10:31  TCP.Demo\Client\Client\obj\
     目录           0  2011-08-25 10:31  TCP.Demo\Client\Client\obj\x86\
     目录           0  2011-08-25 10:31  TCP.Demo\Client\Client\obj\x86\Debug\
     文件         180  2011-08-25 10:27  TCP.Demo\Client\Client\obj\x86\Debug\Client.MainForm.resources
     文件         180  2011-08-24 23:25  TCP.Demo\Client\Client\obj\x86\Debug\Client.Properties.Resources.resources
     文件        1253  2011-08-25 10:30  TCP.Demo\Client\Client\obj\x86\Debug\Client.csproj.FileListAbsolute.txt
     文件       12800  2011-08-25 10:29  TCP.Demo\Client\Client\obj\x86\Debug\Client.exe
     文件       26112  2011-08-25 10:29  TCP.Demo\Client\Client\obj\x86\Debug\Client.pdb
     文件        4440  2011-08-24 22:58  TCP.Demo\Client\Client\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6181  2011-08-25 10:29  TCP.Demo\Client\Client\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
............此处省略40个文件信息

评论

共有 条评论