如何从端口读取文件内容 [英] How to read the content of file from port

查看:194
本文介绍了如何从端口读取文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人打印文件时,我需要在程序中使用文本数据.

I am in needed to use the text data in to a program when someone prints a file.

我对TCP/IP客户端和侦听器编程有基本的了解. 我已经可以在两台机器之间发送和接收txt文件了. 但是,如果文件为docx,xl​​x,pdf或任何其他格式,如何接收文件内容?

I have basic ideas about the TCP/IP client and listener programming. Already I could send and receive txt files between two machines. But how to receive the file contents if the files were in docx,xlx,pdf or any other format?

我的要求是,

当某人打印文件时,我想将文件的内容(文本)用于另一个程序. 如果有其他替代方法,请提出建议.

I wanted to use the contents (texts) of a file in to another program when someone prints a file. Please suggest me if there is some alternative ways to do it.

谢谢.

推荐答案

由于您尚未发布任何代码,因此我将以我的方式"编写代码部分,但阅读此书后应该有所了解.

Since you haven't posted any code I'll write the code part in "my way" but you should have a bit of understanding after reading this.

首先,在两端(客户端和服务器),您都应应用统一协议,该协议将描述您要发送的数据.示例可能是:

First on both of the ends ( client and server ) you should apply unified protocol which will describe what data you're sending. Example could be:

[3Bytes-ASCII扩展名] [4Bytes-lengthOfTheFile] [XBytes-fileContents]

然后在您的发送者中,您可以根据协议接收数据,这意味着您首先读取3个字节来确定哪种格式的文件,然后读取4个字节,这基本上将通知您要传入的文件有多大.最后,您必须阅读内容并将其直接写入文件.示例发件人可能如下所示:

Then in your sender you can receive data according to the protocol which means first your read 3 bytes to decide what format file has, then you read 4 bytes which will basically inform you how large file is incomming. Lastly you have to read the content and write it directly to the file. Example sender could look like this :

byte[] extensionBuffer = new byte[3];
if( 3 != networkStream.Read(extensionBuffer, 0, 3))
    return;

string extension = Encoding.ASCII.GetString(extensionBuffer);
byte[] lengthBuffer = new byte[sizeof(int)];
if(sizeof(int) != networkStream.Read(lengthBuffer, 0, 3))
    return;

int length = BitConverter.ToInt32(lengthBuffer, 0);
int recv = 0;
using (FileStream stream = File.Create(nameOfTheFile + "." + extension))
{
    byte @byte = 0x00;
    while( (@byte = (byte)networkStream.ReadByte() ) != 0x00)
    {
        stream.WriteByte(@byte);
        recv++;
    }
    stream.Flush();
}

在发送方,您可以阅读文件扩展名,然后打开文件流以获取流的长度,然后将流长度发送给客户端,并将每个字节从FileStream重定向到networkStream.看起来可能像这样:

On the sender part you can read the file extension then open up the file stream get the length of the stream then send the stream length to the client and "redirect" each byte from FileStream into a networkStream. This can look something like :

FileInfo meFile = //.. get the file
byte[] extBytes = Encoding.ASCII.GetBytes(meFile.Extension);
using(FileStream stream = meFile.OpenRead())
{
    networkStream.Write(extBytes, 0, extBytes.Length);
    networkStream.Write(BitConverter.GetBytes(stream.BaseStream.Length));
    byte @byte = 0x00;
    while ( stream.Position < stream.BaseStream.Length )
    {
        networkStream.WriteByte((byte)stream.ReadByte());
    }
}

这种方法相当容易实现,如果您要发送不同的文件类型,则不需要进行大的更改.它缺少一些验证器,但我认为您不需要此功能.

This approach is fairly easy to implement and doesn't require big changes if you want to send different file types. It lack some validator but I think you do not require this functionality.

这篇关于如何从端口读取文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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