我如何从端口9100获取数据打印作业

问题描述:

如何从9100端口获取数据打印作业并导出到文本文件
现在我可以从端口9100上获取它,但是队列状态的状态仍在打印.
我不能马上进入第二个队列.

如何
1.如何清除挂起的队列并立即获取信息.

非常感谢大家

这是我的代码

How do i get Data print job from port 9100 and export to text file
Now I can get it from port 9100,but The status of the queue status is still printing.
I can not get in the queue at the second right away.

How to
1. How to clear the pending queue and get the information immediately.

Thank you very much everybody

this my code

<pre lang="c#"> #region btnConnect_Click
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (!IsConnect)
            {

                //Start ReciveFile from Port 9100
                StartReciveFile();
                IsConnect = true;
                btnConnect.Text = "Disconnect";
                this.lblStatusConnect.Text = "Connecting";
                this.lblStatusConnect.Image = global::UI.Properties.Resources.ico_Connect_22;


            }
            else
            {
                if (_thread != null)
                    _thread.Abort();
                if (Server != null)
                    Server.Close();
                IsConnect = false;
                btnConnect.Text = "Connect";
                this.lblStatusConnect.Text = "Disconnect";
                this.lblStatusConnect.Image = global::UI.Properties.Resources.ico_Disconnect_22;
            }


        }
        #endregion





#region StartReceiveFile
       private void StartReciveFile()
       {
           _thread = new Thread(new ThreadStart(ReceiveFile));
           _thread.Start();

       }
       #endregion

       #region ReceiveFile
       private void ReceiveFile()
       {
           while (true)
           {
               int intPort = Convert.ToInt32(this.txtPort.Text.Trim());
               IPEndPoint ip = new IPEndPoint(IPAddress.Any, intPort);
               Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
               using (Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
               {
                   try
                   {
                       //connect ip
                       Server.Bind(ip);
                       Server.Listen(10);


                       Socket Client = Server.Accept();

                       //receive data from Server
                       getReceiveData = 10;

                       Receive(Client);




                       Server.Close();


                   }
                   catch (SocketException ex)
                   {
                       string message = ex.Message.ToString();
                   }
               }
          }

       }
       #endregion







private static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket 
                // from the asynchronous state object.
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;
                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);
                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));


                    //  Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReceiveCallback), state);


                }
                else
                {

                  

                    // All the data has arrived; put it in response.
                    if (state.sb.Length > 1)
                    {
                        response = state.sb.ToString();

                        System.Text.Encoding enc = System.Text.Encoding.ASCII;
                        byte[] myByteArray = enc.GetBytes(response);

                        getFile(myByteArray);
                       

                    }
                    // Signal that all bytes have been received.
                    //receiveDone.Set();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private static void Receive(Socket client)
        {
            try
            {
                // Create the state object.
                StateObject state = new StateObject();
                state.workSocket = client;
        

                // Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback), state);

        


            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public static void getFile(Byte[] Files)
        {

            String LPath = @"D:\fileImport\FORD_" + DateTime.Now.ToShortDateString().Replace("/", "") + DateTime.Now.ToShortTimeString().Replace(":", "") + ".txt";

            if (File.Exists(LPath))
            {
                File.Delete(LPath);
            }

            FileStream fs = new FileStream(LPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Write);
            fs.Write(Files, 0, Files.Length);
            fs.Flush();
            fs.Close();
           
        }

TT
我需要帮助
[ ] [ ]
T T
I need for help
Please[][]