异步套接字和缓冲区大小 [英] Asynchronous sockets and buffer size

查看:82
本文介绍了异步套接字和缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在使用异步套接字和AsyncCallback以及

System.Net.Sockets。


一切都运行良好,除非是我希望收到比我的缓冲区可以容纳的更多

总字节数。


i打开与套接字上的8k缓冲区的连接。如果服务器

发送给我5k并关闭连接它工作正常。如果服务器发给我

10k,我的BeginReceive回调(它会循环)给我8k的数据,

,之后EndReceive返回0,所以我关闭了我的套接字。


但是,如果我将缓冲区设置为32k,我可以获得所有数据。


是否应该像这样工作?

been using asynchronous sockets with AsyncCallback and
System.Net.Sockets as per the excellent MSDN example.

everything has been working great, except when i want to receive more
total bytes than my buffer can hold.

i open a connection with an 8k buffer on the socket. if the server
sends me 5k and closes connection it works fine. if the server sends me
10k, my BeginReceive callback (which does loop) gives me 8k of the data,
after which EndReceive returns 0 and so i close my socket.

however, if i set the buffer to 32k i can get all the data.

is it supposed to work like that?

推荐答案

马修,我不太确定哪个'优秀的MSDN示例''你是
跟随。通常TCP / IP服务器(我假设你做TCP / IP)不模仿

断开连接 - 客户端。在服务器端不要终止客户端

连接,直到客户端断开连接 - 服务器端的BeginReceive()

返回0,你不会丢失数据。


Michael


" Matthew Geyer" < no **** @ geyer.matt在邮件中写道

news:ui ************** @ TK2MSFTNGP05.phx.gbl .. 。
Matthew, I am not quite sure which ''excellent MSDN example'' you were
following. Normally TCP/IP servers (I assume you do TCP/IP) do not imitate
disconnect - clients do. On the server side do not terminate client
connection until the client disconnects - BeginReceive() on the server side
returns 0, and you will not loose data.

Michael

"Matthew Geyer" <no****@geyer.matt.gmail.comwrote in message
news:ui**************@TK2MSFTNGP05.phx.gbl...

使用异步套接字与AsyncCallback和System.Net.Sockets

按照优秀的MSDN示例。


一切都运行得很好,除非我想收到比我的缓冲区可以容纳的更多

总字节数。


i打开一个连接套接字上有8k缓冲区。如果服务器发送
我5k并关闭连接它工作正常。如果服务器发送给我10k,我的

BeginReceive回调(它循环)给我8k的数据,在

后,EndReceive返回0,所以我关闭了我的套接字。


但是,如果我将缓冲区设置为32k,我可以获得所有数据。


是否应该像这样工作?
been using asynchronous sockets with AsyncCallback and System.Net.Sockets
as per the excellent MSDN example.

everything has been working great, except when i want to receive more
total bytes than my buffer can hold.

i open a connection with an 8k buffer on the socket. if the server sends
me 5k and closes connection it works fine. if the server sends me 10k, my
BeginReceive callback (which does loop) gives me 8k of the data, after
which EndReceive returns 0 and so i close my socket.

however, if i set the buffer to 32k i can get all the data.

is it supposed to work like that?



对不起,我不知道我的期望和那样的解释。


这里是我的代码就像所有的绒毛一样:


private void OnReceive(IAsyncResult asyn)

{

ObjectThing Job =(ObjectThing)asyn.AsyncState;

long BytesReceived = Job.s.EndReceive(asyn);


if(BytesReceived 0 )

{

ProcessData(作业);

Job.s.BeginReceive(Job.buffer,0,Job.buffer.Length,0 ,新的

AsyncCallback(this.OnReceive),Job);

}


其他

{

Job.s.Close();

Job.s = null;

}


}


private void OnSend(IAsyncResult asyn)

{

ObjectThing Job =(ObjectThing)asyn.AsyncState;

Job.s.EndSend(asyn);

Job.s.BeginReceive(Job.buffer,0,Job.buffer.Length,0,new

AsyncCallback(this.OnReceive),工作);

}


private void OnConnect(IAsyncResult asyn)

{


ObjectThing Job =(ObjectThing)asyn.AsyncState;

Job.s.EndConnect(asyn);

Job.s.BeginSend(Encoding.ASCII.GetBytes(Job) .data +" \\\\ nn"),0,

Encoding.ASCII.GetBytes(Job.data +" \\\\ n")。长度,0,新

AsyncCallback(this.OnSend),Job);


}

Job.s是套接字,我打电话给Job。 s.BeginConnect(...)开始。

Michael Rubinstein写道:
Sorry, I don''t know what I expected with an explanation like that.

here''s what my code is like with all the fluff cut out:

private void OnReceive(IAsyncResult asyn)
{
ObjectThing Job = (ObjectThing)asyn.AsyncState;
long BytesReceived = Job.s.EndReceive(asyn);

if (BytesReceived 0)
{
ProcessData(Job);
Job.s.BeginReceive(Job.buffer, 0, Job.buffer.Length, 0, new
AsyncCallback(this.OnReceive), Job);
}

else
{
Job.s.Close();
Job.s = null;
}

}

private void OnSend(IAsyncResult asyn)
{
ObjectThing Job = (ObjectThing)asyn.AsyncState;
Job.s.EndSend(asyn);
Job.s.BeginReceive(Job.buffer, 0, Job.buffer.Length, 0, new
AsyncCallback(this.OnReceive), Job);
}

private void OnConnect(IAsyncResult asyn)
{

ObjectThing Job = (ObjectThing)asyn.AsyncState;
Job.s.EndConnect(asyn);
Job.s.BeginSend(Encoding.ASCII.GetBytes(Job.data + "\r\n"), 0,
Encoding.ASCII.GetBytes(Job.data + "\r\n").Length, 0, new
AsyncCallback(this.OnSend), Job);

}
Job.s is the socket and i call Job.s.BeginConnect(...) to start.
Michael Rubinstein wrote:

马修,我不太确定哪个''优秀MSDN示例''你是

跟随。通常TCP / IP服务器(我假设你做TCP / IP)不模仿

断开连接 - 客户端。在服务器端不要终止客户端

连接,直到客户端断开连接 - 服务器端的BeginReceive()

返回0,你不会丢失数据。


Michael
Matthew, I am not quite sure which ''excellent MSDN example'' you were
following. Normally TCP/IP servers (I assume you do TCP/IP) do not imitate
disconnect - clients do. On the server side do not terminate client
connection until the client disconnects - BeginReceive() on the server side
returns 0, and you will not loose data.

Michael


2007年5月26日星期六22:07:52 -0700,Matthew Geyer

< no **** @ geyer.matt.gmail.comwrote:
On Sat, 26 May 2007 22:07:52 -0700, Matthew Geyer
<no****@geyer.matt.gmail.comwrote:

对不起,我不知道我的期望是什么样的解释那个。


这里是我的代码是什么样的所有绒毛切断:[snip]
Sorry, I don''t know what I expected with an explanation like that.

here''s what my code is like with all the fluff cut out: [snip]



嗯,我至少看到一个问题:你没有检查

Socket.EndSend()的字节数。从文档:


如果你使用面向连接的协议,EndSend将阻止

直到

一些缓冲区寄了,送了。如果EndSend的返回值表示缓冲区未完全发送,则再次调用BeginSend方法



修改缓冲区以保持未发布的数据。


假设您的服务器中存在相同的错误,那么我不清楚

服务器实际上是在发送所有服务器你期望它发送的数据。


我注意到的另一件事是你使用Socket.Close()终止

连接。但是,这将重置连接。你应该使用Socket.Shutdown()确保优雅的闭包。
应该使用Socket.Shutdown()。同样,如果

服务器有相同的错误,可能是它在客户端有机会接收所有数据之前中止套接字

连接。如果

客户端和服务器使用Socket.Shutdown(),则连接将

保持半连接,直到客户端实际收到所有

已发送的数据。


因为你实际上没有发布你问题中涉及的所有代码,

它是'很难说肯定会出现什么问题。我只想说,这是不正常的,因为一端无法接收到另一端的所有数据。

结束发送。如果您遇到这个问题,您的代码中就会出现错误

。可能它与上述有关。


Pete

Well, I see at least one problem: you don''t check the byte count for
Socket.EndSend(). From the documentation:

If you are using a connection-oriented protocol, EndSend will block
until
some of the buffer was sent. If the return value from EndSend indicates
that the buffer was not completely sent, call the BeginSend method
again,
modifying the buffer to hold the unsent data.

Assuming you have the same error in your server, then it''s not clear to me
that the server is actually sending all of the data you expect it to send.

The other thing I note is that you are using Socket.Close() to terminate
the connection. However, that will reset the connection abortively. You
should use Socket.Shutdown() to ensure a graceful closure. Again, if the
server has the same error, it could be that it''s aborting the socket
connection before the client has a chance to receive all of the data. If
the client and server use Socket.Shutdown(), then the connection will
remain half-connected until the client has actually received all of the
data that has been sent.

Since you didn''t actually post all of the code involved in your question,
it''s hard to say for sure what''s going wrong. Suffice to say that it''s
not normal for one end to not be able to receive all of the data the other
end sent. If you are having that problem, there''s a bug in your code
somewhere. Possibly it''s related to the above.

Pete


这篇关于异步套接字和缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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