• 大小: 403KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-11
  • 语言: C#
  • 标签: c#  socket聊天  

资源简介

C# socket实现简单的聊天功能 C# socket实现简单的聊天功能 C# socket实现简单的聊天功能

资源截图

代码片段和文件信息

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;

namespace TalkingClient
{
    public partial class Login : Form
    {
        /// 
        /// ip地址
        /// 

        private IPAddress ipAddr;
        /// 
        /// 端口
        /// 

        private int port;
        public Login()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender EventArgs e)
        {
            this.Close();
        }
        private void Login_Load(object sender EventArgs e)
        {
            txtIP.Text = “192.168.10.45“;
            txtPort.Text = “8888“;
        }
        private void btnLogin_Click(object sender EventArgs e)
        {
            if (!ValidateInfo())
            {
                return;
            }
            //向服务器发出连接请求
            TCPConnection conn = new TCPConnection(ipAddr port);
            TcpClient tcpc= conn.Connect();
            if (tcpc == null)
            {
                MessageBox.Show(“无法连接服务器!“ 
                                          “错误“
                                          MessageBoxButtons.OK
                                          MessageBoxIcon.Exclamation);
            }
            else
            {
                NetworkStream netStream = tcpc.GetStream();//提供用于访问网络的基本数据线
                string userName=txtUserName.Text.Trim();
                //向服务器发送用户名以及确认身份
                netStream.Write(Encoding.Unicode.GetBytes(userName)0Encoding.Unicode.GetBytes(userName).Length);
                //获取登入结果并写入buffer
                byte[] buffer = new byte[50];
                netStream.Read(buffer 0 buffer.Length);
                string connResult = Encoding.Unicode.GetString(buffer).TrimEnd(‘\0‘);
                if (connResult.Equals(“cmd::Failure“))
                {
                    MessageBox.Show(“您的用户名已经被使用,请尝试其他用户名!“
                                    “温馨提示“
                                    MessageBoxButtons.OK
                                    MessageBoxIcon.Information);
                }
                else
                {
                    string serverName=ipAddr+“:“+port;
                    Talking talking = new Talking(userName serverName netStream);
                    talking.Owner = this;
                    this.Hide();
                    talking.Show();
                }
            }
        }
        /// 
        /// 验证登入信息
        /// 

        /// 
        private bool ValidateInfo()
        {
            if (!IPAddress.TryParse(txtIP.Text.Trim() out ipAddr))
            {
                MessageBox.Show(“IP地址不合法!“ 
                                           “温馨提示“
                 

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

     文件      10134  2011-08-05 14:50  TalkingSolution\TalkingClient\AquaOS.ico

     文件     169472  2011-08-05 16:27  TalkingSolution\TalkingClient\bin\Debug\TalkingClient.exe

     文件      50688  2011-08-05 16:27  TalkingSolution\TalkingClient\bin\Debug\TalkingClient.pdb

     文件      14328  2011-08-05 16:27  TalkingSolution\TalkingClient\bin\Debug\TalkingClient.vshost.exe

     文件        490  2007-07-21 01:33  TalkingSolution\TalkingClient\bin\Debug\TalkingClient.vshost.exe.manifest

     文件       4208  2011-08-05 15:47  TalkingSolution\TalkingClient\Login.cs

     文件       6630  2011-08-05 15:47  TalkingSolution\TalkingClient\Login.Designer.cs

     文件      21312  2011-08-05 15:47  TalkingSolution\TalkingClient\Login.resx

     文件       1696  2011-08-05 16:27  TalkingSolution\TalkingClient\obj\Debug\TalkingClient.csproj.FileListAbsolute.txt

     文件        983  2011-08-05 16:23  TalkingSolution\TalkingClient\obj\Debug\TalkingClient.csproj.GenerateResource.Cache

     文件     169472  2011-08-05 16:27  TalkingSolution\TalkingClient\obj\Debug\TalkingClient.exe

     文件      10700  2011-08-05 15:47  TalkingSolution\TalkingClient\obj\Debug\TalkingClient.Login.resources

     文件      50688  2011-08-05 16:27  TalkingSolution\TalkingClient\obj\Debug\TalkingClient.pdb

     文件     101742  2011-08-05 14:50  TalkingSolution\TalkingClient\obj\Debug\TalkingClient.Properties.Resources.resources

     文件      23792  2011-08-05 16:23  TalkingSolution\TalkingClient\obj\Debug\TalkingClient.Talking.resources

     文件       4608  2011-08-04 20:57  TalkingSolution\TalkingClient\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        474  2011-08-04 10:31  TalkingSolution\TalkingClient\Program.cs

     文件       1372  2011-08-04 09:12  TalkingSolution\TalkingClient\Properties\AssemblyInfo.cs

     文件       3240  2011-08-04 20:57  TalkingSolution\TalkingClient\Properties\Resources.Designer.cs

     文件       6423  2011-08-04 20:57  TalkingSolution\TalkingClient\Properties\Resources.resx

     文件       1098  2011-08-04 09:12  TalkingSolution\TalkingClient\Properties\Settings.Designer.cs

     文件        249  2011-08-04 09:12  TalkingSolution\TalkingClient\Properties\Settings.settings

     文件      10134  2011-08-05 11:43  TalkingSolution\TalkingClient\Resources\AquaOS.ico

     文件       7258  2011-08-04 20:56  TalkingSolution\TalkingClient\Resources\msg.wav

     文件      94252  2011-08-04 20:56  TalkingSolution\TalkingClient\Resources\nudge.wav

     文件      17164  2011-08-05 16:27  TalkingSolution\TalkingClient\Talking.cs

     文件      14931  2011-08-05 16:23  TalkingSolution\TalkingClient\Talking.Designer.cs

     文件      40987  2011-08-05 16:23  TalkingSolution\TalkingClient\Talking.resx

     文件       4013  2011-08-05 14:50  TalkingSolution\TalkingClient\TalkingClient.csproj

     文件        912  2011-08-04 11:06  TalkingSolution\TalkingClient\TCPConnection.cs

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

评论

共有 条评论