基本信息
源码名称:C#+SOCKET编程文档及实例(含有客户端和服务器端)+源码
源码大小:1.37M
文件格式:.rar
开发语言:C#
更新时间:2020-01-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
SOCKET编程实例
{
//通过转发表得到当前用户套接字
Socket clientSkt = _transmit_tb[obj] as Socket;
while (true)
{
try
{
//接受第一个数据包。
//仅有两种情况,一为可以识别的命令格式,否则为要求转发给指定用户的用户名。
//这里的实现不够优雅,但不失为此简单模型的一个解决之道。
byte[] _cmdBuff = new byte[128];
clientSkt.Receive(_cmdBuff);
string _cmd = DecodingBytes(_cmdBuff);
/// "01"-离线
/// "02"-请求在线列表
/// "03"-请求对所有人闪屏振动
/// "04"-请求对指定用户闪屏振动
/// "05"-请求广播消息
/// default-转发给指定用户
switch (_cmd)
{
case "01":
{
_transmit_tb.Remove(obj);
string svrlog = string.Format("[系统消息]用户 {0} 在 {1} 已断开... 当前在线人数: {2}\r\n\r\n", obj, DateTime.Now, _transmit_tb.Count);
Console.WriteLine(svrlog);
//向所有客户机发送系统消息
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
_clientSkt.Send(Encoding.Unicode.GetBytes(svrlog));//给其他用户通知现在上线的用户
}
Thread.CurrentThread.Abort();
break;
}
case "02":
{
byte[] onlineBuff = SerializeOnlineList();
//先发送响应信号,用于客户机的判断,"11"表示服务发给客户机的更新在线列表的命令
clientSkt.Send(new byte[] { 1, 1 });
clientSkt.Send(onlineBuff);
break;
}
case "03":
{
string displayTxt = string.Format("[系统提示]用户 {0} 向您发送了一个闪屏振动。\r\n\r\n", obj);
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
if (!_clientName.Equals(obj))
{
//先发送响应信号,用于客户机的判断,"12"表示服务发给客户机的闪屏振动的命令
_clientSkt.Send(new byte[] { 1, 2 });
_clientSkt.Send(Encoding.Unicode.GetBytes(displayTxt));
}
}
break;
}
case "04":
{
string _receiver = null;
byte[] _receiverBuff = new byte[128];
clientSkt.Receive(_receiverBuff);
_receiver = Encoding.Unicode.GetString(_receiverBuff).TrimEnd('\0');
string displayTxt = string.Format("[系统提示]用户 {0} 向您发送了一个闪屏振动。\r\n\r\n", obj);
//通过转发表查找接收方的套接字
if (_transmit_tb.ContainsKey(_receiver))
{
Socket receiverSkt = _transmit_tb[_receiver] as Socket;
receiverSkt.Send(new byte[] { 1, 2 });
receiverSkt.Send(Encoding.Unicode.GetBytes(displayTxt));
}
else
{
string sysMessage = string.Format("[系统消息]您刚才的闪屏振动没有发送成功。\r\n可能原因:用户 {0} 已离线或者网络阻塞。\r\n\r\n", _receiver);
clientSkt.Send(Encoding.Unicode.GetBytes(sysMessage));
}
break;
}
case "05":
{
byte[] _msgBuff = new byte[_maxPacket];
clientSkt.Receive(_msgBuff);
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
if (!_clientName.Equals(obj))
{
_clientSkt.Send(_msgBuff);
}
}
break;
}
default:
{
//以上都不是则表明第一个包是接收方用户名,继续接受第二个消息正文数据包
string _receiver = Encoding.Unicode.GetString(_cmdBuff).TrimEnd('\0');
byte[] _packetBuff = new byte[_maxPacket];
clientSkt.Receive(_packetBuff);
if (_transmit_tb.ContainsKey(_receiver))
{
//通过转发表查找接收方的套接字
Socket receiverSkt = _transmit_tb[_receiver] as Socket;
receiverSkt.Send(_packetBuff);
}
else
{
string sysMessage = string.Format("[系统消息]您刚才的内容没有发送成功。\r\n可能原因:用户 {0} 已离线或者网络阻塞。\r\n\r\n", _receiver);
clientSkt.Send(Encoding.Unicode.GetBytes(sysMessage));
}
break;
}
}
}
catch (SocketException)
{
_transmit_tb.Remove(obj);
string svrlog = string.Format("[系统消息]用户 {0} 的客户端在 {1} 意外终止!当前在线人数:{2}\r\n\r\n", obj, DateTime.Now, _transmit_tb.Count);
Console.WriteLine(svrlog);
//向所有客户机发送系统消息
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
_clientSkt.Send(Encoding.Unicode.GetBytes(svrlog));
}
Console.WriteLine();
Thread.CurrentThread.Abort();
}
}
}
SOCKET编程实例
private void ThreadFunc(object obj)
{
//通过转发表得到当前用户套接字
Socket clientSkt = _transmit_tb[obj] as Socket;
while (true)
{
try
{
//接受第一个数据包。
//仅有两种情况,一为可以识别的命令格式,否则为要求转发给指定用户的用户名。
//这里的实现不够优雅,但不失为此简单模型的一个解决之道。
byte[] _cmdBuff = new byte[128];
clientSkt.Receive(_cmdBuff);
string _cmd = DecodingBytes(_cmdBuff);
/// "01"-离线
/// "02"-请求在线列表
/// "03"-请求对所有人闪屏振动
/// "04"-请求对指定用户闪屏振动
/// "05"-请求广播消息
/// default-转发给指定用户
switch (_cmd)
{
case "01":
{
_transmit_tb.Remove(obj);
string svrlog = string.Format("[系统消息]用户 {0} 在 {1} 已断开... 当前在线人数: {2}\r\n\r\n", obj, DateTime.Now, _transmit_tb.Count);
Console.WriteLine(svrlog);
//向所有客户机发送系统消息
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
_clientSkt.Send(Encoding.Unicode.GetBytes(svrlog));//给其他用户通知现在上线的用户
}
Thread.CurrentThread.Abort();
break;
}
case "02":
{
byte[] onlineBuff = SerializeOnlineList();
//先发送响应信号,用于客户机的判断,"11"表示服务发给客户机的更新在线列表的命令
clientSkt.Send(new byte[] { 1, 1 });
clientSkt.Send(onlineBuff);
break;
}
case "03":
{
string displayTxt = string.Format("[系统提示]用户 {0} 向您发送了一个闪屏振动。\r\n\r\n", obj);
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
if (!_clientName.Equals(obj))
{
//先发送响应信号,用于客户机的判断,"12"表示服务发给客户机的闪屏振动的命令
_clientSkt.Send(new byte[] { 1, 2 });
_clientSkt.Send(Encoding.Unicode.GetBytes(displayTxt));
}
}
break;
}
case "04":
{
string _receiver = null;
byte[] _receiverBuff = new byte[128];
clientSkt.Receive(_receiverBuff);
_receiver = Encoding.Unicode.GetString(_receiverBuff).TrimEnd('\0');
string displayTxt = string.Format("[系统提示]用户 {0} 向您发送了一个闪屏振动。\r\n\r\n", obj);
//通过转发表查找接收方的套接字
if (_transmit_tb.ContainsKey(_receiver))
{
Socket receiverSkt = _transmit_tb[_receiver] as Socket;
receiverSkt.Send(new byte[] { 1, 2 });
receiverSkt.Send(Encoding.Unicode.GetBytes(displayTxt));
}
else
{
string sysMessage = string.Format("[系统消息]您刚才的闪屏振动没有发送成功。\r\n可能原因:用户 {0} 已离线或者网络阻塞。\r\n\r\n", _receiver);
clientSkt.Send(Encoding.Unicode.GetBytes(sysMessage));
}
break;
}
case "05":
{
byte[] _msgBuff = new byte[_maxPacket];
clientSkt.Receive(_msgBuff);
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
if (!_clientName.Equals(obj))
{
_clientSkt.Send(_msgBuff);
}
}
break;
}
default:
{
//以上都不是则表明第一个包是接收方用户名,继续接受第二个消息正文数据包
string _receiver = Encoding.Unicode.GetString(_cmdBuff).TrimEnd('\0');
byte[] _packetBuff = new byte[_maxPacket];
clientSkt.Receive(_packetBuff);
if (_transmit_tb.ContainsKey(_receiver))
{
//通过转发表查找接收方的套接字
Socket receiverSkt = _transmit_tb[_receiver] as Socket;
receiverSkt.Send(_packetBuff);
}
else
{
string sysMessage = string.Format("[系统消息]您刚才的内容没有发送成功。\r\n可能原因:用户 {0} 已离线或者网络阻塞。\r\n\r\n", _receiver);
clientSkt.Send(Encoding.Unicode.GetBytes(sysMessage));
}
break;
}
}
}
catch (SocketException)
{
_transmit_tb.Remove(obj);
string svrlog = string.Format("[系统消息]用户 {0} 的客户端在 {1} 意外终止!当前在线人数:{2}\r\n\r\n", obj, DateTime.Now, _transmit_tb.Count);
Console.WriteLine(svrlog);
//向所有客户机发送系统消息
foreach (DictionaryEntry de in _transmit_tb)
{
string _clientName = de.Key as string;
Socket _clientSkt = de.Value as Socket;
_clientSkt.Send(Encoding.Unicode.GetBytes(svrlog));
}
Console.WriteLine();
Thread.CurrentThread.Abort();
}
}
}