static string GetMSSqlPort(string instanceName, string dataSource, int timeout) { string port = String.Empty; try { DatagramSocket socket = new DatagramSocket(); // send request sbyte[] buf = new sbyte[] {2}; InetAddress address = InetAddress.getByName(dataSource); DatagramPacket packet = new DatagramPacket(buf, buf.Length, address, 1434); socket.send(packet); sbyte[] recbuf = new sbyte[1024]; packet = new DatagramPacket(recbuf, recbuf.Length, packet.getAddress(), packet.getPort()); // try to receive from socket while increasing timeouts in geometric progression int iterationTimeout = 1; int totalTimeout = 0; for(;;) { socket.setSoTimeout(iterationTimeout); try { socket.receive(packet); break; } catch (SocketTimeoutException e) { totalTimeout += iterationTimeout; iterationTimeout *= 2; if (totalTimeout >= timeout*1000) { throw new java.sql.SQLException( String.Format ("Unable to retrieve the port number for {0} using UDP on port 1434. Please see your network administrator to solve this problem or add the port number of your SQL server instance to your connection string (i.e. port=1433).", dataSource) ); } } } sbyte[] rcvdSbytes = packet.getData(); char[] rcvdChars = new char[rcvdSbytes.Length]; for(int i=0; i < rcvdSbytes.Length; i++) { rcvdChars[i] = (char)rcvdSbytes[i]; } String received = new String(rcvdChars); java.util.StringTokenizer st = new java.util.StringTokenizer(received, ";"); String prev = ""; bool instanceReached = instanceName == null || instanceName.Length == 0; while (st.hasMoreTokens()) { if (!instanceReached) { if (prev.Trim().Equals("InstanceName")) { if (String.Compare(instanceName,st.nextToken().Trim(),true, CultureInfo.InvariantCulture) == 0) { instanceReached = true; } } } else { if (prev.Trim().Equals("tcp")) { port = st.nextToken().Trim(); //ensure we got a valid int java.lang.Integer.parseInt(port); break; } } prev = st.nextToken(); } socket.close(); if (!instanceReached) throw new java.sql.SQLException( String.Format ("Specified SQL Server '{0}\\{1}' not found.", dataSource, instanceName) ); return port; } catch (java.sql.SQLException) { throw; } catch (Exception e) { throw new java.sql.SQLException(e.Message); } }
protected override void onCreate(Bundle savedInstanceState) { base.onCreate(savedInstanceState); var sv = new ScrollView(this); var b2 = new Button(this); { var ll = new LinearLayout(this); //ll.setOrientation(LinearLayout.VERTICAL); sv.addView(ll); var b1 = new Button(this).AttachTo(ll); b1.WithText("LANBroadcastListener createMulticastLock"); var c = 0; b1.AtClick( v => { // server error { Message = , StackTrace = android.os.NetworkOnMainThreadException //at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) //at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175) //at libcore.io.IoBridge.sendto(IoBridge.java:473) //at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182) //at java.net.DatagramSocket.send(DatagramSocket.java:284) new Thread( delegate() { try { var socket = new DatagramSocket(); //construct a datagram socket and binds it to the available port and the localhos c++; var b = Encoding.UTF8.GetBytes(c + " hi from jvm!"); //creates a variable b of type byte var dgram = new DatagramPacket((sbyte[])(object)b, b.Length, InetAddress.getByName("239.1.2.3"), 40404);//sends the packet details, length of the packet,destination address and the port number as parameters to the DatagramPacket //dgram.setData(b); //System.Console.WriteLine( // "Sending " + b.Length + " bytes to " + dgram.getAddress() + ":" + dgram.getPort());//standard error output stream socket.send(dgram); //send the datagram packet from this port } catch (Exception ex) { System.Console.WriteLine("server error " + new { ex.Message, ex.StackTrace }); } } ) { Name = "sender" }.Start(); } ); b2.setText("The other button!"); ll.addView(b2); this.setContentView(sv); } // http://www.zzzxo.com/q/answers-android-device-not-receiving-multicast-package-13221736.html new Thread( delegate() { // http://stackoverflow.com/questions/12610415/multicast-receiver-malfunction // http://answers.unity3d.com/questions/250732/android-build-is-not-receiving-udp-broadcasts.html // Acquire multicast lock wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); multicastLock = wifi.createMulticastLock("multicastLock"); //multicastLock.setReferenceCounted(true); multicastLock.acquire(); System.Console.WriteLine("LANBroadcastListener ready..."); try { byte[] b = new byte[0x100]; // https://code.google.com/p/android/issues/detail?id=40003 var port = 40404; MulticastSocket socket = new MulticastSocket(port); // must bind receive side socket.setBroadcast(true); socket.setReuseAddress(true); socket.setTimeToLive(30); socket.setReceiveBufferSize(0x100); // https://code.google.com/p/android/issues/detail?id=40003 // http://stackoverflow.com/questions/6550618/multicast-support-on-android-in-hotspot-tethering-mode // http://www.massapi.com/class/java/net/InetSocketAddress.java.html // http://www.javadocexamples.com/java/net/MulticastSocket/joinGroup(SocketAddress%20mcastaddr,NetworkInterface%20netIf).html // http://grokbase.com/t/hadoop/common-issues/117jsjk8d7/jira-created-hadoop-7472-rpc-client-should-deal-with-the-ip-address-changes var group = InetAddress.getByName("239.1.2.3"); var groupSockAddr = new InetSocketAddress(group, port); // what lan interfaces do we have? socket.joinGroup(groupSockAddr, NetworkInterface.getByName("wlan0") ); System.Console.WriteLine("LANBroadcastListener joinGroup..."); while (true) { DatagramPacket dgram = new DatagramPacket((sbyte[])(object)b, b.Length); socket.receive(dgram); // blocks until a datagram is received var bytes = new MemoryStream((byte[])(object)dgram.getData(), 0, dgram.getLength()); var listen = Encoding.UTF8.GetString(bytes.ToArray()); System.Console.WriteLine("Received " + dgram.getLength() + " bytes from " + dgram.getAddress()); //dgram.setLength(b.Length); // must reset length field!s } } catch { System.Console.WriteLine("client error"); } } ) { Name = "client" }.Start(); }
public static void Main(string[] args) { System.Console.WriteLine(": " + typeof(object).FullName); /// http://www.daniweb.com/software-development/java/threads/424998/udp-client-server-in-java new Thread( delegate() { try { DatagramSocket socket = new DatagramSocket(); //construct a datagram socket and binds it to the available port and the localhos byte[] b = Encoding.UTF8.GetBytes("hi from jvm!"); //creates a variable b of type byte DatagramPacket dgram; dgram = new DatagramPacket((sbyte[])(object)b, b.Length, InetAddress.getByName("239.1.2.3"), 40404);//sends the packet details, length of the packet,destination address and the port number as parameters to the DatagramPacket //dgram.setData(b); System.Console.WriteLine( "Sending " + b.Length + " bytes to " + dgram.getAddress() + ":" + dgram.getPort());//standard error output stream while (true) { System.Console.WriteLine("."); socket.send(dgram); //send the datagram packet from this port Thread.Sleep(1000); //cause the current executed thread to sleep for a certain number of miliseconds } } catch { System.Console.WriteLine("server error"); } } ) { Name = "server" }.Start(); new Thread( delegate() { try { byte[] b = new byte[011]; DatagramPacket dgram = new DatagramPacket((sbyte[])(object)b, b.Length); MulticastSocket socket = new MulticastSocket(40404); // must bind receive side socket.joinGroup(InetAddress.getByName("239.1.2.3")); while (true) { socket.receive(dgram); // blocks until a datagram is received System.Console.WriteLine("Received " + Encoding.UTF8.GetString((byte[])(object)dgram.getData()) + " bytes from " + dgram.getAddress()); dgram.setLength(b.Length); // must reset length field!s } } catch { System.Console.WriteLine("client error"); } } ) { Name = "client" }.Start(); CLRProgram.XML = new XElement("hello", "world"); CLRProgram.CLRMain( ); }