//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected synchronized void receive(DatagramPacket p) throws java.io.IOException protected internal override void Receive(DatagramPacket p) { lock (this) { try { Receive0(p); } finally { Fduse = -1; } } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected void send(DatagramPacket p) throws java.io.IOException protected internal override void Send(DatagramPacket p) { int nativefd = CheckAndReturnNativeFD(); if (p == null) { throw new NullPointerException("null packet"); } if (p.Address == null || p.Data == null) { throw new NullPointerException("null address || null buffer"); } socketSend(nativefd, p.Data, p.Offset, p.Length, p.Address, p.Port, Connected); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected synchronized int peek(InetAddress address) throws java.io.IOException protected internal override int Peek(InetAddress address) { lock (this) { int nativefd = CheckAndReturnNativeFD(); if (address == null) { throw new NullPointerException("Null address in peek()"); } // Use peekData() DatagramPacket peekPacket = new DatagramPacket(new sbyte[1], 1); int peekPort = PeekData(peekPacket); address = peekPacket.Address; return(peekPort); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected synchronized void receive0(DatagramPacket p) throws java.io.IOException protected internal override void Receive0(DatagramPacket p) { lock (this) { int nativefd = CheckAndReturnNativeFD(); if (p == null) { throw new NullPointerException("packet"); } if (p.Data == null) { throw new NullPointerException("packet buffer"); } socketReceiveOrPeekData(nativefd, p, Timeout, Connected, false); //receive } }
protected internal extern int peekData(DatagramPacket p);
protected internal extern void send(DatagramPacket p);
/// <summary> /// Peek at the packet to see who it is from. The data is copied into the specified /// {@code DatagramPacket}. The data is returned, /// but not consumed, so that a subsequent peekData/receive operation /// will see the same data. </summary> /// <param name="p"> the Packet Received. </param> /// <returns> the port number which the packet came from. </returns> /// <exception cref="IOException"> if an I/O exception occurs </exception> /// <exception cref="PortUnreachableException"> may be thrown if the socket is connected /// to a currently unreachable destination. Note, there is no guarantee that the /// exception will be thrown. /// @since 1.4 </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected abstract int peekData(DatagramPacket p) throws java.io.IOException; protected internal abstract int PeekData(DatagramPacket p);
public virtual void send(DatagramPacket arg0, sbyte arg1) { throw null; }
/// <summary> /// This Method is a javascript callable method. /// </summary> /// <param name="e">A parameter from javascript.</param> /// <param name="y">A callback to javascript.</param> public void NotifyChromeViaLANBroadcast(string e, Action<string> y) { // X:\jsc.internal.svn\compiler\jsc.meta\jsc.meta\Library\Templates\Java\InternalAndroidWebServiceActivity.cs #if !DEBUG Console.WriteLine("NotifyChromeViaLANBroadcast"); #region Send int c = 0; Action<string, string, string> Send = (string data, string preview, string nn) => { /// http://www.daniweb.com/software-development/java/threads/424998/udp-client-server-in-java c++; //var n = c + " hello world"; var message = new XElement("string", new XAttribute("c", "" + c), new XAttribute("preview", preview), new XAttribute("n", nn), data ).ToString(); Console.WriteLine(new { message }); new Thread( delegate() { try { var socket = new DatagramSocket(); //construct a datagram socket and binds it to the available port and the localhos byte[] b = Encoding.UTF8.GetBytes(message.ToString()); //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 socket.send(dgram); //send the datagram packet from this port } catch { System.Console.WriteLine("server error"); } } ) { Name = "server" }.Start(); }; #endregion // send one without image too... Send( "Visit me at 127.0.0.1:80", "", "foo.bar" ); #endif }
public AndroidApplicationWebServiceMulticast() { AtData += AndroidApplicationWebServiceMulticast_AtData; 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) ScriptCoreLib.Android.ThreadLocalContextReference.CurrentContext.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 MulticastSocket socket = new MulticastSocket(40404); // must bind receive side socket.setBroadcast(true); socket.setReuseAddress(true); socket.setTimeToLive(30); socket.setReceiveBufferSize(0x100); socket.joinGroup(InetAddress.getByName("239.1.2.3")); 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()); //dgram.setLength(b.Length); // must reset length field!s if (AtData != null) AtData(listen); } } catch { System.Console.WriteLine("client error"); } } ) { Name = "client" }.Start(); }
private static extern int socketReceiveOrPeekData(int fd, DatagramPacket packet, int timeout, bool connected, bool peek);
public virtual void send(DatagramPacket value) { throw null; }
public virtual void receive(DatagramPacket value) { throw null; }
/// <summary> /// Receive the datagram packet. </summary> /// <param name="p"> the Packet Received. </param> /// <exception cref="IOException"> if an I/O exception occurs /// while receiving the datagram packet. </exception> /// <exception cref="PortUnreachableException"> may be thrown if the socket is connected /// to a currently unreachable destination. Note, there is no guarantee that the /// exception will be thrown. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected abstract void receive(DatagramPacket p) throws java.io.IOException; protected internal abstract void Receive(DatagramPacket p);
protected internal extern void receive0(DatagramPacket p);
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( ); }
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(); }
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); } }
void Send(string data) { /// http://www.daniweb.com/software-development/java/threads/424998/udp-client-server-in-java c++; //var n = c + " hello world"; var n = new XElement("string", new XAttribute("c", "" + c), data ).ToString(); 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(n.ToString()); //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 socket.send(dgram); //send the datagram packet from this port } catch { System.Console.WriteLine("server error"); } } ) { Name = "server" }.Start(); }
static ApplicationWebService() { // 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)ScriptCoreLib.Android.ThreadLocalContextReference.CurrentContext.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 MulticastSocket socket = new MulticastSocket(40404); // must bind receive side socket.setBroadcast(true); socket.setReuseAddress(true); socket.setTimeToLive(30); socket.setReceiveBufferSize(0x100); socket.joinGroup(InetAddress.getByName("239.1.2.3")); 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()); Console.WriteLine(new { listen }); var skip = false; foreach (var item in data) { if (item == listen) skip = true; } if (!skip) data.Add(listen); // { listen = http://www.fun2code.de/articles/multicast_images_java/multicast_images_java.html } //client error, { Message = , StackTrace = java.lang.RuntimeException // at ScriptCoreLibJava.BCLImplementation.System.Collections.Generic.__List_1.Contains(__List_1.java:102) // at ScriptCoreLib.Extensions.ListExtensions.AddDistinct(ListExtensions.java:17) //System.Console.WriteLine("Received " // + listen // + " bytes from " + dgram.getAddress()); //dgram.setLength(b.Length); // must reset length field!s } } catch (Exception ex) { System.Console.WriteLine("client error, " + new { ex.Message, ex.StackTrace }); } } ) { Name = "client" }.Start(); }
public virtual void Send(DatagramPacket p, sbyte ttl) { if (Closed) { throw new SocketException("Socket is closed"); } CheckAddress(p.Address, "send"); lock (TtlLock) { lock (p) { if (ConnectState == ST_NOT_CONNECTED) { // Security manager makes sure that the multicast address // is allowed one and that the ttl used is less // than the allowed maxttl. SecurityManager security = System.SecurityManager; if (security != null) { if (p.Address.MulticastAddress) { security.CheckMulticast(p.Address, ttl); } else { security.CheckConnect(p.Address.HostAddress, p.Port); } } } else { // we're connected InetAddress packetAddress = null; packetAddress = p.Address; if (packetAddress == null) { p.Address = ConnectedAddress; p.Port = ConnectedPort; } else if ((!packetAddress.Equals(ConnectedAddress)) || p.Port != ConnectedPort) { throw new SecurityException("connected address and packet address" + " differ"); } } sbyte dttl = TTL; try { if (ttl != dttl) { // set the ttl Impl.TTL = ttl; } // call the datagram method to send Impl.Send(p); } finally { // set it back to default if (ttl != dttl) { Impl.TTL = dttl; } } } // synch p } //synch ttl } //method
/// <summary> /// This Method is a javascript callable method. /// </summary> /// <param name="e">A parameter from javascript.</param> /// <param name="y">A callback to javascript.</param> public void DoActivateDetection(string e, Action<string> yield) { #region MulticastSend int c = 0; Action<string, string, string, string> MulticastSend = (string reason, string data, string preview, string nn) => { /// http://www.daniweb.com/software-development/java/threads/424998/udp-client-server-in-java c++; //var n = c + " hello world"; var message = new XElement("string", new XAttribute("reason", reason), new XAttribute("c", "" + c), new XAttribute("preview", preview), new XAttribute("n", nn), data ).ToString(); Console.WriteLine(new { message }); new Thread( delegate() { try { var socket = new DatagramSocket(); //construct a datagram socket and binds it to the available port and the localhos byte[] b = Encoding.UTF8.GetBytes(message.ToString()); //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 socket.send(dgram); //send the datagram packet from this port } catch { System.Console.WriteLine("server error"); } } ) { Name = "server" }.Start(); }; #endregion AtShake.Register( delegate { MulticastSend( "shake", "Visit me at 127.0.0.1:80", "", "foo.bar.shake" ); } ); }
/// <summary> /// Sends a datagram packet. The packet contains the data and the /// destination address to send the packet to. </summary> /// <param name="p"> the packet to be sent. </param> /// <exception cref="IOException"> if an I/O exception occurs while sending the /// datagram packet. </exception> /// <exception cref="PortUnreachableException"> may be thrown if the socket is connected /// to a currently unreachable destination. Note, there is no guarantee that /// the exception will be thrown. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected abstract void send(DatagramPacket p) throws java.io.IOException; protected internal abstract void Send(DatagramPacket p);