263 lines
7.4 KiB
C#
263 lines
7.4 KiB
C#
using Community;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Sockets;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Model
|
||
{
|
||
/// <summary>
|
||
/// 通讯类
|
||
/// </summary>
|
||
public class RobotSocket : NotificationObject
|
||
{
|
||
#region 通讯套接字
|
||
/// <summary>
|
||
/// 通讯套接字
|
||
/// </summary>
|
||
public Socket MySocket { get; set; }
|
||
#endregion
|
||
|
||
#region 通讯连接IP
|
||
/// <summary>
|
||
/// 通讯连接IP
|
||
/// </summary>
|
||
private string CommunicationIP { get; set; }
|
||
#endregion
|
||
|
||
#region 通讯连接端口号
|
||
/// <summary>
|
||
/// 通讯连接端口号
|
||
/// </summary>
|
||
private int CommunicationPort { get; set; }
|
||
#endregion
|
||
|
||
#region 连接状态
|
||
private ShowInfo connStatu;
|
||
/// <summary>
|
||
/// 连接状态
|
||
/// </summary>
|
||
public ShowInfo ConnStatu
|
||
{
|
||
get { return connStatu; }
|
||
set
|
||
{
|
||
connStatu = value;
|
||
this.RaisePropertyChanged("ConnStatu");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 构造函数
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="ip">连接IP</param>
|
||
/// <param name="port">端口号</param>
|
||
public RobotSocket(string ip, int port)
|
||
{
|
||
CommunicationIP = ip;
|
||
CommunicationPort = port;
|
||
ConnStatu = new ShowInfo();
|
||
ConnStatu.StatuInfo = "等待连接";
|
||
}
|
||
#endregion
|
||
|
||
#region 开启连接
|
||
/// <summary>
|
||
/// 开启连接
|
||
/// </summary>
|
||
/// <param name="mySocker">套接字对象</param>
|
||
/// <param name="ip">IP地址</param>
|
||
/// <param name="port">端口</param>
|
||
public void StartConn()
|
||
{
|
||
try
|
||
{
|
||
//1.创建监听套接字 使用ip4协议 流式传输 tcp连接
|
||
MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
// 获取服务器端口
|
||
IPAddress address = IPAddress.Parse(CommunicationIP);
|
||
IPEndPoint endpoint = new IPEndPoint(address, CommunicationPort);
|
||
//开启连接服务器
|
||
MySocket.Connect(endpoint);
|
||
}
|
||
catch
|
||
{
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 关闭连接
|
||
/// <summary>
|
||
/// 关闭连接
|
||
/// </summary>
|
||
public void ShutDown()
|
||
{
|
||
try
|
||
{
|
||
MySocket.Close(); //关闭套接字
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 发送消息
|
||
byte[] sendbyte { get; set; }
|
||
private static object sendLock = new Object();
|
||
/// <summary>
|
||
/// 发送消息
|
||
/// </summary>
|
||
public void Send(string strMgs)
|
||
{
|
||
try
|
||
{
|
||
lock (sendLock)
|
||
{
|
||
if (!string.IsNullOrEmpty(strMgs))
|
||
{
|
||
//发送操作指令
|
||
sendbyte = System.Text.Encoding.ASCII.GetBytes(strMgs);
|
||
//sendbyte = System.Text.Encoding.Default.GetBytes(strMgs);
|
||
if (MySocket != null)
|
||
{
|
||
MySocket.Send(sendbyte);
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 接收消息
|
||
byte[] Message { get; set; }
|
||
int lenght { get; set; }
|
||
/// <summary>
|
||
/// 接收消息A
|
||
/// </summary>
|
||
public string ReceivesMessageA()
|
||
{
|
||
Message = new byte[1024 * 1024 * 1]; //1.1创建消息缓存容器
|
||
lenght = MySocket.Receive(Message); //1.2接收的消息并存入缓存区,注意Receive也会阻断当前线程
|
||
TheReceives = System.Text.Encoding.ASCII.GetString(Message, 0, lenght);
|
||
return TheReceives;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 接收消息B
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string ReceivesMessageB()
|
||
{
|
||
Message = new byte[1024 * 1024 * 1]; //1.1创建消息缓存容器
|
||
MySocket.Receive(Message); //1.2接收的消息并存入缓存区,注意Receive也会阻断当前线程
|
||
TheReceives = System.Text.Encoding.UTF8.GetString(Message);
|
||
//TheReceives = Encoding.Default.GetString(Message);
|
||
//TheReceives = System.Text.Encoding.ASCII.GetString(Message, 0, lenght);
|
||
return TheReceives;
|
||
}
|
||
#endregion
|
||
|
||
#region 启动服务器监听
|
||
/// <summary>
|
||
/// 启动服务器监听
|
||
/// </summary>
|
||
/// <param name="ip">监听IP</param>
|
||
/// <param name="port">监听端口</param>
|
||
public void StartServer()
|
||
{
|
||
try
|
||
{
|
||
IsServer = true;
|
||
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
IPAddress address = IPAddress.Parse(CommunicationIP);
|
||
IPEndPoint endpoint = new IPEndPoint(address, CommunicationPort);
|
||
ServerSocket.Bind(endpoint);
|
||
ServerSocket.Listen(10); // 最大连接数10,可根据需求调整
|
||
ConnStatu.StatuInfo = "服务器已启动,等待客户端连接";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ConnStatu.StatuInfo = "服务器启动失败: " + ex.Message;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 接受客户端连接(同步方式)
|
||
/// </summary>
|
||
public void AcceptClient()
|
||
{
|
||
try
|
||
{
|
||
if (ServerSocket != null)
|
||
{
|
||
MySocket = ServerSocket.Accept();
|
||
ConnStatu.StatuInfo = "客户端已连接";
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ConnStatu.StatuInfo = "接受客户端失败: " + ex.Message;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 接收到的信息
|
||
private string theReceives;
|
||
/// <summary>
|
||
/// 接收到的信息
|
||
/// </summary>
|
||
public string TheReceives
|
||
{
|
||
get { return theReceives; }
|
||
set
|
||
{
|
||
theReceives = value;
|
||
this.RaisePropertyChanged("TheReceives");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 服务器监听配置
|
||
/// <summary>
|
||
/// 是否作为服务器
|
||
/// </summary>
|
||
public bool IsServer { get; set; }
|
||
|
||
/// <summary>
|
||
/// 服务器监听Socket
|
||
/// </summary>
|
||
public Socket ServerSocket { get; private set; }
|
||
#endregion
|
||
|
||
// 在RobotSocket类中添加如下方法(如未有)
|
||
public Socket AcceptClientWithReturn()
|
||
{
|
||
if (ServerSocket == null) return null;
|
||
try
|
||
{
|
||
return ServerSocket.Accept();
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|