套接字和缓冲区大小 [英] Sockets and Buffer size

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

问题描述




我需要使用套接字写一个应用程序。

我有一台服务器和大约10个客户端说话同时使用

服务器,所以我想我需要使用异步套接字。

但服务器将从客户端收到带有

可变长度。

在我发现的所有示例中,缓冲区的大小始终是固定的

(private byte [] theBuffer = new byte [ 1024])。

我可能误解了缓冲区的使用!

我以为我可以选择最大尺寸,然后决定一个

特殊字符(例如#)将结束流,但我不确定,

并且不知道该怎么做?!


感谢您的帮助!

Hi,

I need to write an application using sockets.
I have a server and about 10 clients "speaking" at the same time with the
server, so i guess i need to use asynchronous sockets.
But the server will receive from the clients an ascii string with a
viariable length.
And in all the example i found, the size of the buffer is always fixed
(private byte[] theBuffer = new byte[1024]).
I probably misunderstood the use of the buffer !
I thought i could choose the largest size possible, and then decide that a
special char (# for example) will end the stream, but i''m not sure of this,
and don''t know how to do this ?!

Thanks for any help !

推荐答案

" Steven" <无** @ none.com>在消息中写道

news:uk ************** @ TK2MSFTNGP10.phx.gbl ...
"Steven" <no**@none.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...

我需要使用套接字编写一个应用程序。
我有一个服务器和大约10个客户端说话与
服务器同时,所以我想我需要使用异步套接字。
但服务器将从客户端收到一个长度为
的ascii字符串。
在我发现的所有示例中,缓冲区的大小始终是固定的(private byte [] theBuffer = new byte [1024])。
我可能误解了缓冲区的使用!
我想我可以选择最大的尺寸,然后决定一个
特殊字符(例如#)将结束流,但我不确定
这个,并且'''''知道怎么做了吗?!

感谢您的帮助!
Hi,

I need to write an application using sockets.
I have a server and about 10 clients "speaking" at the same time with the
server, so i guess i need to use asynchronous sockets.
But the server will receive from the clients an ascii string with a
viariable length.
And in all the example i found, the size of the buffer is always fixed
(private byte[] theBuffer = new byte[1024]).
I probably misunderstood the use of the buffer !
I thought i could choose the largest size possible, and then decide that a
special char (# for example) will end the stream, but i''m not sure of
this, and don''t know how to do this ?!

Thanks for any help !




缓冲区的大小始终是固定的,因为完整的消息必须总是

由零件组装而成。这是因为发件人和网络可以和/ b $ b将邮件分成几块 - 你可以将它发送为100字节,但

收到它为10,30和60 [这只是一个例子 - 在现实生活中,

块更大]。这是因为TCP是一种流协议,而不是像UDP这样的数据报协议(对于

同样的原因,它不能处理大量的消息)。


由于上述情况,以

a固定大小格式发送长度几乎总是更好,而不是使用终结器。



The size of the buffer is always fixed because the full message must always
be assembled from parts. This is because the sender and the network can and
will chop the message into pieces - You might send it as 100 bytes but
receive it as 10,30 and 60 [This is an example only - in real life the
chunks are much bigger]. This is because TCP is a stream protocol not a
datagram protocol such as UDP (which wont work with huge messages for the
same reasons).

Because of the above it is almost always better to send the length first in
a fixed size format rather than use a terminator.


Hi Steven;


你可以使用


Sockets.Bind(YourLocalEndPoint)

BeginRereive( )

EndReceive()


EndReceive()返回数据包的长度。

假设你的缓冲区是2048字节,但你的数据包的大小是230.

然后EndReceive()返回230,现在你可能只从缓冲区复制前230个
字节到本地例如,数组和处理它。


我还在学习这个,因为我现在正在实施它。

祝你好运。

-----原始消息-----

来自:Steven [mailto:no ** @ none。 com]

发表于:2006年3月8日星期三上午8:02

发布至:microsoft.public.dotnet.languages.csharp

会话:套接字和缓冲区大小

主题:套接字和缓冲区大小





我需要使用套接字写一个应用程序。

我有一个服务器和大约10个客户端说话与

同时使用

服务器,所以我想我需要使用异步套接字。

但服务器将从客户端收到一个带有

可变长度的ascii字符串。

在我发现的所有示例中,缓冲区的大小始终是固定的

(私有) byte [] theBuffer = new byte [1024])。

我可能误解了缓冲区的使用!

我以为我可以选择最大可能的大小,然后决定那个

a

特殊字符(例如#)将结束流,但我不确定

这个,
并且不知道怎么做?!


感谢您的帮助!

Hi Steven;

You sould to use

Sockets.Bind(YourLocalEndPoint)
BeginRereive()
EndReceive()

EndReceive() returns the length of the packet.
Let''s say your buffer is 2048 bytes, but the size of your packet is 230.
Then EndReceive() returns 230, now you might copy only the first 230
bytes from the buffer to a local array and process it for example.

I''m still learning this because I''m implementing it right now.
Good luck.
-----Original Message-----
From: Steven [mailto:no**@none.com]
Posted At: Wednesday, March 08, 2006 8:02 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Sockets and Buffer size
Subject: Sockets and Buffer size

Hi,

I need to write an application using sockets.
I have a server and about 10 clients "speaking" at the same time with
the
server, so i guess i need to use asynchronous sockets.
But the server will receive from the clients an ascii string with a
viariable length.
And in all the example i found, the size of the buffer is always fixed
(private byte[] theBuffer = new byte[1024]).
I probably misunderstood the use of the buffer !
I thought i could choose the largest size possible, and then decide that
a
special char (# for example) will end the stream, but i''m not sure of
this,
and don''t know how to do this ?!

Thanks for any help !





看看
http://msdn2.microsoft.com/en-us/lib...f5(VS.80).aspx


如果您使用的是无连接protoco如UDP,你根本不需要

来监听连接。调用ReceiveFrom方法接受

任何传入的数据报。使用SendTo方法将数据报发送到

远程主机。


祝你好运


---- - 原创讯息-----

来自:史蒂文[mailto:no ** @ none.com]

发表于:2006年3月8日星期三8:02 AM

发布到:microsoft.public.dotnet.languages.csharp

对话:套接字和缓冲区大小

主题:套接字和缓冲区大小





我需要使用套接字写一个应用程序。


我有一台服务器和大约10个客户说话同时使用


服务器,所以我想我需要使用异步套接字。


但服务器将从客户端收到一个ascii字符串,其长度为




在我找到的所有示例中,大小缓冲区总是固定的


(私有字节[] theBuffer =新字节[1024])。


我可能误解了使用缓冲区!


我以为我可以选择最大尺寸,然后决定

a


特价char(#例如)将结束流,但我不确定

这个,


并且不知道该怎么做这个?!


感谢您的帮助!





Take a look at
http://msdn2.microsoft.com/en-us/lib...f5(VS.80).aspx

If you are using a connectionless protocol such as UDP, you do not need
to listen for connections at all. Call the ReceiveFrom method to accept
any incoming datagrams. Use the SendTo method to send datagrams to a
remote host.

Good luck

-----Original Message-----
From: Steven [mailto:no**@none.com]
Posted At: Wednesday, March 08, 2006 8:02 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Sockets and Buffer size
Subject: Sockets and Buffer size

Hi,

I need to write an application using sockets.

I have a server and about 10 clients "speaking" at the same time with
the

server, so i guess i need to use asynchronous sockets.

But the server will receive from the clients an ascii string with a

viariable length.

And in all the example i found, the size of the buffer is always fixed

(private byte[] theBuffer = new byte[1024]).

I probably misunderstood the use of the buffer !

I thought i could choose the largest size possible, and then decide that
a

special char (# for example) will end the stream, but i''m not sure of
this,

and don''t know how to do this ?!

Thanks for any help !





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

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