【发布时间】:2011-05-08 11:30:35
【问题描述】:
我这里有一个用 C# 编写的小型 HTTP 服务器,直到现在我只需要将原始文本发送回发送者。但是现在我必须发送一个 JPG-Image 并且我不明白如何。
这就是我现在拥有的:
// Read the HTTP Request
Byte[] bReceive = new Byte[MAXBUFFERSIZE];
int i = socket.Receive(bReceive, bReceive.Length, 0);
//Convert Byte to String
string sBuffer = Encoding.ASCII.GetString(bReceive);
// Look for HTTP request
iStartPos = sBuffer.IndexOf("HTTP", 1);
// Extract the Command without GET_/ at the beginning and _HTTP at the end
sRequest = sBuffer.Substring(5, iStartPos - 1 - 5);
String answer = handleRequest(sRequest);
// Send the response
socket.Send(Encoding.UTF8.GetBytes(answer));
我想我必须做某种文件流而不是字符串,但我真的没有胶水..
【问题讨论】:
-
你能发布你的handleRequest方法的一部分吗?我猜这就是您构建 HTTP 响应对象的地方,该对象将被发送回发出请求的浏览器。您必须弄清楚如何修改它以支持图像。
-
如果你想借,我有胶水:/
-
@Martin 如果您正在从文件中读取,您可以调用 socket.SendFile 。请参阅此处(msdn.microsoft.com/en-us/library/sx0a40c2.aspx
标签: c# image http send tcplistener