自动存包柜

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Web;
using System.Web.Services;

namespace LockerService
{
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        static WebService()
        {
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            clientSocket.Connect(ipe);
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }


        public static Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        public static string host = ConfigurationManager.AppSettings["ip"];

        public static int port = int.Parse(ConfigurationManager.AppSettings["port"]);


        [WebMethod]
        public void Open(string sendStr)
        {
            try
            {
                SendCommand(sendStr);
            }
            catch
            {
                OpenLink();
            }
        }



        public void OpenLink()
        {
            try
            {
                clientSocket.Dispose();
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                clientSocket.Connect(ipe);
            }
            catch (Exception ex)
            {
                OpenLink();
            }

        }




        /// <summary>
        /// 发送指令
        /// </summary>
        /// <param name="number"></param>
        public void SendCommand(string sendStr)
        {

            //send message
            byte[] sendBytes = strToToHexByte(sendStr);

            clientSocket.Send(sendBytes);
        }

        /// <summary>
        /// 字符串转换16进制byte数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
            return returnBytes;
        }


        #region 写文件
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <param name="Strings">文件内容</param>
        public void WriteFile(string FileFullPath, string Strings)
        {
            if (!System.IO.File.Exists(FileFullPath))
            {
                System.IO.FileStream fs = System.IO.File.Create(FileFullPath);
                fs.Close();
            }
            System.IO.StreamWriter sw = new System.IO.StreamWriter(FileFullPath, false, System.Text.Encoding.GetEncoding("gb2312"));
            sw.Write(Strings);
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }

        #endregion
    }
}

作者:【唐】三三

出处:https://www.cnblogs.com/tangge/p/4994236.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   【唐】三三  阅读(286)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-11-25 MVC - 17.OA项目
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示