套接字:在C#中部署网络资源 [英] Socket: dispose network resources in C#

查看:125
本文介绍了套接字:在C#中部署网络资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我编写了一个客户端/服务器程序,当客户端程序尝试关闭时,服务器程序将识别客户端的此操作,并尝试将所有分配的资源分配给该套接字连接,并开始在同一端口上重新监听

我这样做是为了处理网络(不知道这样做对不对)

hey

i wrote a client/server program, when client program try close the server program will recognize this action of client side, and try to dispose all assigned resources to that socket connection, an begin to re-listen on the same port

i did this to dispose network(dont know it is right to do or not)

<code>con.CurrentConnectionSocket.Close();
((System.IDisposable)con.CurrentConnectionSocket).Dispose();
</code>


但是当我尝试重新收听时,使用此异常消息捕获"SocketException"

Only one usage of each socket address (protocol/network address/port) is normally permitted

我该如何处理?

我需要尽快的帮助

提前tnx


BUT when i try to re-listen, catch a ''SocketException'' with this exception message

Only one usage of each socket address (protocol/network address/port) is normally permitted

how do i handle this?

i need help ASAP

tnx in advance

推荐答案

您不能重用已处置或已关闭的套接字.如果这样做,则实例化Socket 类的新实例.或者,如果您想重用它,请不要调用Close (btw内部会调用Dispose ,因此无论您何时调用Close ,您都无需调用Dispose ).而是在套接字实例上调用Disconnect(true).

[更新]
----------
在回答有关重新连接的评论时,请执行以下操作:进行初始连接(实例化套接字对象除外).
You cannot reuse a disposed or closed socket. If you do that, instantiate a new instance of the Socket class. Or if you want to reuse it, do not call Close (which btw calls Dispose internally - so you don''t need to call Dispose once you call Close anyway). Instead call Disconnect(true) on the socket instance.

[Update]
----------
In answer to your comment on reconnecting: Just do everything that you do to make the initial connection (other than instantiating the socket object).


tnx,但是我该如何重新连接呢?我仍然抓到那个异常:(
tnx, but how do i re-connect then? i still catch that exception :(


为一个简单的重新连接代码而发疯〜X(

我这样做,但是仍然有问题[在断开插座后]
getting crazy for a simple reconnection code~X(

i do this but still have that problem[after i disconnect the socket]
<br />
<pre><br />
CurrentConnectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<br />
CurrentConnectionSocket.Bind(ConnetionIPEP);<br />
CurrentConnectionSocket.Listen(1);<br />
ConnectionStatusString = "Waitting for client connection  ....";<br />
CurrentConnectionSocket.NoDelay = true;<br />
StaticConnectionInfo.ConnectionRootMode = ConnectionMode.Listenning;<br />
CurrentConnectionSocket.BeginAccept(new AsyncCallback(AcceptCon_SYNC), CurrentConnectionSocket);<br />
StaticConnectionInfo.NetworkSideOption = Setting.SettingSideOption.Server;<br />
StaticConnectionInfo.ServerConnectionRoot = this;</pre><br />


这篇关于套接字:在C#中部署网络资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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