asp.net如何实现在线聊天

水墨熊猫 2010-08-05 10:15:53
只要能够实现收发信息就可以,用stock的话怎么获得IP地址?或者是用其他的方法实现?最好是有例子小弟在此感谢!!!
...全文
411 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycproc 2011-03-03
  • 打赏
  • 举报
回复
上面是服务器端
下面是客户端

仅仅只是DEMO
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace Clent
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}

/// <summary>
/// IP地址
/// </summary>
private IPAddress _Ipaddress;

/// <summary>
/// 用于接受和发送消息的网络流
/// </summary>
private NetworkStream _nws = null;

/// <summary>
/// 链接服务器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnOk_Click(object sender, EventArgs e)
{
if (!ValidateInfo())
{
return;
}
int port = int.Parse(duankou.Text);
//向服务器发送链接请求
TCPConnection conn = new TCPConnection(_Ipaddress, port);
TcpClient _tcpc = conn.Connect();
if (_tcpc == null)
{
MessageBox.Show("无法链接到服务器,请重试!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
NetworkStream netstream = _tcpc.GetStream();//提供用于访问网络的基本数据流
//向服务器发送用户名以确认身份
netstream.Write(Encoding.Unicode.GetBytes(this.username.Text), 0, Encoding.Unicode.GetBytes(this.username.Text).Length);
//得到登录结果
byte[] buffer = new byte[1024];
}
string localtext = null;
string sendtext = null;
string msg = username.Text.Trim();
if (msg == string.Empty)
{
MessageBox.Show("不能发送空消息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
sendtext = string.Format("[广播]" + this.iptext.Text + "在" + DateTime.Now + "对所有人说:" + msg + "\t\n");
_nws.Write(Encoding.Unicode.GetBytes(sendtext), 0, Encoding.Unicode.GetBytes(sendtext).Length);

richTextBox1.AppendText(localtext);
this.username.Clear();
}

/// <summary>
/// 清空文本框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
this.duankou.Text = "";
this.iptext.Text = "";
this.username.Text = "";
}

/// <summary>
/// 验证登录信息
/// </summary>
/// <returns>验证结果</returns>
private bool ValidateInfo()
{
if (username.Text.Trim() == string.Empty)
{
MessageBox.Show("请填写用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}

if (!IPAddress.TryParse(iptext.Text, out _Ipaddress))
{
MessageBox.Show("IP地址不合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
int _port;
if (!int.TryParse(duankou.Text, out _port))
{
MessageBox.Show("端口号不合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
else
{
if (_port < 1024 || _port > 65535)
{
MessageBox.Show("端口号不合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}
return true;
}
}
}
ycproc 2011-03-03
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Windows.Forms;

namespace Chat
{
public partial class ServerForm : Form
{
public ServerForm()
{
InitializeComponent();
}

#region 自定义字段

/// <summary>
/// 服务器端Ip,默认为8888
/// </summary>
private int _port = 8888;

/// <summary>
/// 保存客户端所有回话的哈希表
/// </summary>
private Hashtable _transmit_tb = new Hashtable();

/// <summary>
/// 用于接受消息的线程
/// </summary>
private Thread _receviccethread = null;

/// <summary>
/// 用于接受和发送消息的网络流
/// </summary>
private NetworkStream _nws = null;

#endregion

/// <summary>
/// 初始化运行启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ServerForm_Load(object sender, EventArgs e)
{
try
{
Form.CheckForIllegalCrossThreadCalls = false;
for (int i = 0; i < 10; i++)
{

}
ThreadStart thsrt = new ThreadStart(Listen);
Thread mythread = new Thread(thsrt);
mythread.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), " 提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

/// <summary>
/// 启动监听,轮询监听客户机请求并将客户端套接字存入转发表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Listen()
{
IPAddress _ip = IPAddress.Any;
Socket newsoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsoc.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
IPEndPoint locaEp = new IPEndPoint(IPAddress.Any, _port);
this.messageAll.AppendText("服务器已启动,正在侦听...\t\n");
try
{
newsoc.Bind(locaEp);
newsoc.Listen(10);
newsoc.BeginAccept(new AsyncCallback(onCall), newsoc);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), " 提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

/// <summary>
/// 监听回调
/// </summary>
/// <param name="ar"></param>
private void onCall(IAsyncResult ar)
{
try
{
Socket serverSoc = (Socket)ar.AsyncState;
Socket clent = serverSoc.EndAccept(ar);
byte[] comes = System.Text.Encoding.UTF8.GetBytes("已链接");
EndPoint enp = clent.RemoteEndPoint;
clent.Send(comes, comes.Length, 0);
_transmit_tb.Add(clent.RemoteEndPoint, null);
serverSoc.BeginAccept(new AsyncCallback(onCall), serverSoc);
while (true)
{
byte[] buffer = new byte[1024];
int re = clent.Receive(comes);
string msg = Encoding.UTF8.GetString(comes, 0, re);
string ip = clent.RemoteEndPoint.ToString();
if (msg.Length == 0)
{
_transmit_tb.Remove(clent.RemoteEndPoint);
_receviccethread.Interrupt();
_receviccethread.Abort();
break;
}
this.messageAll.AppendText(ip + "在" + DateTime.Now + "说" + msg + "\t\n");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), " 提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

/// <summary>
/// 关闭进程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ServerForm_FormClosing1(object sender, FormClosingEventArgs e)
{
if (_receviccethread != null)
{
_receviccethread.Interrupt();
_receviccethread.Abort();
_nws.Close();
this.Close();
}
}
}
}
zenggezhuang 2011-03-03
  • 打赏
  • 举报
回复
有没有完整的代码呀?
lishuai1030 2010-08-05
  • 打赏
  • 举报
回复
获得ip地址?
stock不懂 获得IP不用stock也可以啊

public IPAddress GetIP()
{
WebClient client = new WebClient();
byte[] bytRecv = client.DownloadData("http://www.123cha.com/"); //下载网页数据
string str = System.Text.Encoding.GetEncoding("gb2312").GetString(bytRecv);
string r = @"(((\d{1,3})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,3})|(1\d{2})|(2[0-4]\d)|(25[0-5]))";
string ip = System.Text.RegularExpressions.Regex.Match(str, r).ToString(); //提取信息
return IPAddress.Parse(ip);
}这个方法就可以获得用户IP了啊
获得的是公网IP哦,类似的像局域网的内网IP啊什么的都能获取

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧