[英]How do you send a file via HTTP over tcp sockets in C?
I am implementing a client and server in C (on linux) and i want to send a text file to a server from the client using an HTTP PUT message. 我正在用C(在Linux上)实现客户端和服务器,我想使用HTTP PUT消息从客户端向服务器发送文本文件。
I'm not really sure how to do this. 我不太确定该怎么做。 Do I first send the HTTP request and header line over the socket, and then send the file over the socket piece by piece using a buffer?
我是否首先通过套接字发送HTTP请求和标头行,然后使用缓冲区逐个通过套接字发送文件? or do I need to prepend every piece of the text file with its own HTTP request and header line before I send them over?
还是在发送文本文件之前,需要在文本文件的每个部分之前添加自己的HTTP请求和标题行?
I also read about this function called sendfile that seems like it would make this easier, but I wasn't sure how I would prepend an HTTP header and request line to the file if sendfile just sends the file straight to the socket. 我还阅读了有关名为sendfile的函数的信息,该函数看起来似乎会使它更容易,但是我不确定如果sendfile只是将文件直接发送到套接字,我该如何在该文件的前面加上HTTP标头和请求行。
Thank you for your help. 谢谢您的帮助。
Do I first send the HTTP request and header line over the socket, and then send the file over the socket piece by piece using a buffer?
我是否首先通过套接字发送HTTP请求和标头行,然后使用缓冲区逐个通过套接字发送文件?
Yes. 是。
or do I need to prepend every piece of the text file with its own HTTP request and header line before I send them over?
还是在发送文本文件之前,需要在文本文件的每个部分之前添加自己的HTTP请求和标题行?
No. 没有。
I also read about this function called sendfile that seems like it would make this easier, but I wasn't sure how I would prepend an HTTP header and request line to the file if sendfile just sends the file straight to the socket.
我还阅读了有关名为sendfile的函数的信息,该函数看起来似乎会使它更容易,但是我不确定如果sendfile只是将文件直接发送到套接字,我该如何在该文件的前面加上HTTP标头和请求行。
First send the header with a normal send
, then call sendfile
. 先用正常发送头
send
,然后调用sendfile
。 However you should read the documentation, which says 但是,您应该阅读说明文件
Note that a successful call to sendfile() may write fewer bytes than requested;
请注意,成功调用sendfile()可能会写入少于请求的字节数。 the caller should be prepared to retry the call if there were unsent bytes.
如果有未发送的字节,则呼叫者应准备重试呼叫。
This means that your life is not that much easier with sendfile
. 这意味着
sendfile
使您的生活变得不那么轻松。 It is more efficient than a combination of read
and write
, but the amount of work the programmer needs to do is similar in both cases. 它比的组合更有效的
read
和write
,但工作的程序员需要做的量是在两种情况下类似。
if you want to implement HTTP transmission protocol then check this RFC http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.2 如果要实现HTTP传输协议,请检查此RFC http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.2
else if you want your own easy method then you can for example implement an API such DWORD(cmdPut)+filename+'\\0'+DWORD(fileLen)+fileBinaryData.
否则,如果您想要自己的简单方法,则可以实现一个API,例如
DWORD(cmdPut)+filename+'\\0'+DWORD(fileLen)+fileBinaryData.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.