如何知道Java Socket已经死了(how to know a Java Socket is dead)
我在客户端应用程序中使用Java Socket对象。 我需要知道服务器的线路何时断开,或者是否有任何事件导致套接字死机。
我看到两种方法:
- 在写入或读取套接字时捕获SocketException,考虑到这些异常会终止套接字
- 捕获这些异常时,检查Socket.isClosed()方法以确定它是否杀死了套接字
这些方法中的任何一种都能保证我知道套接字何时死机并且不会再次工作,即使线路上的临时问题已经解决了吗? 我们可以除了在套接字上的操作期间抛出的(Socket)异常暗示套接字已经死了吗?
知道这个有更好的方法吗?
I use a Java Socket object in my client application. I need to know when the line to the server is broken, or if any event caused the socket to be dead.
I see two methods:
- catching SocketException when writing in or reading from the socket, considering these exceptions kill the socket
- when catching these exceptions, checking the Socket.isClosed() method to know if it killed the socket
Does any of these methods guarantee I'll know when the socket is dead and won't work again, even if a temporary problem on the line is solved? Can we except a (Socket)Exception thrown during an operation on a socket to imply the socket is dead?
Is there a better method to know this?
原文:https://stackoverflow.com/questions/2055650
最满意答案
您的代码中有2个粘贴操作。 你知道的一个:
WshtCrnt.Paste
以及属于此范围复制声明的一部分:
。 。 使用WshtSht1 .Range(.Cells(1,1),。Cell(RowSht1DataFirst - 1,ColSht1LastHdr))。Copy_ Destination:= WshtCrnt.Range(“A1”)。 。
通过指定“目的地”,您要求复制并粘贴您的范围。
You have 2 paste operations in your code. One that you know of:
WshtCrnt.Paste
and one that is part of this range copy statement:
. . With WshtSht1 .Range(.Cells(1, 1), .Cells(RowSht1DataFirst - 1, ColSht1LastHdr)).Copy_ Destination:=WshtCrnt.Range("A1") . .
By specifying a "Destination" you are requesting a copy AND paste of your range.
相关问答
更多-
尝试这个: For Each mycell In MyRange Sheets("Template").Copy After:=Sheets(Sheets.Count) With ActiveSheet .Name = mycell .Range("A1").Value = mycell.value .Range("E1").Value = mycell.Offset(0, 1).Value End With mycell.Pa ...
-
前段时间我一直在寻找同样问题的解决方案。 没有找到解决方案,但我找到了一个很好的解决方法: 制作一个单独的表格,里面有很多照片。 将其命名为user_form_pics 。 然后在表单上调用以下内容: Me.Image1.Picture = user_form_pics.img_name11.Picture 这是在构造函数中使用它的方法: Private Sub UserForm_Initialize() Me.Image1.Picture = user_form_pics.img_name11.P ...
-
您需要以下代码。 如果您可以将其合并到工作表中,请查看并告诉我们: Sub SendToSheet2() Dim wsIn As Worksheet Dim wsOut As Worksheet Dim rngToCopy As Range Set wsIn = Worksheets("CSVin") Set wsOut = Worksheets("CSVOut") Set rngToCopy = wsIn.Range("A1").CurrentRegion ...
-
您的代码中有2个粘贴操作。 你知道的一个: WshtCrnt.Paste 以及属于此范围复制声明的一部分: 。 。 使用WshtSht1 .Range(.Cells(1,1),。Cell(RowSht1DataFirst - 1,ColSht1LastHdr))。Copy_ Destination:= WshtCrnt.Range(“A1”)。 。 通过指定“目的地”,您要求复制并粘贴您的范围。 You have 2 paste operations in your code. One that you k ...
-
创建完所有工作表后,您只需搜索列表以搜索城市。 对于每一行,请查看城市,并将其写入相应的表格中。 工作表需要与我的代码工作的城市具有相同的名称。 我假设你从A列第1行开始。 dim strCity as string dim strAdd as string dim strName as string for i = 1 to Sheets("[TableSheet]").Cells(Rows.Count, "A").End(xlUp).row strCity = Sheets("[TableSh ...
-
用一个列表框和一个按钮来构建一个表单。 然后将这些代码放到表单模块中。 该按钮应该具有名称CommandButton1,并且该列表框应该被命名为ListBox1。 最后在列表框控件上将columncount更改为15。 Private Sub CommandButton1_Click() Unload Me End Sub Private Sub UserForm_Initialize() ' Activate the sheet that has the data you want d ...
-
VBA:根据单元格的值将工作表1中的单元格复制到工作表2(VBA: Copy cell in worksheet1 to worksheet2 based on value of cell)[2022-03-25]
您只需要快速修改即可将行复制到单个单元格和位置。 我用过你从c行的AG栏复制。 Sub Output() Dim c As Range Dim j As Integer Dim Source As Worksheet Dim Target As Worksheet Set Source = ActiveWorkbook.Worksheets("workshee1") Set Target = ActiveWorkbook.Worksheets("workshee2") j = 1 ' Start ... -
我正在做类似的事情,这是我的子目的: 我用|打开一个txt文件 作为分隔符。 然后将工作表的内容复制到我的目标工作簿(全局变量)。 然后我关闭包含原始txt文件的第一个工作簿而不保存。 Workbooks.OpenText的代码基本上是通过录制宏并根据我的需要进行调整。 Sub ImportTextFile(path As String) Dim SheetName As String Dim TMPWorkBook As Workbook Dim FilePath As Strin ...
-
您可以为目标行设置变量,如下所示: Sub CopyRow() Application.ScreenUpdating = False Dim x As Long Dim MaxRowList As Long Dim S As String Dim wsSource As Worksheet Dim wsTarget As Worksheet Set wsSource = ThisWorkbook.Worksheets("Tab 1") Set wsTarget = ThisWorkbook.Work ...
-
将行从工作表复制到新工作表Excel VBA的结尾(Copying Rows from Worksheet to End of New Worksheet Excel VBA)[2022-07-13]
继我的评论之后,请看这个 UNTESTED Option Explicit Sub sample() Dim wsO As Worksheet, wsI As Worksheet Dim wsOLRow As Long, wsILRow As Long Set wsO = ThisWorkbook.Worksheets("NEW WS") Set wsI = ThisWorkbook.Worksheets("Original") With wsO ...