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); } }
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( ); }