socket发送数据和接收数据长度不一至

butnet 2009-08-29 11:26:18

我自己实现一个简单的HTTP服务器,其中处理发送文件内容时,
浏览器接收到的数据只是其中一部分,下面的我的处理代码
class httpprocessor
{

private Socket s;
private StreamReader sr;
private string method;
private string url;
private string protocol;
private Hashtable hashtable;

public httpprocessor( ref Socket s)
{
this.s = s;
hashtable = new Hashtable();
}

public void process()
{
try
{
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite);
sr = new StreamReader(ns);
parserequest();
readheaders();
writeurl();
s.Shutdown(SocketShutdown.Both);
s.Close();
}
catch (Exception e)
{
LogTools.write(e);
}
}

public void parserequest()
{
string request = sr.ReadLine();
LogTools.write(request);
string[] tokens = request.Split(' ');
method = tokens[0];
url = tokens[1];
int index = url.IndexOf('?');
if (index != -1)
{
url = url.Substring(0, index);
}
protocol = tokens[2];
}

public void readheaders()
{
string line;
while ((line = sr.ReadLine()) != null && line != "")
{
LogTools.write(line);
string[] tokens = line.Split(':');
string name = tokens[0];
string value = "";
for (int i = 1; i < tokens.Length; i++)
{
value += tokens[i];
if (i < tokens.Length - 1) tokens[i] += ":";
}
hashtable[name] = value;
}
}

public void writeurl()
{
try
{
string id = url.Substring(1);
ProofInfo p = LocalHttpServer.ProofPackagePanel.getProofInfoById(id);
if (p == null)
{
writefailure();
return;
}
FileInfo file = new FileInfo(p.Prf_Path);
//这里发送文件,但有些文件只能收到一部分,有些文件又能全部接收。
s.SendFile(file.FullName, writesuccess(file.Length), null, TransmitFileOptions.WriteBehind);
}
catch (FileNotFoundException e)
{
writefailure();
LogTools.write(e);
}
catch (IOException e)
{
LogTools.write(e);
}
}

public byte[] writesuccess(long len)
{
string str = "";
str += "http/1.0 200 ok\r\n";
str += "Server: LocalHttpServer\r\n";
str += "Cache-Control: max-age=31104000\r\n";
str += "Content-Type: image/jpeg\r\n";
str += "Accept-Ranges: bytes\r\n";
str += "Content-Length: " + len + "\r\n\r\n";
byte[] data = Encoding.ASCII.GetBytes(str);
return data;
}

public void writefailure()
{
string str = "";
str += "http/1.0 404 file not found\r\n";
str += "connection: close\r\n";
byte[] data = Encoding.ASCII.GetBytes(str);
s.Send(data);
}
}


比较急啊!!!
...全文
214 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
butnet 2009-08-30
  • 打赏
  • 举报
回复
各位大哥,可不可以给个例子。
先谢谢啦。
vipexxe@126.com
lextm 2009-08-30
  • 打赏
  • 举报
回复
最好是使用System.Net tracing查看一下发送和接受的包内容对不对,不对的话看看代码是哪里错了。

Microsoft Network Monitor也可以用来抓包和分析。
tangyong12 2009-08-30
  • 打赏
  • 举报
回复
肯定要设置长度的,需要手工封包
深海之蓝 2009-08-30
  • 打赏
  • 举报
回复
设定长度,并且在循环外在加一个循环,当size 大于1024时,继续接受
butnet 2009-08-30
  • 打赏
  • 举报
回复
在哪设置???

是在这吗??????
//这里发送文件,但有些文件只能收到一部分,有些文件又能全部接收。
s.SendFile(file.FullName, writesuccess(file.Length), null, TransmitFileOptions.WriteBehind);
whowhen21 2009-08-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chaozi_249 的回复:]
byte[] 要设置大小
[/Quote]
butnet 2009-08-30
  • 打赏
  • 举报
回复
啊,这么麻烦啊,
他应该自动的封包啊,
为什么还要手动重组。
CGabriel 2009-08-29
  • 打赏
  • 举报
回复
数据报要自己手动重组。否者要是几个包连在以前,再大的缓冲区都是白搭
chaozi_249 2009-08-29
  • 打赏
  • 举报
回复
byte[] 要设置大小
chaozi_249 2009-08-29
  • 打赏
  • 举报
回复
是不是接受的数组没有设置成1024的啊

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧