我有一个始终侦听端口 6002 的插件,并且我有一个 ASP.net 应用程序,它将消息发送到同一端口并从同一端口上的插件接收回复,
发送工作正常,并且插件在同一端口上发送回复,但当我尝试使用 Tcplistener start 方法抛出此异常:每个套接字地址(协议/网络地址/端口)只能使用一次)通常是允许的,
有什么办法可以捕获收到的消息
谢谢
I've a plugin which always listening to the port 6002, and i have an ASP.net application which sending messages to the same port and receiving the reply from the plugin on the same port,
Sending is working fine, and the plugin sends a reply on the same port but i don't know how to catch this reply, when i try to listen to the same port using Tcplistener the start method throws this exception : Only one usage of each socket address (protocol/network address/port) is normally permitted,
is there any way to catch the received message
Thanks
发布评论
评论(4)
听起来您错误地假设您从 Socket 获得的 < a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.acceptsocket.aspx" rel="nofollow">TcpListener.AcceptSocket 只能在一个方向使用。
套接字实际上可以是双向的。您可以使用发送发送内容,然后接收以
收听获取回复。打开一个套接字,然后将其用于发送和接收。It sounds like you are wrongly assuming that the Socket which you get from TcpListener.AcceptSocket can only be used in one direction.
Sockets can actually be bidirectional. You can use Send to send something, and Receive to
listen forget the replies. Open one socket, and then use it for both sending and receiving.简而言之,不。
一旦端口打开,如果进一步尝试使用来自不同源的同一端口,就会抛出异常 - 正如您现在所经历的那样。没有办法解决这个问题。
In short, no.
Once a port is opened an exception will be thrown if further attempts are made to utilise that same port from a different source - as you are experiencing right now. There isn't a way to get around this.
我已经用这种方式解决了这个问题,我知道这是旧方法,但它有效! :
I've solved this problem using this way ,, I know it's old method but it's working !! :
如果您想检查流量,可以使用 winpcap。编辑:我认为您没有问正确的问题。在这种情况下,插件是服务器(侦听端口 6002),而 ASP.net 应用程序是侦听某个任意端口的客户端。如果 ASP.net 应用程序还需要作为服务器运行且插件充当客户端,则只需绑定到 ASP.net 应用程序中的不同端口。在这种情况下,您应该选择不同的端口,即使事实上,当它们都绑定到同一端口时,有多种方法可以使其工作。
在您的情况下,您应该只读回从客户端建立的连接的响应。
If you want to inspect traffic you can use winpcap.edit: I don't think you are asking the right question. In this case the plugin is the server (listening on port 6002) and your ASP.net app is the client listening on some arbitrary port. You only need to bind to a different port in your ASP.net app if it also needs to run as a server with the plugin acting s the client. In this case you should pick a different port even though there are, in fact, ways to make it work when they are both bound to the same port.
In your case though you should just read back responses from the connection you established from the client.