具有多个连接的TCP侦听端口 [英] TCP Listen port with multiple connections

查看:85
本文介绍了具有多个连接的TCP侦听端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有这个代码用于TCPListenPort。代码工作正常,但我的经理

要求我建立到同一端口的多个连接。我怎么能实现
下面的
是我的代码


Int32 port = Int32.Parse(ConfigurationManager。 AppSettings [" port"]);


IPAddress localAddr =

IPAddress.Parse(ConfigurationManager.AppSettings [" IpAddress"]);

TcpListener server = new TcpListener(localAddr,port);

server.Start();

Byte [] bytes = new Byte [256];

字符串数据= null;


//输入监听循环。

while(true)

{

//执行阻止调用以接受请求。

TcpClient client = server.AcceptTcpClient();

remotePoint = client.Client .RemoteEndPoint.ToString();

data = null;

NetworkStream stream = client.GetStream();

int i;


//循环接收clie nt发送的所有数据。

while((i = stream.Read(bytes,0,bytes.Length))! = 0)

{

//翻译数据字节到ASCII字符串。

data = System.Text.Encoding.ASCII.GetString(bytes,

0,i);

//处理客户端发送的数据。

data = data.ToUpper();

byte [] msg =

System.Text.Encoding .ASCII.GetBytes(数据);

if(msg.Length 5)

{


byte [] msgClone =

writeClassLibrary.getReturnBytes.ReturnData(_sessi onDisplay,msg);

stream.Write(msgClone,0,msgClone.Length);

}


}

//关机和结束连接

client.Close();

}

Hello everyone,
I have this code for TCPListenPort. The code works fine, but my manager is
asking me to establish multiple connections to the same port. How can i
acheive that

below is my code

Int32 port = Int32.Parse(ConfigurationManager.AppSettings["port"]);

IPAddress localAddr =
IPAddress.Parse(ConfigurationManager.AppSettings["IpAddress"]);
TcpListener server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;

// Enter the listening loop.
while (true)
{
// Perform a blocking call to accept requests.
TcpClient client = server.AcceptTcpClient();
remotePoint = client.Client.RemoteEndPoint.ToString();
data = null;
NetworkStream stream = client.GetStream();
int i;

// Loop to receive all the data sent by the clie nt.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes,
0, i);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg =
System.Text.Encoding.ASCII.GetBytes(data);
if (msg.Length 5)
{

byte[] msgClone =
writeClassLibrary.getReturnBytes.ReturnData(_sessi onDisplay, msg);
stream.Write(msgClone, 0, msgClone.Length);
}

}
// Shutdown and end connection
client.Close();
}

推荐答案

Vinki写道:
Vinki wrote:

我有这个代码用于TCPListenPort。代码工作正常,但我的经理

要求我建立到同一端口的多个连接。我怎么能实现

I have this code for TCPListenPort. The code works fine, but my manager is
asking me to establish multiple connections to the same port. How can i
acheive that



恕我直言,最直接的方法是使用async方法

TcpClient,所以你不必在循环中进行处理。

这就是处理阻止你与新客户打交道

直到完全使用当前的一个。


如果你想让代码真的很好,你可以使

TcpListener使用异步方法也是如此。那么这些代码都不会在特定的帖子中存在。


Pete

IMHO, the most straightforward way would be to use the async methods for
the TcpClient, so that you don''t have to do the processing in the loop.
It''s that processing that prevents you from dealing with a new client
until you''re completely done with the current one.

If you wanted to make the code really nice, you could make the
TcpListener use the async methods as well. Then none of this code would
have to exist in a specific thread.

Pete


你能指点我的异步方法的任何代码。


谢谢


Peter Duniho写道:
can you point me to any example code for async method.

Thanks

"Peter Duniho" wrote:

Vinki写道:
Vinki wrote:

我有这个代码用于TCPListenPort。代码工作正常,但我的经理

要求我建立到同一端口的多个连接。我怎么能够
实现
I have this code for TCPListenPort. The code works fine, but my manager is
asking me to establish multiple connections to the same port. How can i
acheive that



恕我直言,最直接的方法是使用异步方法来支付
TcpClient,所以你不必在循环中进行处理。

这就是处理阻止你与新客户打交道

直到完全使用当前的一个。


如果你想让代码真的很好,你可以使

TcpListener使用异步方法也是如此。那么这些代码都不会在特定的帖子中存在。


Pete


IMHO, the most straightforward way would be to use the async methods for
the TcpClient, so that you don''t have to do the processing in the loop.
It''s that processing that prevents you from dealing with a new client
until you''re completely done with the current one.

If you wanted to make the code really nice, you could make the
TcpListener use the async methods as well. Then none of this code would
have to exist in a specific thread.

Pete

你是否提到过类似的东西

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


" Peter Duniho"写道:
Are you mentioning to something like this

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

"Peter Duniho" wrote:

Vinki写道:
Vinki wrote:

我有这个代码用于TCPListenPort。代码工作正常,但我的经理

要求我建立到同一端口的多个连接。我怎么能够
实现
I have this code for TCPListenPort. The code works fine, but my manager is
asking me to establish multiple connections to the same port. How can i
acheive that



恕我直言,最直接的方法是使用异步方法来支付
TcpClient,所以你不必在循环中进行处理。

这就是处理阻止你与新客户打交道

直到完全使用当前的一个。


如果你想让代码真的很好,你可以使

TcpListener使用异步方法也是如此。那么这些代码都不会在特定的帖子中存在。


Pete


IMHO, the most straightforward way would be to use the async methods for
the TcpClient, so that you don''t have to do the processing in the loop.
It''s that processing that prevents you from dealing with a new client
until you''re completely done with the current one.

If you wanted to make the code really nice, you could make the
TcpListener use the async methods as well. Then none of this code would
have to exist in a specific thread.

Pete

这篇关于具有多个连接的TCP侦听端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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