Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
<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


C#
#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



C#
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();
           
        }
Posted

1 solution

T T
I need for help
Please[][]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900