zoukankan
html css js c++ java
封装的一个套接字服务端
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Net.Sockets;
using
System.Threading;
namespace
EarlyServer
{
public
class
ConnectClient : IDisposable
{
Member
#region
Member
private
Socket m_Socket;
private
string
m_NetWorkName;
private
Thread m_Thread;
private
bool
disposed
=
false
;
private
bool
m_IsRuning
=
false
;
public
event
CommandReceiveEventHandler CommandReceived;
#endregion
public
ConnectClient(Socket socket,
string
netWorkName)
{
m_Socket
=
socket;
m_NetWorkName
=
netWorkName;
}
Public Methods
#region
Public Methods
public
void
Start()
{
m_Socket
=
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_Thread
=
new
Thread(
new
ThreadStart(ThreadFunction));
m_Thread.IsBackground
=
true
;
m_Thread.Start();
}
#endregion
Private Methods
#region
Private Methods
private
void
ThreadFunction()
{
m_IsRuning
=
true
;
while
(m_IsRuning)
{
byte
[] byteMessage
=
null
; ;
string
tmpStr
=
string
.Empty;
while
(ServiceStartFlag)
{
try
{
if
(m_Socket.Connected)
{
byteMessage
=
new
byte
[
1000
];
int
len
=
m_Socket.Receive(byteMessage);
if
(len
>
0
)
{
tmpStr
=
Encoding.UTF8.GetString(byteMessage);
if
(tmpStr.Length
>
0
)
{
string
[] ary
=
tmpStr.Split(
'
|
'
);
if
(ary.Length
>
3
)
{
//
命令|发送者|目标|内容
MsgCommand cmd
=
new
MsgCommand(ary[
0
], ary[
1
], ary[
2
], tmpStr.Replace(ary[
0
]
+
"
|
"
+
ary[
1
]
+
"
|
"
+
ary[
2
]
+
"
|
"
,
""
));
this
.OnCommandReceived(
this
,
new
CommandEventArgs(cmd));
}
}
}
}
}
catch
(SocketException ex)
{
m_IsRuning
=
true
;
ClientData.List.Remove(m_NetWorkName);
}
}
}
}
#endregion
event Methods
#region
event Methods
private
void
OnCommandReceived(
object
sender, CommandEventArgs e)
{
if
(CommandReceived
!=
null
)
{
CommandReceived(
this
, e);
}
}
#endregion
IDisposable 成员
#region
IDisposable 成员
public
void
Dispose()
{
Dispose(
true
);
GC.SuppressFinalize(
this
);
}
protected
virtual
void
Dispose(
bool
disposing)
{
if
(
!
this
.disposed)
{
this
.Close();
//
注意这里不是线程安全的
}
disposed
=
true
;
}
public
void
Close()
{
if
(disposed)
{
m_IsRuning
=
false
;
m_Thread.Abort();
}
MsgCommand cmd
=
new
MsgCommand(CommandType.Outing,
this
.m_ServerIPAddress,
this
.m_NetWorkName);
this
.SendToServer(cmd);
this
.m_Socket.Close();
this
.m_NetworkStream.Close();
}
~
ConnectClient()
{
Dispose(
false
);
}
#endregion
}
public
class
ClientData
{
public
static
Dictionary
<
string
, ConnectClient
>
List
=
new
Dictionary
<
string
, ConnectClient
>
();
}
public
delegate
void
CommandReceiveEventHandler(
object
sender, CommandEventArgs eventArgs);
public
class
CommandEventArgs : EventArgs
{
private
MsgCommand m_MsgCommand;
public
MsgCommand Command
{
get
{
return
m_MsgCommand; }
}
public
CommandEventArgs(MsgCommand msgCommand)
{
this
.m_MsgCommand
=
msgCommand;
}
}
public
class
MsgCommand
{
private
string
commnadString;
/**/
///
<summary>
///
发送者名称
///
</summary>
public
string
CommnadString
{
get
{
return
commnadString; }
set
{ commnadString
=
value; }
}
private
string
senderName;
/**/
///
<summary>
///
发送者名称
///
</summary>
public
string
SenderName
{
get
{
return
senderName; }
set
{ senderName
=
value; }
}
private
string
targetName;
/**/
///
<summary>
///
目标名称
///
</summary>
public
string
TargetName
{
get
{
return
targetName; }
set
{ targetName
=
value; }
}
private
string
commandBody;
/**/
///
<summary>
///
内容
///
</summary>
public
string
MetaData
{
get
{
return
commandBody; }
set
{ commandBody
=
value; }
}
Constructors
#region
Constructors
public
MsgCommand(
string
commnadstring,
string
sendername,
string
targetName,
string
metaData)
{
this
.commnadString
=
commnadstring;
this
.senderName
=
sendername;
this
.commandBody
=
metaData;
this
.targetName
=
targetName;
}
#endregion
}
}
查看全文
相关阅读:
最长有效括号
C++ 环形缓存区的实现
vector的原理与底层实现
聚合分析与分组
求两个数的最大公约数
单例模式
工厂方法模式
责任链模式
适配器模式
策略模式
原文地址:https://www.cnblogs.com/skyblue/p/1525892.html
最新文章
Metersploit系统参数说明
nmap中文手册
hydra 常用命令解析
关于对生成器对象的理解
Zabbix创建模板(templates)及监控项(item)
Zabbix的通知功能以及自定义脚本告警
Ubuntu16.04安装Zabbix
ARP协议详解
MAC地址格式详解
TCP数据段格式+UDP数据段格式详解
热门文章
udp协议及包格式
IP报文格式详解
Plan B
二叉树的层序遍历
堆的插入和删除
C/C++混合调用(链接指示)
声明一个全局变量可以跨文件使用---extern的使用
析构函数、虚析构函数、纯虚函数
最小栈
复原IP地址-DFS
Copyright © 2011-2022 走看看