SendFile over TCP/IP .NET CF

大城市里の小女人 提交于 2020-01-06 06:53:06

问题


I would like to send a file ("Label.prn") from a Windows Mobile 6.5 Device to a printer. This happens over TCP/IP. I have realized this application as a test on a normal Desktop-Client. It is very simple and easy. Just send the file over the TCP Socket and the printer does its job.

Here is the desktop client demo (works just fine):

private void buttonSend_Click(object sender, EventArgs e)

    {
        TcpClient client = new TcpClient(AddressFamily.InterNetwork);
        client.Connect(IPAddress.Parse("192.168.1.5"), 6101);
        if (client.Connected)
        {
            client.Client.SendFile("C:\\Label.prn");
            client.Close();
        }
    }

Now I want to do the same on the mobile part. The problem is: There is no SendFile() method!

This is how I tried to solve it. It either fails with an Exception "The connection was closed by the printer" or it just executes the code, but nothing happens. Have you got any suggestions? Maybe I'm doing something wrong. How can I send a file over a TCP Socket on .NET CF 3.5?

Here the code sample:

 Exception returnException = new Exception("The file could not be sent to the printer!");
 TcpClient client = null;

        try
        {
            FileStream fs = new FileStream(strFilePath, FileMode.Open);
            StreamReader reader = new StreamReader(fs);


            string strBuffer = reader.ReadToEnd();

            strBuffer = PrinterHelper.FillTestInformations(strBuffer);

            byte[] array = new byte[strBuffer.Length];
            array = Encoding.ASCII.GetBytes(strBuffer);

            //Tried manually and with ASCII-Encoding.
            //for (int n = 0; n < strBuffer.Length; n++)
            //{

            //    array[n] = Convert.ToByte(strBuffer[n]);
            //}

            client = new TcpClient();
            client.Connect(IPAddress.Parse(strIP), nPort);

            // Gets executed but nothing seems to happen.
            client.GetStream().Write(array, 0, array.Length);

            // Exception!
            //client.Client.Send(array, array.Length, SocketFlags.None);
        }
        catch (Exception ex)
        {
            returnException = new Exception("The label could not be printed!", ex);
        }
        finally
        {
            try
            {
                client.Close();
            }
            catch
            {

            }
        }

回答1:


I could solve this issue. Apparently it was an error with the Length of the string passed.

I initialized my byte array like this:

byte[] array = new byte[strBuffer.Length];

The problem is that the string.Length wasn't accurate. If I initialize the Byte array implicit then the problem doesn't occur.

byte[] array = Encoding.ASCII.GetBytes(strBuffer);

Thanks for the support. I hope this helps other people trying to solve the issue.

Following is the new code:

FileStream fs = new FileStream(strFilePath, FileMode.Open);
StreamReader reader = new StreamReader(fs);
string strBuffer = reader.ReadToEnd();
reader.Close();

strBuffer = PrinterHelper.FillTestInformations(strBuffer);

byte[] array = Encoding.ASCII.GetBytes(strBuffer);
client = new TcpClient(AddressFamily.InterNetwork);
client.Connect(IPAddress.Parse(strIP), nPort);
client.GetStream().Write(array, 0, array.Length);
client.Close();


来源:https://stackoverflow.com/questions/20325795/sendfile-over-tcp-ip-net-cf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!