<appSettings> <add key="maxQueueCount" value="10"/> <add key="failoverServer" value="192.168.250.113,192.168.250.141"/> </appSettings>
Install.bat @echo off path %SystemRoot%\Microsoft.NET\Framework\v4.0.30319;%path% installutil %~dp0\WindowsChat.exe %SystemRoot%\system32\sc failure "ChatService" reset= 30 actions= restart/1000 pause @echo on Uninstall.bat @echo off path %SystemRoot%\Microsoft.NET\Framework\v4.0.30319;%path% installutil -u %~dp0\WindowsChat.exe pause @echo on
namespace WindowsChat
{
public delegate void WriteInfo(string info);
public class SocketHelper
{
#region 构造函数
public SocketHelper()
{
}
public SocketHelper(WriteInfo method)
{
this.method = method;
}
#endregion
public static Socket LocalSocket = null;
private object lockObj = new object();
public static List<Socket> Clients = new List<Socket>();
private WriteInfo method = null;
/// <summary>
/// 创建Socket
/// </summary>
/// <param name="port">端口默认 11011</param>
/// <param name="backlog">The maximum length of the pending connections queue.</param>
/// <returns></returns>
public Socket Create(int port = 11011, int backlog = 100)
{
if (LocalSocket == null)
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, port);//本机预使用的IP和端口
LocalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
LocalSocket.Bind(ipEndPoint);
LocalSocket.Listen(backlog);
}
return LocalSocket;
}
/// <summary>
/// 查找客户端连接
/// </summary>
/// <param name="id">标识</param>
/// <returns></returns>
private Socket FindLinked(string id)
{
foreach (var item in Clients)
{
if (item.RemoteEndPoint.ToString() == id)
return item;
}
return null;
}
/// <summary>
/// 接受远程连接
/// </summary>
public void Accept()
{
if (LocalSocket != null)
{
while (true)
{
Socket client = LocalSocket.Accept();
Thread thread = new Thread(new ParameterizedThreadStart(Revice));
thread.Start(client);
WriteLog("客户端:" + client.RemoteEndPoint.ToString() + " 接入");
lock (lockObj)
{
Clients.Add(client);
}
BroadCast("ADD|" + client.RemoteEndPoint.ToString());
}
}
}
/// <summary>
/// 日志
/// </summary>
/// <param name="info">信息</param>
private void WriteLog(string info)
{
using (FileStream fs = new FileStream("C:\\chatservice.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.WriteLine(info);
}
}
if (method != null)
{
method(info);
}
}
/// <summary>
/// 广播
/// </summary>
/// <param name="info">信息</param>
public void BroadCast(string info)
{
foreach (var item in Clients)
{
try
{
item.Send(Encoding.UTF8.GetBytes(info));
}
catch (Exception ex)
{
WriteLog(item.RemoteEndPoint.ToString() + ex.Message);
continue;
}
}
}
/// <summary>
/// 介绍信息
/// </summary>
/// <param name="client"></param>
public void Revice(object client)
{
Socket param = client as Socket;
var remoteName = param.RemoteEndPoint.ToString();
if (param != null)
{
int res = 0;
while (true)
{
byte[] buffer = new byte[10240];
int size = param.ReceiveBufferSize;
try
{
res = param.Receive(buffer);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.ConnectionReset)
{
Clients.Remove(param);
WriteLog("客户端:" + remoteName + "断开连接1");
BroadCast("REMOVE|" + remoteName);
param.Close();
return;
}
}
if (res == 0)
{
Clients.Remove(param);
WriteLog("客户端:" + remoteName + "断开连接2");
BroadCast("REMOVE|" + remoteName);
param.Close();
return;
}
var clientMsg = Encoding.UTF8.GetString(buffer, 0, res);
WriteLog(string.Format("收到客户端{0}命令:{1}", remoteName, clientMsg));
if (clientMsg == "GETALL")
{
StringBuilder sb = new StringBuilder();
foreach (var item in Clients)
{
sb.AppendFormat("{0}|", item.RemoteEndPoint.ToString());
}
param.Send(Encoding.UTF8.GetBytes("ALL|" + sb.ToString()));
}
else if (clientMsg == "OFFLINE")
{
if (Clients.Contains(param))
{
Clients.Remove(param);
WriteLog("客户端:" + remoteName + "断开连接2");
BroadCast("REMOVE|" + remoteName);
param.Close();
return;
}
}
else if (clientMsg.StartsWith("TRANST|"))
{
var msgs = clientMsg.Split('|');
var toSocket = FindLinked(msgs[1]);
if (toSocket != null)
{
WriteLog(remoteName + "发给" + msgs[1] + "的消息" + msgs[2]);
toSocket.Send(Encoding.UTF8.GetBytes("TRANSF|" + remoteName + "|" + msgs[2]));
}
}
}
}
}
}
}
public partial class ChatService : ServiceBase
{
SocketHelper helper;
Thread mainThread;
public ChatService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (helper == null)
{
helper = new SocketHelper();
}
helper.Create();
mainThread = new Thread(new ThreadStart(helper.Accept));
mainThread.IsBackground = true;
mainThread.Start();
}
protected override void OnStop()
{
helper.BroadCast("SHUTDOWN");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有