服务端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms; namespace SocketServer
{
public partial class Form1 : Form
{
/// <summary>
/// 保存连接的所有用户
/// </summary>
private List<User> userList = new List<User>(); /// <summary>
/// 服务器IP地址
/// </summary>;
private string ServerIP; /// <summary>
/// 监听端口
/// </summary>
private int port;
private TcpListener myListener; /// <summary>
/// 是否正常退出所有接收线程
/// </summary>
bool isNormalExit = false; public Form1()
{
InitializeComponent();
btn_Stop.Enabled = false;
SetServerIPAndPort();
} /// <summary>
/// 根据当前程序目录的文本文件‘ServerIPAndPort.txt’内容来设定IP和端口
/// 格式:127.0.0.1:8885
/// </summary>
private void SetServerIPAndPort()
{
FileStream fs = new FileStream("ServerIPAndPort.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string IPAndPort = sr.ReadLine();
ServerIP = IPAndPort.Split(':')[]; //设定IP
port = int.Parse(IPAndPort.Split(':')[]); //设定端口
sr.Close();
fs.Close();
} /// <summary>
/// 开始监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Start_Click(object sender, EventArgs e)
{
ListenClient();
} /// <summary>
/// 开始服务,监听客户端
/// </summary>
private void ListenClient()
{
myListener = new TcpListener(IPAddress.Parse(ServerIP), port);
myListener.Start();
AddTalkMessage(string.Format("开始在{0}:{1}监听客户连接", ServerIP, port));
//创建一个线程监客户端连接请求
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
btn_Start.Enabled = false;
btn_Stop.Enabled = true;
} /// <summary>
/// 接收客户端连接
/// </summary>
private void ListenClientConnect()
{
TcpClient newClient = null;
while (true)
{
try
{
newClient = myListener.AcceptTcpClient();
}
catch
{
//当单击‘停止监听’或者退出此窗体时 AcceptTcpClient() 会产生异常
//因此可以利用此异常退出循环
break;
}
//每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息;
User user = new User(newClient);
Thread threadReceive = new Thread(ReceiveData);
threadReceive.Start(user);
userList.Add(user);
AddTalkMessage(string.Format("[{0}]进入", newClient.Client.RemoteEndPoint));
AddTalkMessage(string.Format("当前连接用户数:{0}", userList.Count));
} } /// <summary>
/// 处理接收的客户端信息
/// </summary>
/// <param name="userState">客户端信息</param>
private void ReceiveData(object userState)
{
User user = (User)userState;
TcpClient client = user.client;
while (isNormalExit == false)
{
string receiveString = null;
try
{
//从网络流中读出字符串,此方法会自动判断字符串长度前缀
receiveString = user.br.ReadString();
}
catch (Exception)
{
if (isNormalExit == false)
{
AddTalkMessage(string.Format("与[{0}]失去联系,已终止接收该用户信息", client.Client.RemoteEndPoint));
RemoveUser(user);
}
break;
}
AddTalkMessage(string.Format("来自[{0}]:{1}", user.client.Client.RemoteEndPoint, receiveString));
string[] splitString = receiveString.Split(',');
switch (splitString[])
{
case "Login":
user.userName = splitString[];
SendToAllClient(user, receiveString);
break;
case "Logout":
SendToAllClient(user, receiveString);
RemoveUser(user);
return;
case "AllTalk":
string allTalkString = receiveString.Substring(splitString[].Length + splitString[].Length + );
AddTalkMessage(string.Format("{0}说:{1}", user.userName, allTalkString));
for (int i = ; i < userList.Count; i++)
{
SendToClient(userList[i], "talk," + user.userName + "," + allTalkString);
}
break;
case "Talk":
string talkString = receiveString.Substring(splitString[].Length + splitString[].Length + );
AddTalkMessage(string.Format("{0}对{1}说:{2}", user.userName, splitString[], talkString));
SendToClient(user, "talk," + "你对"+ splitString[] + "说," + talkString);//发给自己
foreach (User target in userList)//发给对方
{
if (target.userName == splitString[] && user.userName != splitString[])
{
SendToClient(target, "talk," + user.userName + "对你说," + talkString);
break;
}
}
break;
default:
AddTalkMessage("错误:" + receiveString);
break;
}
}
} /// <summary>
/// 发送消息给所有客户
/// </summary>
/// <param name="user">指定发给哪个用户</param>
/// <param name="message">信息内容</param>
private void SendToAllClient(User user, string message)
{
string command = message.Split(',')[].ToLower();
if (command == "login")
{
//获取所有客户端在线信息到当前登录用户
for (int i = ; i < userList.Count; i++)
{
SendToClient(user, "login," + userList[i].userName);
}
//把自己上线,发送给所有客户端
for (int i = ; i < userList.Count; i++)
{
if (user.userName != userList[i].userName)
{
SendToClient(userList[i], "login," + user.userName);
SendToClient(userList[i], "talk,服务器:" + user.userName + "已上线,");
}
}
}
else if (command == "logout")
{
for (int i = ; i < userList.Count; i++)
{
if (userList[i].userName != user.userName)
{
SendToClient(userList[i], message);
SendToClient(userList[i], "talk,服务器:" + user.userName + "已下线,");
}
}
}
} /// <summary>
/// 发送 message 给 user
/// </summary>
/// <param name="user">指定发给哪个用户</param>
/// <param name="message">信息内容</param>
private void SendToClient(User user, string message)
{
try
{
//将字符串写入网络流,此方法会自动附加字符串长度前缀
user.bw.Write(message);
user.bw.Flush();
AddTalkMessage(string.Format("向[{0}]发送:{1}", user.userName, message));
}
catch
{
AddTalkMessage(string.Format("向[{0}]发送信息失败", user.userName));
}
} /// <summary>
/// 移除用户
/// </summary>
/// <param name="user">指定要移除的用户</param>
private void RemoveUser(User user)
{
userList.Remove(user);
user.Close();
AddTalkMessage(string.Format("当前连接用户数:{0}", userList.Count));
} private delegate void AddTalkMessageDelegate(string message);
/// <summary>
/// 在聊天对话框(txt_Message)中追加聊天信息
/// </summary>
/// <param name="str">要追加的信息</param>
private void AddTalkMessage(string message)
{
if (txt_Message.InvokeRequired)
{
AddTalkMessageDelegate d = new AddTalkMessageDelegate(AddTalkMessage);
txt_Message.Invoke(d, new object[] { message });
}
else
{
txt_Message.AppendText(message + Environment.NewLine);
txt_Message.ScrollToCaret();
}
} /// <summary>
/// 停止监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Stop_Click(object sender, EventArgs e)
{
stopListen();
} private void stopListen()
{
AddTalkMessage("开始停止服务,并依次使用户退出!");
isNormalExit = true;
for (int i = userList.Count - ; i >= ; i--)
{
RemoveUser(userList[i]);
}
//通过停止监听让 myListener.AcceptTcpClient() 产生异常退出监听线程
myListener.Stop();
btn_Start.Enabled = true;
btn_Stop.Enabled = false;
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (myListener != null)
stopListen();//btn_Stop.PerformClick(); //引发 btn_Stop 的Click事件
} private void btn_send_Click(object sender, EventArgs e)
{
for (int i = ; i < userList.Count; i++)
{
SendToClient(userList[i], "talk,服务器," + talk.Text + "\r\n");
}
talk.Clear();
} private void talk_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
//触发【发送】按钮的单击事件
btn_send.PerformClick();
}
} }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO; namespace SocketServer
{
class User
{
public TcpClient client { get; private set; }
public BinaryReader br { get; private set; }
public BinaryWriter bw { get; private set; }
public string userName { get; set; } public User(TcpClient client)
{
this.client = client;
NetworkStream networkStream = client.GetStream();
br = new BinaryReader(networkStream);
bw = new BinaryWriter(networkStream);
} public void Close()
{
br.Close();
bw.Close();
client.Close();
} }
}

客户端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms; namespace SocketClient
{
public partial class Form1 : Form
{
private string ServerIP; //IP
private int port; //端口
private bool isExit = false;
private TcpClient client;
private BinaryReader br;
private BinaryWriter bw; public Form1()
{
InitializeComponent();
Random r = new Random((int)DateTime.Now.Ticks);
txt_UserName.Text = "user" + r.Next(, );
lst_OnlineUser.HorizontalScrollbar = true;
lst_OnlineUser.Items.Add("所有人");
lst_OnlineUser.SelectedIndex = ;
SetServerIPAndPort();
} /// <summary>
/// 根据当前程序目录的文本文件‘ServerIPAndPort.txt’内容来设定IP和端口
/// 格式:127.0.0.1:8885
/// </summary>
private void SetServerIPAndPort()
{
try
{
FileStream fs = new FileStream("ServerIPAndPort.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string IPAndPort = sr.ReadLine();
ServerIP = IPAndPort.Split(':')[]; //设定IP
port = int.Parse(IPAndPort.Split(':')[]); //设定端口
sr.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show("配置IP与端口失败,错误原因:" + ex.Message);
Application.Exit();
}
} /// <summary>
/// 打开客户端,即连接服务器
/// </summary>
private void ConnectServer()
{
btn_Login.Enabled = false;
try
{
//此处为方便演示,实际使用时要将Dns.GetHostName()改为服务器域名
//IPAddress ipAd = IPAddress.Parse("182.150.193.7");
client = new TcpClient();
client.Connect(IPAddress.Parse(ServerIP), port);
AddTalkMessage("连接成功\r\n");
}
catch (Exception ex)
{
AddTalkMessage("连接失败,原因:" + ex.Message + "\r\n");
btn_Login.Enabled = true;
return;
}
//获取网络流
NetworkStream networkStream = client.GetStream();
//将网络流作为二进制读写对象
br = new BinaryReader(networkStream);
bw = new BinaryWriter(networkStream);
SendMessage("Login," + txt_UserName.Text);
Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
threadReceive.IsBackground = true;
threadReceive.Start();
} /// <summary>
/// 【登陆】按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Login_Click(object sender, EventArgs e)
{
ConnectServer();
} /// <summary>
/// 处理服务器信息
/// </summary>
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[].ToLower();
switch (command)
{
case "login": //格式: login,用户名
AddOnline(splitString[]);
break;
case "logout": //格式: logout,用户名
RemoveUserName(splitString[]);
break;
case "talk": //格式: talk,用户名,对话信息
AddTalkMessage(splitString[] + ":\r\n");
AddTalkMessage(receiveString.Substring(splitString[].Length + splitString[].Length + ) + "\r\n");
break;
default:
AddTalkMessage("错误:" + receiveString + "\r\n");
break;
}
}
Application.Exit();
} /// <summary>
/// 向服务端发送消息
/// </summary>
/// <param name="message"></param>
private void SendMessage(string message)
{
try
{
//将字符串写入网络流,此方法会自动附加字符串长度前缀
bw.Write(message);
bw.Flush();
}
catch
{
AddTalkMessage("发送失败\r\n");
}
} private delegate void AddTalkMessageDelegate(string message);
/// <summary>
/// 在聊天对话框(txt_Message)中追加聊天信息
/// </summary>
/// <param name="message"></param>
private void AddTalkMessage(string message)
{
if (txt_Message.InvokeRequired)
{
AddTalkMessageDelegate d = new AddTalkMessageDelegate(AddTalkMessage);
txt_Message.Invoke(d, new object[] { message });
}
else
{
txt_Message.AppendText(message);
txt_Message.ScrollToCaret();
}
} private delegate void AddOnlineDelegate(string message);
/// <summary>
/// 在在线框(lst_Online)中添加其他客户端信息
/// </summary>
/// <param name="userName"></param>
private void AddOnline(string userName)
{
if (lst_OnlineUser.InvokeRequired)
{
AddOnlineDelegate d = new AddOnlineDelegate(AddOnline);
lst_OnlineUser.Invoke(d, new object[] { userName });
}
else
{
lst_OnlineUser.Items.Add(userName);
lst_OnlineUser.SelectedIndex = lst_OnlineUser.Items.Count - ;
lst_OnlineUser.ClearSelected();
}
} private delegate void RemoveUserNameDelegate(string userName);
/// <summary>
/// 在在线框(lst_Online)中移除不在线的客户端信息
/// </summary>
/// <param name="userName"></param>
private void RemoveUserName(string userName)
{
if (lst_OnlineUser.InvokeRequired)
{
RemoveUserNameDelegate d = new RemoveUserNameDelegate(RemoveUserName);
lst_OnlineUser.Invoke(d, userName);
}
else
{
lst_OnlineUser.Items.Remove(userName);
lst_OnlineUser.SelectedIndex = lst_OnlineUser.Items.Count - ;
lst_OnlineUser.ClearSelected();
}
} /// <summary>
/// 【发送】按钮单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Send_Click(object sender, EventArgs e)
{
if (txt_SendText.Text == "")
{
MessageBox.Show("发送不能为空!");
return;
}
if (lst_OnlineUser.Text == "所有人" || lst_OnlineUser.Text == "")
{
SendMessage("AllTalk,all," + txt_SendText.Text + "\r\n");
}
else
{
SendMessage("Talk," + lst_OnlineUser.SelectedItem + "," + txt_SendText.Text + "\r\n");
}
txt_SendText.Clear();
} /// <summary>
/// 在发送信息的文本框中按下【Enter】键触发的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_SendText_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
txt_SendText.Text = txt_SendText.Text.Substring(, txt_SendText.Text.Length - );
//触发【发送】按钮的单击事件
btn_Send.PerformClick();
}
} /// <summary>
/// 窗体关闭事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//未与服务器连接前 client 为 null
if (client != null)
{
try
{
SendMessage("Logout," + txt_UserName.Text);
isExit = true;
br.Close();
bw.Close();
client.Close();
}
catch
{
}
}
} }
}

实现效果:

socket学习之聊天室的更多相关文章

  1. 【总结】学习Socket编写的聊天室小程序

    1.前言 在学习Socket之前,先来学习点网络相关的知识吧,自己学习过程中的一些总结,Socket是一门很高深的学问,本文只是Socket一些最基础的东西,大神请自觉绕路. 传输协议 TCP:Tra ...

  2. Socket.io官方聊天室DEMO的学习笔记

    照着Socket.io官方的聊天室代码敲了一遍,遇到了一个奇怪的问题: 每次点击SEND按钮的时候,都会重新刷新页面. 在点击页面的一瞬间,看到了正在加载jquery的提示, 然后以为是jquery用 ...

  3. 使用socket.io搭建聊天室

    最近在学习nodejs,需要找一些项目练练手.找来找去发现了一个聊天室的教程,足够简单,也能从中学到一些东西.下面记录我练习过程中待一些笔记. nodeJS模块 共用到了2个模块,express和so ...

  4. node.js + socket.io实现聊天室一

    前段时间,公司打算在社区做一个聊天室.决定让我来做.本小白第一次做聊天类功能,当时还想着通过ajax请求来实现.经过经理提示,说试试当前流行的node.js 和socket.io来做.于是就上网学习研 ...

  5. 【C++】基于socket的多线程聊天室(控制台版)

    以前学习socket网络编程和多线程编程的时候写的一个练手程序 聊天室基本功能: 1.用户管理:登录,注册,登出,修改用户名,修改密码 2.聊天室功能:群聊,私聊,获取在线用户列表,获取所有用户列表 ...

  6. C#基于Socket的简单聊天室实践

    序:实现一个基于Socket的简易的聊天室,实现的思路如下: 程序的结构:多个客户端+一个服务端,客户端都是向服务端发送消息,然后服务端转发给所有的客户端,这样形成一个简单的聊天室功能. 实现的细节: ...

  7. Socket.io在线聊天室

    从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...

  8. 使用 Socket.IO 开发聊天室

    前言 Socket.IO 是一个用来实现实时双向通信的框架,其本质是基于 WebSocket 技术. 我们首先来聊聊 WebSocket 技术,先设想这么一个场景: · 用户小A,打开了某个网站的充值 ...

  9. Socket实现简易聊天室,Client,Server

    package seday08; import java.io.BufferedWriter;import java.io.OutputStream;import java.io.OutputStre ...

随机推荐

  1. js中获取URL中指定的查询字符串

    js中获取URL中指定的搜索字符串,主要利用location对象实现,废话少说,上代码. function getSearchString(key) { // 获取URL中?之后的字符 var str ...

  2. Oracle数据库,非空约束、主键约束、外键约束、唯一约束

    非空约束:设置列时,可为空默认可为空,去掉对号之后设置数据不可为空: 唯一约束:在键中设置,唯一约束名称.类型Unique.列名:设置应用完成之后,此列数据具有唯一性:即数据不可重复(类型:Uniqu ...

  3. 第 11 章 进度条媒体对象和 Well 组件

    学习要点: 1.Well 组件 2.进度条组件 3.媒体对象组件 主讲教师:李炎恢 本节课我们主要学习一下 Bootstrap 的三个组件功能:Well 组件.进度条组件.媒体对象组件. 一.Well ...

  4. 袋鼠过河---DP

    题目:一只袋鼠要从河这边跳到河对岸,河很宽,但是河中间打了很多桩子,每隔一米就有一个,每个桩子上都有一个弹簧,袋鼠跳到弹簧上就可以跳的更远,每个弹簧力量不同,用一个数字代表它的力量,如果弹簧力量为5, ...

  5. Web-Nginx配置

    #运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; ...

  6. 项目中应用eventbus解决的问题

    在项目开发过程中,往往有些功能表面看起来简单,但实际开发的结果非常复杂,仔细分析下原因发现很多都是因为附加了许多的额外功能. 真的简单吗? 比如我们对一个电商平台的商品数据做修改的功能来讲,其实非常简 ...

  7. rabbitMQ publish丢包分析

    rabbitMQ publish丢包分析

  8. rabbitmq binary/other_system内存占用很高

    最近有台服务器的MQ应用占用内存比较偏高,如下: 但是看控制台本身内存中消息积压并不多, 查看rabbtmqctl发现,binary data和other data占据了绝大部分的内存,如下: {me ...

  9. jquery多次上传同一张图片

    $().reset(); wrap():$('p').wrap('div');===><div><p></p></div>; .closest() ...

  10. ArcGIS Server开发实践之【Search Widget工具查询本地地图服务】

    加载本地地图服务,并实现要素的查询.(不足之处还请指点)具体代码如下: <!DOCTYPE html> <html dir="ltr"> <head& ...