如何使用C#中的TCP侦听器将消息从服​​务器发送到客户端而没有客户端ipaddress? [英] How to send the message from server to client with out client ipaddress using TCP listener in C#?

查看:84
本文介绍了如何使用C#中的TCP侦听器将消息从服​​务器发送到客户端而没有客户端ipaddress?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#中的TCP侦听器将消息从服​​务器发送到客户端而没有客户端ipaddress?



我的客户端应用程序使用TCP协议连接到服务器,主机IP地址和端口地址。

当时服务器收到客户端消息(消息可以使用tcp侦听器获取流并写入流)。



客户端还会收听服务器请求,然后向客户端显示服务器消息。



我的问题有时候任何消息发送到没有IP地址的任何客户端机器?请介绍如何发送给客户端?

How to send the message from server to client with out client ipaddress using TCP listener in C#?

My client application connect to server using TCP protocol with host ip address and port address.
at that time the server receive the client message (Message can getstream and write the stream) using tcp listener.

The client also listen to server request and then show the server message to client.

My problem is some time any message send to any client machine without ip address? Please give any idea about how to send to client??

推荐答案

数据传输的方向与侦听和知道IP地址无关。 (此外,您可以创建两个频道:在一个频道中,您可以传递客户端IP,然后创建另一个频道,客户端侦听。但是,让我以正确的顺序告诉它...)



要建立沟通,你应该在一边听,在另一边连接。让我们称呼听侧服务器并连接侧客户端。您应该理解,同一个应用程序可以同时扮演服务器和客户端的角色,或者同时代表多个客户端或几个服务器或任何组合。 (只有传统的客户端 - 服务器模型不使用这些组合,但这是一个非常糟糕的简单模型,这是Web应用程序开发如此困难的原因之一;但我们不能在这里详细讨论它。)所有你需要的每个活动是一个单独的线程,通常每个监听器有两个线程(使用一个是常见错误),每个客户端一个。



听,你需要类 System.Net.Sockets.TcpListener ,要连接到监听应用程序,需要类 System.Net.Sockets.TcpClient

http:// msdn .microsoft.com / zh-cn / library / system.net.sockets.tcplistener.aspx [ ^ ],

http://msdn.microsoft.com/en-us/ library / system.net.sockets.tcpclient.aspx [ ^ ]。



即使在原始的客户端 - 服务器模型中,在服务器端,你也会听用于新连接(在单独的线程中),并使用另一个线程来读取/写入网络流。更确切地说,您可以接受套接字并获取表示远程(客户端)套接字的套接字实例,或接受表示连接的远程TCP客户端的 TcpClient 的实例。您可以在接受线程的循环中接受,以允许其他客户端连接。



您可以共享 TcpClient的实例 System.Net.Sockets.Socket (一般情况下,一些实例集合)与另一个执行读/写操作的线程。 (别忘了使用 互斥 ,例如,使用 lock 语句。)此线程以及客户端端的通信线程应实现一些隔行扫描读/写操作的协议。即使你不称它为协议,这样的事情总是存在的。这是一些应用程序层协议:

http://en.wikipedia。 org / wiki / Application_layer [ ^ ]。 />


正如您所看到的,即使您有一个频道,客户端和服务器也会按顺序读取和写入。



最后,让我回到倒置频道的想法。您可以开始聆听最初的客户。你创建了两个线程并开始监听,同时扮演服务器的角色。你已经拥有一个频道。其应用协议可以包括将IP地址(端口或其他任何内容)传递给服务的机制。 服务器部分可以扮演客户端的角色并连接到客户端。



另见我过去的答案:来自同一端口号的多个客户端 [ ^ ]。



-SA
The direction of data transfer has nothing to do with listening and knowing IP address. (Besides, you can create two channels: in one channel you can pass client IP and later create another channel, where a client listens. However, let me tell about it in proper order…)

To establish communication you should listen on one side and connect on another side. Let''s call listening side "server" and connecting side "client". You should understand though, that the same application can play a role of a server and a client at the same time, or represent several "clients" or several "servers", or any combination, at the same time. (Only a traditional "client-server" model does not use those combinations, but this is a very bad simple model, one of the reasons why Web application development is so difficult; but we cannot discuss it here in detail.) All you need is a separate thread for each activity, normally, two threads per listener (it''s a common mistake to use one) and one per client.

To listen, you need the class System.Net.Sockets.TcpListener, to connect to listening application, you need the class System.Net.Sockets.TcpClient:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx[^].

Even in the primitive "client-server" model, on the server side, you listen for new connection (in a separate thread), and use another thread to read/write from/to a network stream. More exactly, you can accept a socket and obtain the socket instance representing a remote (client) socket, or accept an instance of TcpClient, representing the connected remote TCP client. You can accept in a loop in your accepting thread, to allow other clients to connect.

You can share the instances of the TcpClient or System.Net.Sockets.Socket (in general case, some collection of instances) with another thread which does read/write operations. (Don''t forget to use mutual exclusion, for example, with lock statements.) This thread, as well as the communication thread on the "client" side, should implement some protocol of interlacing read/write operation. Even if you don''t call it a protocol, such thing always exists. This is some application layer protocol:
http://en.wikipedia.org/wiki/Application_layer[^].

As you can see, even if you have one channel, both "client" and "server" reads and writes in sequence.

And finally, let me get back to the idea of "inverted" channel. You can start listening on what was initially a "client". You create two more threads and start listening, playing the role of the "server" as well. You already have one channel. Its application protocol can include the mechanism of passing of the IP address (port or whatever else) to the "service". The "server" part can play the role of the client and connect to the "client".

See also my past answer: Multple clients from same port Number[^].

—SA


这篇关于如何使用C#中的TCP侦听器将消息从服​​务器发送到客户端而没有客户端ipaddress?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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