ASP.net(C#)实现简易聊天室功能

本文实例为大家分享了ASP.net(C#)实现简易聊天室功能的具体代码,供大家参考,具体内容如下

1.搭建框架

<html >
<head>
    <title>聊天系统</title>
</head>
    <frameset rows="80%,20%" >
          <frameset cols="20%,80%">
          <frame src="Register.aspx" />
               <frame src="main.aspx" /> 
                     </frameset>
               <frame src="login.aspx"/>

      </frameset>

<body >
 
</body>
</html>

2.框架涉及三个页面

建立相应的页面布局:

1.login.asp

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class login : System.Web.UI.Page
{undefined
    protected void Page_Load(object sender, EventArgs e)
    {undefined

    }
    protected void LoginBtn_Click(object sender, EventArgs e)
    {undefined
        if (LoginID.Text.Trim() == string.Empty)
        {undefined
            Response.Write("<script>alert('请输入用户名!')</script>");
            return;
        }
        if (LoginPwd.Text!= "123456")
        {undefined
            Response.Write("<script>alert('密码不正确,请重新输入')</script>");
            return;
        }
        if (!IfLonined())
        {undefined
            Response.Write("<script>alert('用户名已经存在')</script>");
            return;
        }
        Session["username"] = LoginID.Text;
        if (Application["user"] == null)
        {undefined
            Application["user"] = Session["username"];
        }
        else {undefined
            Application["user"] += "," + Session["username"];

        }
        Response.Redirect("send.aspx");

    }
    protected bool IfLonined()
    {undefined
        Application.Lock();
        string users;
        string[]user;
        if (Application["user"]!=null)
        {undefined
            users = Application["user"].ToString();
            user = users.Split(',');
            foreach(string s in user)
            {undefined
                if(s==LoginID.Text.Trim().ToString())
                {undefined
                    return false;
                }
            }
        }
        Application.UnLock();
        return true;
    }
    protected void LoginPWD_TextChanged(object sender, EventArgs e)
    {undefined

}
}

2.Register.asp

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Register : System.Web.UI.Page
{undefined
    protected ArrayList ItemList = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {undefined
        Response.AddHeader("Refresh", "1");
        Application.Lock();
        string users;
        string[] user;
        if (Application["user"]!=null)
        {undefined
            users = Application["user"].ToString();
            user = users.Split(',');
            for(int i=user.Length-1;i>=0;i--)
            {undefined
                ItemList.Add(user[i].ToString());

            }
            UserList.DataSource = ItemList;
            UserList.DataBind();
        }
        Application.UnLock();
    }
    protected void UserList_SelectedIndexChanged(object sender, EventArgs e)
    {undefined

    }
}

3.send.asp

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class send : System.Web.UI.Page
{undefined
    protected void Page_Load(object sender, EventArgs e)
    {undefined
        if (Session["username"] != null)
        {undefined
            Username.Text = Session["username"].ToString() + "说:";
        }
        else
        {undefined
            Response.Redirect("login.aspx");
        }

    }
    protected void SendBtn_Click(object sender, EventArgs e)
    {undefined
        string message;
        message = "<font color='blue'>" + Session["username"].ToString() + "</font>说:";
        message += Message.Text;
        message += "(<i>" + DateTime.Now.ToString() + "</i>)";
        message += "<br>";
        Application.Lock();
        if (chk.Checked)
            Application["chatcontent"] = (string)Application["chatcontent"] + message + "<img src=image/00.gif>" + "<img src=image/01.gif>";
        else
            Application["chatcontent"] = (string)Application["chatcontent"] + message;
      
        Application.UnLock();
        Message.Text = null;
    }
    protected void LoginBtn_Click(object sender, EventArgs e)
    {undefined
        Response.Redirect("login.aspx");
    }
    protected void LoginOutBtn_Click(object sender, EventArgs e)
    {undefined
        Application.Lock();
        if (Application["user"] != null)
        {undefined
            string users;
            string[] user;
            users = Application["user"].ToString();
            Application["user"] = null;
            user = users.Split(',');
            foreach (string s in user)
            {undefined
                if (s != Session["username"].ToString())
                {undefined
                    if (Application["user"] == null)
                    {undefined
                        Application["user"] = s;
                    }

                    else
                    {undefined
                        Application["uesr"] = Application["user"] + "," + s;
                    }
                }
            }
        }
        if (Session["username"] != null)
        {undefined
            Session["username"] = null;
        }
        Application.UnLock();
        Response.Redirect("login.aspx");

    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {undefined

    }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#简单聊天室雏形

    本文实例为大家分享了C#简单聊天室雏形的具体代码,供大家参考,具体内容如下 程序使用的控制台的黑窗口模拟程序,第一次涉及网络编程,写出来方便以后查阅,代码很简单首先是服务器端的代码: public class ServerControl     {         private Socket serverSocket;         public ServerControl()         {             serverSocket = new Socket(AddressFa

  • c#基于WinForm的Socket实现简单的聊天室 IM

    1:什么是Socket 所谓套接字(Socket),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象. 一个套接字就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制. 从所处的地位来讲,套接字上联应用进程,下联网络协议栈,是应用程序通过网络协议进行通信的接口,是应用程序与网络协议根进行交互的接口. 2:客服端和服务端的通信简单流程 3:服务端Code: using System; using System.Collections.Generic; using Sys

  • C#制作简单的多人在线即时交流聊天室

    实现网页版的在线聊天室的方法有很多,在没有来到HTML5之前,常见的有:定时轮询.长连接+长轮询.基于第三方插件(如FLASH的Socket),而如果是HTML5,则比较简单,可以直接使用WebSocket,当然HTML5目前在PC端并没有被所有浏览器支持,所以我的这个聊天室仍是基于长连接+长轮询+原生的JS及AJAX实现的多人在线即时交流聊天室,这个聊天室其实是我上周周末完成的,功能简单,可能有些不足,但可以满足在线即时聊天需求,分享也是给大家提供一个思路,大家可以基于此来实现更好的在线即时聊

  • C#基于Socket的TCP通信实现聊天室案例

    本文实例为大家分享了vue + element ui实现锚点定位的具体代码,供大家参考,具体内容如下 一.Socket(套接字)通信概念 套接字(socket)是通信的基石,用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息:连接使用的协议,本地主机的IP地址,本地进程的协议端口,远地主机的IP地址,远地进程的协议端口. 应用层通过传输层进行数据

  • C#使用Socket实现本地多人聊天室

    本文实例为大家分享了C#使用Socket实现本地多人聊天室的具体代码,供大家参考,具体内容如下 [脚本一:Server端] 使用本机地址:127.0.0.1 完整代码 using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading;   namespace ConsoleApp1 {     p

  • 分享一个C#编写简单的聊天程序(详细介绍)

    引言 这是一篇基于Socket进行网络编程的入门文章,我对于网络编程的学习并不够深入,这篇文章是对于自己知识的一个巩固,同时希望能为初学的朋友提供一点参考.文章大体分为四个部分:程序的分析与设计.C#网络编程基础(篇外篇).聊天程序的实现模式.程序实现. 程序的分析与设计 1.明确程序功能 如果大家现在已经参加了工作,你的经理或者老板告诉你,"小王,我需要你开发一个聊天程序".那么接下来该怎么做呢?你是不是在脑子里有个雏形,然后就直接打开VS2005开始设计窗体,编写代码了呢?在开始之

  • C#基于Socket实现简单聊天室功能

    因为这段时间在学习Socket,所以就试着写了一个简单的聊天室.主要分为服务器端和多个客户端.利用服务器端作数据中转站,实现消息群发. 1.服务器端有两个类: using System.Collections.Generic; using System.Net; using System.Net.Sockets; namespace 聊天室_Socket_TCP_服务器端 {     class Program     {         static List<Client> clients

  • C#聊天程序服务端与客户端完整实例代码

    本文所述为基于C#实现的多人聊天程序服务端与客户端完整代码.本实例省略了结构定义部分,服务端主要是逻辑处理部分代码,因此使用时需要完善一些窗体按钮之类的. 先看服务端代码如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using

  • C#使用WebSocket实现聊天室功能

    WebSocket介绍 WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. 在WebSocket API中,浏览器和服务器只需要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道.两者之间就直接可以数据互相传送. 浏览器通过 JavaScript 向服务器发出建立 WebSocket 连接的请求,连接建立以后,客户端和服务器端就可以通过 TCP 连接直接交换数据. 当你获取 Web Socket 连接后,你可以通过 send() 方法来向服务器发

  • C#实现简易多人聊天室

    本文实例为大家分享了C#实现简易多人聊天室的具体代码,供大家参考,具体内容如下 只有一个群聊的功能 服务端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Te

随机推荐