GVKun编程网logo

[Swift]LeetCode684. 冗余连接 | Redundant Connection(冗余连接是什么意思)

3

想了解[Swift]LeetCode684.冗余连接|RedundantConnection的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于冗余连接是什么意思的相关问题,此外,我们还将为您介

想了解[Swift]LeetCode684. 冗余连接 | Redundant Connection的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于冗余连接是什么意思的相关问题,此外,我们还将为您介绍关于"from pymongo import Connection" (to import mongo.connection using python) , got "500 Internal Server Error"、7.MQTT 网页客户端连接 MQTT 服务器的问题 WebSocket connection to ''ws://XXX:1883/'' failed: Connection closed...、ActiveMQ 错误 java.net.SocketException: Connection reset java.net.SocketException: Connection reset、C# 使用 FtpWebRequest 基础连接已经关闭:连接被意外关闭 (The underlying connection was closed:The connection was close...的新知识。

本文目录一览:

[Swift]LeetCode684. 冗余连接 | Redundant Connection(冗余连接是什么意思)

[Swift]LeetCode684. 冗余连接 | Redundant Connection(冗余连接是什么意思)

In this problem,a tree is an undirected graph that is connected and has no cycles.

The given input is a graph that started as a tree with N nodes (with distinct values 1,2,...,N),with one additional edge added. The added edge has two different vertices chosen from 1 to N,and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u,v] with u < v,that represents an undirected edge connecting nodes u and v.

Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers,return the answer that occurs last in the given 2D-array. The answer edge [u,v] should be in the same format,with u < v.

Example 1:

Input: [[1,2],[1,3],[2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:
  1
 / 2 - 3 

Example 2:

Input: [[1,[3,4],5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
    |   |
    4 - 3 

Note:

  • The size of the input 2D-array will be between 3 and 1000.
  • Every integer represented in the 2D-array will be between 1 and N,where N is the size of the input array.

Update (2017-09-26):
We have overhauled the problem description + test cases and specified clearly the graph is an undirectedgraph. For the directed graph follow up please see Redundant Connection II). We apologize for any inconvenience caused.

在本问题中,树指的是一个连通且无环的无向图。

输入一个图,该图由一个有着N个节点 (节点值不重复1,N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。

结果图是一个以组成的二维数组。每一个的元素是一对[u,v] ,满足 u < v,表示连接顶点u 和v的无向图的边。

返回一条可以删去的边,使得结果图是一个有着N个节点的树。如果有多个答案,则返回二维数组中最后出现的边。答案边 [u,v] 应满足相同的格式 u < v

示例 1:

输入: [[1,3]]
输出: [2,3]
解释: 给定的无向图为:
  1
 / 2 - 3

示例 2:

输入: [[1,5]]
输出: [1,4]
解释: 给定的无向图为:
5 - 1 - 2
    |   |
    4 - 3

注意:

  • 输入的二维数组大小在 3 到 1000。
  • 二维数组中的整数在1到N之间,其中N是输入数组的大小。

更新(2017-09-26):
我们已经重新检查了问题描述及测试用例,明确图是无向 图。对于有向图详见冗余连接II。对于造成任何不便,我们深感歉意。

Runtime: 28 ms
Memory Usage: 18.9 MB
 1 class Solution {
 2     func findRedundantConnection(_ edges: [[Int]]) -> [Int] {
 3         var root:[Int] = [Int](repeating:-1,count:2001)
 4         for edge in edges
 5         {
 6             var x:Int = find(&root,edge[0])
 7             var y:Int = find(&root,edge[1])
 8             if x == y {return edge}
 9             root[x] = y
10         }
11         return [Int]()
12     }
13     
14     func find (_ root:inout [Int],_ i:Int) -> Int
15     {
16         var i = i
17         while (root[i] != -1)
18         {
19             i = root[i]
20         }
21         return i
22     }
23 }

32ms

 1 class Solution {
 2     func findRedundantConnection(_ edges: [[Int]]) -> [Int] {
 3         guard edges.isEmpty == false else {
 4             return []
 5         }
 6         let n = edges.count
 7         var parents: [Int] = []
 8         for i in 0...n {
 9             parents.append(i)
10         }
11         for edge in edges {
12             let first = edge[0]
13             let second = edge[1]
14             let p1 = find(parents,first)
15             let p2 = find(parents,second)
16             if p1 == p2 {
17                 return edge
18             }
19             parents[p2] = p1
20         }
21         return []
22     }
23     
24     private func find(_ parents: [Int],_ val: Int) -> Int {
25         if parents[val] == val {
26             return val
27         }
28         return find(parents,parents[val])
29     }
30 }

48ms

 1 class Solution {
 2     // s1: union find
 3     func findRedundantConnection(_ edges: [[Int]]) -> [Int] {
 4         var uf = UnionFind(n: edges.count+1)
 5         for con in edges {
 6             let s = con[0]
 7             let e = con[1]
 8             if uf.union(s,e) == false {
 9                 return con
10             } 
11         }
12         return []
13     }
14 }
15 
16 class UnionFind {
17     public var parents = [Int]()
18     private var ranks = [Int]()
19     public var count: Int = 0
20     init(n: Int) {
21         for i in 0..<n {
22             parents.append(i)
23             ranks.append(1)
24         }
25     }
26     
27     func find(_ x: Int) -> Int {
28         var x = x
29         if parents[x] != x {
30             parents[x] = find(parents[x])
31         }
32         return parents[x]
33     }
34     /*
35     1 2 3
36     5 6
37     */
38     func union(_ x: Int,_ y: Int) -> Bool {
39         let px = find(x)
40         let py = find(y)
41         if px == py {
42             return false
43         }
44         count -= 1
45         if ranks[x] > ranks[y] {
46             parents[py] = px
47         } else if ranks[x] < ranks[y] {
48             parents[px] = py
49         } else {
50             parents[py] = px
51             ranks[px] += 1
52         }
53         return true
54     }
55 }

52ms

 1 class Solution {
 2     func findRedundantConnection(_ edges: [[Int]]) -> [Int] {
 3         guard edges.count > 0 else { return [0,0] }
 4 
 5         var totalNode = edges.count + 1
 6         
 7         var group: [Int] = []
 8         var groupLevel: [Int] = []
 9         
10         for i in 0..<totalNode {
11             group.append(i)
12             groupLevel.append(0)
13         }
14         
15         var extraEdge:[Int] = []
16         
17         for edge in edges {
18            var nodeX = edge[0]
19            var nodeY = edge[1]
20             
21            var pNodeX =  findParent(nodeX,&group)
22            var pNodeY =  findParent(nodeY,&group)
23            if pNodeX != pNodeY {
24                 if groupLevel[pNodeX] > groupLevel[pNodeY] {
25                     group[pNodeY] = pNodeX
26                 }else if groupLevel[pNodeX] < groupLevel[pNodeY] {
27                     group[pNodeX] = pNodeY
28                 }else {
29                     group[pNodeY] = pNodeX
30                     groupLevel[pNodeX] += 1
31                 }
32            }else {
33                extraEdge = edge
34            }
35         }
36         return extraEdge
37     }
38         
39     
40     func findParent(_ node: Int,_ group: inout [Int]) -> Int {
41         var currentNode = node
42         while currentNode != group[currentNode] {
43             group[currentNode] = group[group[currentNode]]
44             currentNode = group[currentNode]
45         }
46         
47         return currentNode
48     }
49 }

64ms

  1 class Solution {
  2     
  3     struct Edge {
  4         var w: Int
  5         var a: Int
  6         var b: Int
  7     }
  8     
  9     func findRedundantConnection(_ _edges: [[Int]]) -> [Int] {
 10         var wEdges = [Int: [(Int,Int)]]()
 11         for (i,edge) in _edges.enumerated() {
 12             wEdges[edge[0],default: []].append((edge[1],i))
 13             wEdges[edge[1],default: []].append((edge[0],i))
 14         }
 15         var safe: Set<Int> = []
 16         var heap = Heap<((Int,Int),Int)>(sort: {
 17             $0.1 < $1.1
 18         })
 19         let source = _edges[0][0]
 20         var edges = Set<[Int]>()
 21         safe.insert(source)
 22         for n in wEdges[source]! {
 23             heap.insert( ((source,n.0),n.1) )
 24         }
 25         while !heap.isEmpty {
 26             let ((source,node),_) = heap.remove()!
 27             safe.insert(node)
 28             edges.insert([source,node])
 29             edges.insert([node,source])
 30             for n in wEdges[node]! {
 31                 if edges.contains( [n.0,node] ) {
 32                     
 33                 } else if safe.contains(n.0) {
 34                     return [node,n.0].sorted()
 35                 } else {
 36                     heap.insert( ((node,n.1) )
 37                 }
 38             }
 39         }
 40         
 41         return _edges.last!
 42     }
 43 }
 44 
 45 
 46 
 47 
 48 
 49 public struct Heap<T> {
 50   
 51   /** The array that stores the heap‘s nodes. */
 52   var nodes = [T]()
 53   
 54   /**
 55    * Determines how to compare two nodes in the heap.
 56    * Use ‘>‘ for a max-heap or ‘<‘ for a min-heap, 57    * or provide a comparing method if the heap is made
 58    * of custom elements,for example tuples.
 59    */
 60   private var orderCriteria: (T,T) -> Bool
 61   
 62   /**
 63    * Creates an empty heap.
 64    * The sort function determines whether this is a min-heap or max-heap.
 65    * For comparable data types,> makes a max-heap,< makes a min-heap.
 66    */
 67   public init(sort: @escaping (T,T) -> Bool) {
 68     self.orderCriteria = sort
 69   }
 70   
 71   /**
 72    * Creates a heap from an array. The order of the array does not matter;
 73    * the elements are inserted into the heap in the order determined by the
 74    * sort function. For comparable data types,‘>‘ makes a max-heap, 75    * ‘<‘ makes a min-heap.
 76    */
 77   public init(array: [T],sort: @escaping (T,T) -> Bool) {
 78     self.orderCriteria = sort
 79     configureHeap(from: array)
 80   }
 81   
 82   /**
 83    * Configures the max-heap or min-heap from an array,in a bottom-up manner.
 84    * Performance: This runs pretty much in O(n).
 85    */
 86   private mutating func configureHeap(from array: [T]) {
 87     nodes = array
 88     for i in stride(from: (nodes.count/2-1),through: 0,by: -1) {
 89       shiftDown(i)
 90     }
 91   }
 92   
 93   public var isEmpty: Bool {
 94     return nodes.isEmpty
 95   }
 96   
 97   public var count: Int {
 98     return nodes.count
 99   }
100   
101   /**
102    * Returns the index of the parent of the element at index i.
103    * The element at index 0 is the root of the tree and has no parent.
104    */
105   @inline(__always) internal func parentIndex(ofIndex i: Int) -> Int {
106     return (i - 1) / 2
107   }
108   
109   /**
110    * Returns the index of the left child of the element at index i.
111    * Note that this index can be greater than the heap size,in which case
112    * there is no left child.
113    */
114   @inline(__always) internal func leftChildindex(ofIndex i: Int) -> Int {
115     return 2*i + 1
116   }
117   
118   /**
119    * Returns the index of the right child of the element at index i.
120    * Note that this index can be greater than the heap size,in which case
121    * there is no right child.
122    */
123   @inline(__always) internal func rightChildindex(ofIndex i: Int) -> Int {
124     return 2*i + 2
125   }
126   
127   /**
128    * Returns the maximum value in the heap (for a max-heap) or the minimum
129    * value (for a min-heap).
130    */
131   public func peek() -> T? {
132     return nodes.first
133   }
134   
135   /**
136    * Adds a new value to the heap. This reorders the heap so that the max-heap
137    * or min-heap property still holds. Performance: O(log n).
138    */
139   public mutating func insert(_ value: T) {
140     nodes.append(value)
141     shiftUp(nodes.count - 1)
142   }
143   
144   /**
145    * Adds a sequence of values to the heap. This reorders the heap so that
146    * the max-heap or min-heap property still holds. Performance: O(log n).
147    */
148   public mutating func insert<S: Sequence>(_ sequence: S) where S.Iterator.Element == T {
149     for value in sequence {
150       insert(value)
151     }
152   }
153   
154   /**
155    * Allows you to change an element. This reorders the heap so that
156    * the max-heap or min-heap property still holds.
157    */
158   public mutating func replace(index i: Int,value: T) {
159     guard i < nodes.count else { return }
160     
161     remove(at: i)
162     insert(value)
163   }
164   
165   /**
166    * Removes the root node from the heap. For a max-heap,this is the maximum
167    * value; for a min-heap it is the minimum value. Performance: O(log n).
168    */
169   @discardableResult public mutating func remove() -> T? {
170     guard !nodes.isEmpty else { return nil }
171     
172     if nodes.count == 1 {
173       return nodes.removeLast()
174     } else {
175       // Use the last node to replace the first one,then fix the heap by
176       // shifting this new first node into its proper position.
177       let value = nodes[0]
178       nodes[0] = nodes.removeLast()
179       shiftDown(0)
180       return value
181     }
182   }
183   
184   /**
185    * Removes an arbitrary node from the heap. Performance: O(log n).
186    * Note that you need to kNow the node‘s index.
187    */
188   @discardableResult public mutating func remove(at index: Int) -> T? {
189     guard index < nodes.count else { return nil }
190     
191     let size = nodes.count - 1
192     if index != size {
193       nodes.swapAt(index,size)
194       shiftDown(from: index,until: size)
195       shiftUp(index)
196     }
197     return nodes.removeLast()
198   }
199   
200   /**
201    * Takes a child node and looks at its parents; if a parent is not larger
202    * (max-heap) or not smaller (min-heap) than the child,we exchange them.
203    */
204   internal mutating func shiftUp(_ index: Int) {
205     var childindex = index
206     let child = nodes[childindex]
207     var parentIndex = self.parentIndex(ofIndex: childindex)
208     
209     while childindex > 0 && orderCriteria(child,nodes[parentIndex]) {
210       nodes[childindex] = nodes[parentIndex]
211       childindex = parentIndex
212       parentIndex = self.parentIndex(ofIndex: childindex)
213     }
214     
215     nodes[childindex] = child
216   }
217   
218   /**
219    * Looks at a parent node and makes sure it is still larger (max-heap) or
220    * smaller (min-heap) than its childeren.
221    */
222   internal mutating func shiftDown(from index: Int,until endindex: Int) {
223     let leftChildindex = self.leftChildindex(ofIndex: index)
224     let rightChildindex = leftChildindex + 1
225     
226     // figure out which comes first if we order them by the sort function:
227     // the parent,the left child,or the right child. If the parent comes
228     // first,we‘re done. If not,that element is out-of-place and we make
229     // it "float down" the tree until the heap property is restored.
230     var first = index
231     if leftChildindex < endindex && orderCriteria(nodes[leftChildindex],nodes[first]) {
232       first = leftChildindex
233     }
234     if rightChildindex < endindex && orderCriteria(nodes[rightChildindex],nodes[first]) {
235       first = rightChildindex
236     }
237     if first == index { return }
238     
239     nodes.swapAt(index,first)
240     shiftDown(from: first,until: endindex)
241   }
242   
243   internal mutating func shiftDown(_ index: Int) {
244     shiftDown(from: index,until: nodes.count)
245   }
246   
247 }
248 
249 // MARK: - Searching
250 extension Heap where T: Equatable {
251   
252   /** Get the index of a node in the heap. Performance: O(n). */
253   public func index(of node: T) -> Int? {
254     return nodes.index(where: { $0 == node })
255   }
256   
257   /** Removes the first occurrence of a node from the heap. Performance: O(n log n). */
258   @discardableResult public mutating func remove(node: T) -> T? {
259     if let index = index(of: node) {
260       return remove(at: index)
261     }
262     return nil
263   }  
264 }

88ms

 1 class Solution {
 2 
 3     func findRedundantConnection(_ edges: [[Int]]) -> [Int] {
 4         var N = 0
 5         var graph = [Int: Set<Int>]()
 6         for edge in edges {
 7             graph[edge[0],default: []].insert(edge[1])
 8             graph[edge[1],default: []].insert(edge[0])
 9             N = max(N,edge[0],edge[1])
10         }
11         let source = edges[0][0]
12         for edge in edges.reversed() {
13             if isConnected(graph,edge,source,N) {
14                 return edge
15             }
16         }
17         return edges.last!
18     }
19     
20     func isConnected(_ graph: [Int: Set<Int>],_ edge: [Int],_ source: Int,_ N: Int) -> Bool {
21         var graph = graph
22         graph[edge[0]]!.remove(edge[1])
23         graph[edge[1]]!.remove(edge[0])
24         var stack = [Int]()
25         var visited = Set<Int>()
26         stack.append(source)
27         while !stack.isEmpty {
28             let node = stack.popLast()!
29             visited.insert(node)
30             for edge in graph[node] ?? [] {
31                 if !visited.contains(edge) {
32                     stack.append(edge)
33                 }
34             }
35         }
36         
37         return visited.count == N
38     }
39 }

112ms

 1 class Solution {
 2     
 3     let MAX_EDGE_VAL = 1000
 4     
 5     func findRedundantConnection(_ edges: [[Int]]) -> [Int] {
 6         var graph = [Int: [Int]]()
 7         
 8         for edge in edges {
 9             let u = edge[0]
10             let v = edge[1]
11             var visited = Set<Int>()
12             if hasPath(&graph,&visited,u,v) {
13                 return [u,v]
14             }
15             graph[u] = graph[u] ?? [Int]()
16             graph[u]!.append(v)
17             graph[v] = graph[v] ?? [Int]()
18             graph[v]!.append(u)
19         }
20         return [-1,-1]
21     }
22     
23     public func hasPath(_ graph: inout [Int: [Int]],_ visited: inout Set<Int>,_ target: Int) -> Bool {
24         if source == target {
25             return true
26         }
27         if !graph.keys.contains(source) || !graph.keys.contains(target) {
28             return false
29         }
30         visited.insert(source)
31         if let neighbers = graph[source] {
32             for neighber in neighbers {
33                 if visited.contains(neighber) {
34                     continue
35                 }
36                 if hasPath(&graph,neighber,target) {
37                     return true
38                 }
39             }   
40         }
41         return false
42     }
43 }

"from pymongo import Connection" (to import mongo.connection using python) , got "500 Internal Server Error"

I created the /var/www/cgi-bin/test.py in my web server machine (Linux). The service of mongoDB "mongod" is running.

When I run the test.py locally, "./test.py", I could get the correct result.

But when I use the URL: http://xxxxxx.compute-1.amazonaws.com/cgi-bin/python_test/test.py to run the test.py, I will get the "500 Internal Server Error". 

If I remove the line "from pymongo import Connection", and use the URL above to access, I won''t get the 500 Internal server error. So, it should be the problem when I import the pymongo.connection.

Is it the problem of any configuration issue? Thanks so much for your help

========== test.py ==============

#!/usr/bin/python
import cgi
import sys
sys.path.append("/home/ec2-user/mongo-python-driver-2.6.3/")

#import pymongo.connection
from pymongo import Connection
def main():
        print "Content-type: text/html\r\n\r\n"
        form = cgi.FieldStorage()
        name = form.getvalue(''ServiceCode'')
#       name = "abcddd"
#       con = Connection()
#       db = con.test
#       posts = db.post

#       print name
        if form.has_key("ServiceCode") and form["ServiceCode"].value!="":
                print"<h1>Hello", form["ServiceCode"].value,"</h1>"
        else:
                print"<h1>Error! Please enter first name. </h1>"

 .....


7.MQTT 网页客户端连接 MQTT 服务器的问题 WebSocket connection to ''ws://XXX:1883/'' failed: Connection closed...

7.MQTT 网页客户端连接 MQTT 服务器的问题 WebSocket connection to ''ws://XXX:1883/'' failed: Connection closed...

问题描述:
MQTT.js 提供了连接 MQTT 的一套 javascipt 的方法,可供前端连接到 MQTT 服务器,也可以作为脚本测试。
以脚本形式,用 nodejs 运行,是没有问题的,能够正常连接并且发送报文。
但是如果把 js 代码放到 HTML 文件中,就不能正常完成连接,提示:
客户端提示:

 

服务器提示:

 

问题解决;
根据客户端提示,是无法完成握手连接,根据服务器提示,是因为解包的时候,包不符合格式,导致了连接拒绝。
通过查阅文献,发现普通的 socket 和 websocket 是不一样的。
直接运行脚本使用的是 socket.io 而 浏览器使用的是 websocket,而处理这两种报文是不一样的。
所以猜测是 MQTT 并没有打开 websocket 支持,而是把过来的包当做普通的 socket 包处理了。
查阅代码发现:
我服务器使用的是 moquette,里面的 nettyAcceptor 已经明确说明了 TCP 和 websocket 的不同处理,可以看到代码中 websocket 是关闭的:

private void initializePlainTCPTransport(final NettyMQTTHandler handler, IConfig props) throws IOException {
final MoquetteIdleTimeoutHandler timeoutHandler = new MoquetteIdleTimeoutHandler();
String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
String tcpPortProp = props.getProperty(PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
if (DISABLED_PORT_BIND.equals(tcpPortProp)) {
LOG.info("tcp MQTT is disabled because the value for the property with key {}", BrokerConstants.PORT_PROPERTY_NAME);
return;
}
int port = Integer.parseInt(tcpPortProp);
initFactory(host, port, new PipelineInitializer() {
@Override
void init(ChannelPipeline pipeline) {
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
// pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
pipeline.addLast("decoder", new MQTTDecoder());
pipeline.addLast("encoder", new MQTTEncoder());
pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
// pipeline.addLast("messageLogger", new MQTTMessageLogger());
pipeline.addLast("handler", handler);
}
});
}

private void initializeWebSocketTransport(final NettyMQTTHandler handler, IConfig props) throws IOException {
String webSocketPortProp = props.getProperty(WEB_SOCKET_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
if (DISABLED_PORT_BIND.equals(webSocketPortProp)) {
//Do nothing no WebSocket configured
LOG.info("WebSocket is disabled");
return;
}
else{
LOG.info("WebSocket is enable");
}
int port = Integer.valueOf(webSocketPortProp);

final MoquetteIdleTimeoutHandler timeoutHandler = new MoquetteIdleTimeoutHandler();

String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
initFactory(host, port, new PipelineInitializer() {
@Override
void init(ChannelPipeline pipeline) {
pipeline.addLast("httpEncoder", new HttpResponseEncoder());
pipeline.addLast("httpDecoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST));
pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
pipeline.addLast("decoder", new MQTTDecoder());
pipeline.addLast("encoder", new MQTTEncoder());
pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler);
}
});
}

 

解决方法:
设置一个端口,我设为 1885,将 websocket 服务打开就可以了。

String webSocketPortProp = props.getProperty(WEB_SOCKET_PORT_PROPERTY_NAME);
        if (DISABLED_PORT_BIND.equals(webSocketPortProp)) {
            //Do nothing no WebSocket configured
            LOG.info("WebSocket is disabled");
            return;
        }
        else{
            LOG.info("WebSocket is enable");
        }
        int port = Integer.valueOf(webSocketPortProp);

 

ActiveMQ 错误 java.net.SocketException: Connection reset java.net.SocketException: Connection reset

ActiveMQ 错误 java.net.SocketException: Connection reset java.net.SocketException: Connection reset

最近在研究 ActiveMQ 控制台报 错误 java.net.SocketException: Connection reset   java.net.SocketException: Connection reset, 客户端无法发送消息,客户端是使用Spring结合来做的,请问这个问题何解决?

C# 使用 FtpWebRequest 基础连接已经关闭:连接被意外关闭 (The underlying connection was closed:The connection was close...

C# 使用 FtpWebRequest 基础连接已经关闭:连接被意外关闭 (The underlying connection was closed:The connection was close...

 公司内部开发的 winform 程序使用了 FtpWebRequest 下载 FTP 服务器的文件到本地。

大多数人运行良好,由于我们是试运行逐步有人加入到平台的使用,前两天突然有个别机器无法连接 FTP 服务器报出了如下错误。

The underlying connection was closed:The connection was closed unexpectedly

 

 

 进行排查没有发现异常,windows 事件管理器中查看也没有相关的错误。写的代码也没有发现什么不对的地方

FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                reqFtp.UseBinary = true;
                reqFtp.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);
                FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                FileStream outputStream = new FileStream(newFileName, FileMode.Create);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();

  

一时之间比较懵逼,同时网上搜索 FtpWebRequest "基础连接被关闭,连接被意外关闭" 的异常更是少之又少。估计这年头大家都不怎么开发桌面程序,所以用到这个类的出现问题的更少。

但是说实话写了 winform 程序的人或许对多线程,事件委托,网络请求,Windows 服务等技术会有更深的理解。

无奈没有现成的解决方案,加上 99% 的人运行程序正常,最后怀疑是机器环境问题。由于工作比较忙就直接喊运维给他重新装了系统(这个方法不推荐,我们情况比较特殊)。

通过重装后就能正常下载文件了,但是又过了 2 天,这位同学的机器又无法下载了 (这部分同学对电脑不是很熟悉,解压软件有时候都不知道装)。不知道是操作了什么还是杀毒软件误杀。

最后本着不能让别人再装系统吧,得找一下是不是自己程序得问题。

测试 + 搜索依然毫无进展,然后我就去官方文档看了下,直到我发现这个东西:

 

 

 然后进入 GitHub

stack overflow 上面的关于这个第三方库的问答。

 

 

 

 

 

 最后重新改写下载代码,使用 nuget 添加 FluentFTP 类库

using (FtpClient conn = new FtpClient())
    {
        conn.Host = uri.Host;
        conn.Port = uri.Port;
        conn.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);
        byte[] outBuffs;
        bool flag = conn.Download(out outBuffs, uri.AbsolutePath);

        FileStream fs = new FileStream(newFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        fs.Write(outBuffs, 0, outBuffs.Length);
        //清空缓冲区、关闭流
        fs.Flush();
        fs.Close();
    }

  最后更新程序后一切都风平浪静了!

其实这个问题不应该是 FtpWebRequest 的问题,因为这个类是依赖.net Framework 的,应该是部分机器环境问题导致可能出现未知的网络问题。换用 FluentFTP 只是换了一个第三方库,在打包程序的时候被一起打包了。所以以后再操作这种网络请求的时候还是尽量用第三方库吧,毕竟每个机器环境我们无法排查(程序员会修电脑?)。

最后说下 HttpWebRequest 也存在同样的各种意想不到的问题,建议大家用 RestSharp 这个库。其实通篇下来并没有找到具体是什么导致 FtpWebRequest 连接不到服务器,只是换了一种方式也算是一种解决办法,没必要在一个点上出不来。

出于网上对这个问题并没有怎么提及,特意记录下来。如果有道友知道确切出现问题的点还请留下你的经验。

 

原文出处:https://www.cnblogs.com/SunSpring/p/12100427.html

我们今天的关于[Swift]LeetCode684. 冗余连接 | Redundant Connection冗余连接是什么意思的分享已经告一段落,感谢您的关注,如果您想了解更多关于"from pymongo import Connection" (to import mongo.connection using python) , got "500 Internal Server Error"、7.MQTT 网页客户端连接 MQTT 服务器的问题 WebSocket connection to ''ws://XXX:1883/'' failed: Connection closed...、ActiveMQ 错误 java.net.SocketException: Connection reset java.net.SocketException: Connection reset、C# 使用 FtpWebRequest 基础连接已经关闭:连接被意外关闭 (The underlying connection was closed:The connection was close...的相关信息,请在本站查询。

本文标签: