• 大小: 117KB
    文件类型: .rar
    金币: 2
    下载: 0 次
    发布日期: 2024-01-07
  • 语言: 其他
  • 标签: C#  

资源简介

利用同步TCP编写网络聊天程序 很值得学习

资源截图

代码片段和文件信息

using System;
using System.Windows.Forms;
//添加的命名空间引用
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace SyncChatClient
{
    public partial class MainForm : Form
    {
        private bool isExit = false;
        private TcpClient client;
        private BinaryReader br;
        private BinaryWriter bw;
        public MainForm()
        {
            InitializeComponent();
            Random r = new Random((int)DateTime.Now.Ticks);
            textBoxUserName.Text = “user“ + r.Next(100 999);
            listBoxOnlineStatus.HorizontalScrollbar = true;

        }
        /// 
        /// 【连接服务器】按钮的Click事件
        /// 

        private void buttonConnect_Click(object sender EventArgs e)
        {
            buttonConnect.Enabled = false;
            try
            {
                //此处为方便演示,实际使用时要将Dns.GetHostName()改为服务器域名
                client = new TcpClient(Dns.GetHostName() 51888);
                AddTalkMessage(“连接成功“);
            }
            catch
            {
                AddTalkMessage(“连接失败“);
                buttonConnect.Enabled = true;
                return;
            }
            //获取网络流
            NetworkStream networkStream = client.GetStream();
            //将网络流作为二进制读写对象
            br = new BinaryReader(networkStream);
            bw = new BinaryWriter(networkStream);
            SendMessage(“Login“ + textBoxUserName.Text);
            Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
            //threadReceive.IsBackground = true;
            threadReceive.Start();
        }
        /// 处理接收的服务器端数据
        private void ReceiveData()
        {
            string receiveString = null;
            while (isExit == false)
            {
                try
                {
                    //从网络流中读出字符串
                    //此方法会自动判断字符串长度前缀,并根据长度前缀读出字符串
                    receiveString = br.ReadString();
                }
                catch
                {
                    if (isExit == false)
                    {
                        MessageBox.Show(“与服务器失去联系。“);
                    }
                    break;
                }
                string[] splitString= receiveString.Split(‘‘);
                string command = splitString[0].ToLower();
                switch (command)
                {
                    case “login“:  //格式:login用户名
                        AddOnline(splitString[1]);
                        break;
                    case “logout“:  //格式:logout用户名
                        RemoveUserName(splitString[1]);
                        break;
                    case “talk“:  //格式:talk用户名对话信息
                        //AddTalkMessage(splitString[1] + “:\r\n“);
                        //AddTalkMessage(receiveString.Substring(
                        //    splitString[0].Length + splitString[1].Lengt

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

     文件      13824  2010-08-08 15:23  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\bin\Debug\SyncChatClient.exe

     文件      46592  2010-08-08 15:23  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\bin\Debug\SyncChatClient.pdb

     文件      14328  2010-08-09 08:47  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\bin\Debug\SyncChatClient.vshost.exe

     文件        490  2007-07-21 01:33  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\bin\Debug\SyncChatClient.vshost.exe.manifest

     文件       7733  2010-08-08 15:23  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\MainForm.cs

     文件       7954  2009-04-10 20:35  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\MainForm.Designer.cs

     文件       5814  2009-04-10 20:35  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\MainForm.resx

     文件       4604  2010-08-09 08:47  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\Debug\SyncChatClient.csproj.FileListAbsolute.txt

     文件        850  2009-04-10 20:35  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\Debug\SyncChatClient.csproj.GenerateResource.Cache

     文件      13824  2010-08-08 15:23  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\Debug\SyncChatClient.exe

     文件        180  2009-04-10 20:35  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\Debug\SyncChatClient.MainForm.resources

     文件      46592  2010-08-08 15:23  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\Debug\SyncChatClient.pdb

     文件        180  2009-04-08 02:47  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\Debug\SyncChatClient.Properties.Resources.resources

     文件       4608  2009-04-08 01:40  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件         60  2008-05-05 00:01  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\SyncChatClient.csproj.FileList.txt

     文件       2704  2008-08-31 03:01  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\obj\SyncChatClient.csproj.FileListAbsolute.txt

     文件        476  2008-03-20 00:33  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\Program.cs

     文件       1200  2008-03-20 00:33  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\Properties\AssemblyInfo.cs

     文件       2852  2008-09-08 12:59  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\Properties\Resources.Designer.cs

     文件       5612  2008-03-20 00:33  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\Properties\Resources.resx

     文件       1112  2008-09-08 12:59  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\Properties\Settings.Designer.cs

     文件        249  2008-03-20 00:33  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\Properties\Settings.settings

     文件       3458  2008-09-08 12:59  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient\SyncChatClient.csproj

     文件        932  2008-09-08 12:59  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient.sln

    ..A..H.     24576  2010-08-09 08:47  利用同步TCP编写网络聊天程序\SyncChatClient\SyncChatClient.suo

     文件      13824  2010-08-08 15:10  利用同步TCP编写网络聊天程序\SyncChatServer\SyncChatServer\bin\Debug\SyncChatServer.exe

     文件      46592  2010-08-08 15:10  利用同步TCP编写网络聊天程序\SyncChatServer\SyncChatServer\bin\Debug\SyncChatServer.pdb

     文件      14328  2010-08-08 15:24  利用同步TCP编写网络聊天程序\SyncChatServer\SyncChatServer\bin\Debug\SyncChatServer.vshost.exe

     文件        490  2007-07-21 01:33  利用同步TCP编写网络聊天程序\SyncChatServer\SyncChatServer\bin\Debug\SyncChatServer.vshost.exe.manifest

     文件       9200  2009-12-06 22:08  利用同步TCP编写网络聊天程序\SyncChatServer\SyncChatServer\MainForm.cs

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

评论

共有 条评论