基于 TCP/IP 的 SendFile .NET CF [英] SendFile over TCP/IP .NET CF

查看:71
本文介绍了基于 TCP/IP 的 SendFile .NET CF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Windows Mobile 6.5 设备向打印机发送文件(Label.prn").这发生在 TCP/IP 上.我已经意识到这个应用程序是对普通桌面客户端的测试.这是非常简单和容易的.只需通过 TCP 套接字发送文件,打印机就会完成它的工作.

这是桌面客户端演示(工作正常):

<块引用>

private void buttonSend_Click(object sender, EventArgs e)

<代码> {TcpClient 客户端 = new TcpClient(AddressFamily.InterNetwork);客户端连接(IPAddress.Parse(192.168.1.5"),6101);如果(客户端.已连接){client.Client.SendFile("C:\\Label.prn");客户端.关闭();}}

现在我想在移动部分做同样的事情.问题是:没有 SendFile() 方法!

这就是我试图解决它的方式.它要么因异常连接已被打印机关闭"而失败,要么只是执行代码,但没有任何反应.你有什么建议吗?也许我做错了什么.如何通过 .NET CF 3.5 上的 TCP 套接字发送文件?

这里是代码示例:

<块引用>

 Exception returnException = new Exception("文件无法发送到打印机!");TcpClient 客户端 = null;尝试{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];数组 = Encoding.ASCII.GetBytes(strBuffer);//手动尝试并使用ASCII编码.//for (int n = 0; n < strBuffer.Length; n++)//{//array[n] = Convert.ToByte(strBuffer[n]);//}客户端 = 新的 TcpClient();客户端连接(IPAddress.Parse(strIP),nPort);//被执行,但似乎什么也没发生.client.GetStream().Write(array, 0, array.Length);//例外!//client.Client.Send(array, array.Length, SocketFlags.None);}捕获(异常前){returnException = new Exception("无法打印标签!", ex);}最后{尝试{客户端.关闭();}抓住{}}

解决方案

我可以解决这个问题.显然这是传递的字符串长度错误.

我像这样初始化了我的字节数组:

<块引用>

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

问题是 string.Length 不准确.如果我隐式初始化 Byte 数组,则问题不会发生.

<块引用>

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

感谢支持.我希望这能帮助其他试图解决问题的人.

以下是新代码:

<块引用>

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);客户端 = 新的 TcpClient(AddressFamily.InterNetwork);客户端连接(IPAddress.Parse(strIP),nPort);client.GetStream().Write(array, 0, array.Length);客户端.关闭();

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
            {

            }
        }

解决方案

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();

这篇关于基于 TCP/IP 的 SendFile .NET CF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆