110,527
社区成员
发帖
与我相关
我的任务
分享
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("Send Message");
c.Send(bs, bs.Length, 0);//发送客户端请求
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using HurysCommon;
using System.Xml;
namespace HurysDataConventTools
{
class Program
{
static void Main(string[] args)
{
//1.Socket Client端接收数据 使用TCP/IP协议 长连接 获取四通诱导平台节目参数信息 即XML数据
//2.建立对象存储熙讯节目XML数据,分析熙讯LED显示内容的XML格式数据,了解相关参数.例如显示内容,间隔时间,背景色等等
//3.获取四通诱导平台数据并且处理为熙讯LED数据格式.将四通诱导平台的参数和熙讯LED中的XML参数关联.
//4.初始化熙讯LED
//5.将数据制作成XML文件并且使用熙讯协议中的编译函数编译该XML文件,然后调用interface.dll中的通讯API发送显示内容
try
{
IPAddress ip = IPAddress.Parse(SocketHelper._host);
IPEndPoint ipe = new IPEndPoint(ip, SocketHelper._port);//把ip和端口转化为IPEndPoint实例
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);//连接到诱导平台服务器
//1.登录
SocketHelper.GetResponseXml(SocketHelper._login, socket);
//2.读取用户对应的VMS配置
//SocketHelper.GetResponseXml(SocketHelper._vms, c);
//3.读取所有显示页
SocketHelper.GetResponseXml(SocketHelper._page, socket);
//4.解析四通诱导平台数据,并且赋值熙讯LED信息类
//5.建立熙讯LED所需节目XML字符串并且转换为XML文件
//6.熙讯LED相关API
XiXunLedProgram m_led = new XiXunLedProgram();
m_led.Call_CI_InitDLL();
string strPath = System.Environment.CurrentDirectory.Replace(@"\bin\Debug", @"\XiXunXmlFiles\demo.xml");
if (strPath == "")
{
return;
}
string strSave = System.IO.Path.GetDirectoryName(strPath);
bool b = m_led.Call_CI_CompileProgram(strPath, strSave);
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net.Sockets;
namespace HurysCommon
{
public class SocketHelper
{
public const string _host = "127.0.0.1";
public const int _port = 2000;
public static string _login = GetRequestXml("Login.xml");
public static string _error = GetRequestXml("Error.xml");
public static string _vms = GetRequestXml("Vms.xml");
public static string _page = GetRequestXml("Page.xml");
/// <summary>
/// 客户端请求数据包
/// </summary>
/// <returns></returns>
public static string GetRequestXml(string fileName)
{
string path = System.Environment.CurrentDirectory.Replace(@"\bin\Debug", string.Format(@"\SITS_XML\{0}", fileName));
XmlDocument xml = XmlHelper.LoadXmlDocumentFromFile(path);
if (!string.IsNullOrEmpty(xml.InnerXml))
{
return xml.InnerXml;
}
return "";
}
/// <summary>
/// 服务器返回数据包
/// </summary>
/// <returns></returns>
public static string GetResponseXml(string sendStr, Socket socket)
{
if (!string.IsNullOrEmpty(sendStr))
{
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("Send Message");
socket.Send(bs, bs.Length, 0);//发送客户端请求
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = socket.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
return recvStr;
}
return "";
}
}
}
/// <summary>
/// 服务器返回数据包
/// </summary>
/// <returns></returns>
public static string GetResponseXml(string sendStr, Socket socket)
{
if (!string.IsNullOrEmpty(sendStr))
{
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("Send Message");
socket.Send(bs, bs.Length, 0);//发送客户端请求
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = socket.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
return recvStr;
}
return "";
}
以下是三次指令调用
//1.登录
SocketHelper.GetResponseXml(SocketHelper._login, socket);
//2.读取用户对应的VMS配置
//SocketHelper.GetResponseXml(SocketHelper._vms, c);
//3.读取所有显示页
SocketHelper.GetResponseXml(SocketHelper._page, socket);