Socket.Close()........................... [英] Socket.Close()........................

查看:114
本文介绍了Socket.Close()...........................的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,据我所知,在C#中,您默认传递了引用的副本,
对于我的示例,否

iv'e在第一个套接字连接中得到2个表单女巫,然后在承包商中传递给另一种表单

现在,据我了解,它们都引用了内存中的同一位置,
现在以第二种形式调用Socket.Close()
当第二种形式关闭时,第一种形式向上

和套接字女巫被使用,当然设置为关闭,我想检测套接字的状态
类似于if(Socket.Closed)

这是因为在某些情况下,我会调用Socket.Disconnect(true),并且在发生错误时会调用Socket.Close()

我的问题是:
1)如何检测插座是否关闭且未在其断开的那一侧断开连接,
(请尝试理解,我可以说是一个oledb连接,套接字不是
就像检测是否关闭了可关闭对象一样重要)
2)可以将Socket对象的引用设置为null,然后另一个Socket也可以引用null,并且我可以检测到

hello, as i understand it in C# u pass a copy of the reference by default,
no for my Example

iv''e got 2 Forms witch in the first a Socket connects and then is being passed in the constractor to the other form

now as i understand it they both have a reference to the same place in memory ,
now in the second form i call Socket.Close()
when the second form closes , the first form is up

and the socket witch is used there was of course set to close, i want to detect that status of the socket
something like if(Socket.Closed)

this is because in some situations i Call Socket.Disconnect(true) , and when errors occur i call Socket.Close()

my questions are :
1) how to detect if a socket is closed and not disconnected on The side it was closed on,
(please try to understand that i could well of said a oledb connection,the socket is not
the issue as much as detecting if a closeable object was closed)
2) can the reference of the Socket object be set to null , then the other Socket would also reference null and that i could detect

推荐答案

这要容易得多:否.
当您将引用传递给第二种形式时,会将引用存储在第二种形式的变量中.将其设置为null将不会对变量产生任何影响,该变量也会以第一种形式存储实例.就像电话号码一样:我从通讯录中读取了一个号码给您,然后您将其写下来.如果我随后将书中的页面撕开并刻录,则不会影响您所用的页面:您仍然拥有该编号.

第一个问题:AFAIK不能告诉套接字是否已关闭,只能尝试从中读取并查看它是否立即返回.
相反,我将从Socket派生一个类并传递该类,包括"IsOpen"属性,您可以设置该属性以确定关闭它的时间.

The second question first, as it is a lot easier: No.
When you pass the reference to the second form, you store the reference in a variable in the second form. Setting that to null will have no effect on the variable also storing the instance in the first form. It''s like a phone number: I read a number from my address book to you, and you write it down in yours. If I then rip up the page in my book and burn it, it does not affect the page in yours: you still have the number.

The first question: there is no way AFAIK to tell if a socket has been closed, other than to try to read from it and see if it returns at once.
Instead, I would derive a class from Socket and pass that instead, including a "IsOpen" property which you can set to determine when you closed it.

public class MySocket : Socket
    {
    public bool IsOpen { get; set; }
    public MySocket()
        {
        IsOpen = false;
        }
    }




对不起-忘记回答第一个问题. :-O-OriginalGriff [/edit]




[edit]Sorry - forgot to answer the first question. :-O - OriginalGriff[/edit]


您好,

套接字连接和关闭我自己发现了一个问题.您可以检测套接字是否被Socket.IsConnected正常关闭.尽管连接另一端的套接字上的连接识别出连接已结束,但这并没有保证.另外,我注意到在关闭连接时,在关闭连接之前,正在从关闭套接字发送了一个最终为空的最终消息.这是确定的,并且是一种确定连接何时结束的相当可靠的方式.

如果您想连接到服务/程序,我写了几篇文章(和类库),它们将完全满足您的要求:
网络游戏的类和功能
快速联网库2

但是,如果您按照建议的方式使用oledb连接,则这对您不起作用,但可能会为您提供良好的理解基础.

希望这会有所帮助,

埃德:)
Hi there,

Socket connections and closing I have found a problem myself. You can detect if the socket was closed by Socket.IsConnected normally. This does not garuntee though that the connection on the socket on the other side of the connection recognises the connection has ended. Also, I noted that when closing a connection a final, completely empty, message was sent from the closing socket just before it closes. This is detactable and a fairly reliable way of knowing when the connection has ended.

If your looking to connect to services/programs I wrote a couple of articles (and class libraries) that will do exactly what you want:
Network gaming classes and functions
Fast Networking Library 2

However, if your going to use an oledb connection, as you seem to suggest, then this owuld not work for you, but may give a good base of understanding.

Hope this helps,

Ed :)


这篇关于Socket.Close()...........................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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