/// <summary> /// Connects to the end point (node) and sends message to it. If waitForReply is set /// it will wait for a reply from the node and return the reply. It uses Asynchronous UDP /// protocol /// </summary> /// <param name="node">The end point (node) in the network to communicate with</param> /// <param name="message">Message to send to node</param> /// <param name="waitForReply">Whether a reply is expected</param> /// <returns></returns> public static byte[] communicateAsync(IPEndPoint node, byte[] message, bool waitForReply) { Connection nodeConnection = null; UdpClient udpClient = null; byte[] reply = null; try { //connect to node nodeConnection = new Connection(node); udpClient = nodeConnection.connect_async(); if(udpClient == null) { nodeConnection.disconnect_async(); return null; } udpClient.Send(message, message.Length); System.Net.IPEndPoint senderEndPoint = new System.Net.IPEndPoint(0,0); if(waitForReply) reply = udpClient.Receive(ref senderEndPoint); } catch (Exception e) { return null; } finally { nodeConnection.disconnect_async(); } if(reply != null && reply.Length == 0) return null; return reply; }
public static NetworkStream OpenCommunicationChannel(IPEndPoint node) { Connection nodeConnection = null; NetworkStream nodeStream = null; try { //connect to node nodeConnection = new Connection(node); nodeStream = nodeConnection.connect_sync(); if(nodeStream == null) { nodeConnection.disconnect_sync(); return null; } } catch (Exception e) { int x = 2; if(nodeConnection != null) nodeConnection.disconnect_sync(); } return nodeStream; }