コード例 #1
0
 public ActionResult LabelNetwork()
 {
     PrintLabelTestModel model = new PrintLabelTestModel
     {
         HostName = GetHostName(),
         Port = GetPort()
         //Zpl = GetLabelMultiZpl()
     };
     return View(model);
 }
コード例 #2
0
        public ActionResult LabelNetwork(PrintLabelTestModel model)
        {
            model.Message = null;

            NetworkStream ns = null;
            Socket socket = null;
            try
            {
                IPEndPoint printerIP = new IPEndPoint(IPAddress.Parse(GetHostName()),  GetPort());

                socket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,
                    ProtocolType.Tcp);
                socket.Connect(printerIP);

                ns = new NetworkStream(socket);

                model.Zpl = GetLabelMultiZpl();
                byte[] toSend = Encoding.ASCII.GetBytes(model.Zpl);
                ns.Write(toSend, 0, toSend.Length);
                model.Message = "Label Sent!";
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }
            finally
            {
                if (ns != null)
                    ns.Close();

                if (socket != null && socket.Connected)
                    socket.Close();
            }
            return View(model);
        }