using System; using System.Net; class AddressSample { public static void Main() { IPAddress test2 = IPAddress.Loopback; Console.WriteLine("Broadcast address: {0}",test2.ToString()); } }
Parse an IPAddress
using System; using System.Net; class AddressSample { public static void Main() { IPAddress test1 = IPAddress.Parse("192.168.1.1"); Console.WriteLine("The test address is: {0}", test1.ToString()); } }
IPAddress AddressFamily
using System; using System.Net; class MainClass { public static void Main(string[] args) { foreach (string comp in args) { try { IPAddress[] addresses = Dns.GetHostEntry(comp).AddressList; foreach (IPAddress address in addresses) { Console.WriteLine("{0} = {1} ({2})", comp, address, address.AddressFamily); } } catch (Exception ex) { Console.WriteLine("{0} = Error ({1})", comp, ex.Message); } } } }
HTTP put with user name and password
using System; using System.IO; using System.Net; public class HttpPut { public static void Main(string [] args) { string url = "http://www.kutayzorlu.com/java2s/com/"; string username = "user"; string password = "password"; string data = "This data should be written to the URL."; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "PUT"; request.Credentials = new NetworkCredential(username, password); request.ContentLength = data.Length; request.ContentType = "text/plain"; using (StreamWriter writer = new StreamWriter(request.GetRequestStream( ))) { writer.WriteLine(data); } WebResponse response = request.GetResponse( ); using (StreamReader reader = new StreamReader(response.GetResponseStream( ))) { while (reader.Peek( ) != -1) { Console.WriteLine(reader.ReadLine( )); } } } }
New Math Client
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; public class NewMathClient { public static int Main(string[] argv) { HttpChannel chan = new HttpChannel(); ChannelServices.RegisterChannel(chan); MathClass obj = new MathClass(); if (obj == null) System.Console.WriteLine("Could not locate server"); else { int a = Convert.ToInt32(argv[0]); int b = Convert.ToInt32(argv[1]); int c = obj.Add(a, b); Console.WriteLine("a + b = {0}", c); c = obj.Subtract(a, b); Console.WriteLine("a - b = {0}", c); c = obj.Multiply(a, b); Console.WriteLine("a * b = {0}", c); c = obj.Divide(a, b); Console.WriteLine("a / b = {0}", c); } return 0; } } public class MathClass : MarshalByRefObject { public int Add(int a, int b) { int c = a + b; return c; } public int Subtract(int a, int b) { int c = a - b; return c; } public int Multiply(int a, int b) { int c = a * b; return c; } public int Divide(int a, int b) { int c; if (b != 0) c = a / b; else c = 0; return c; } }
Math Client
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; public class MathClient { public static int Main(string[] argv) { HttpChannel chan = new HttpChannel(); ChannelServices.RegisterChannel(chan); MathClass obj = (MathClass)Activator.GetObject( typeof(MathClass), "http://127.0.0.1:9050/MyMathServer"); if (obj == null) System.Console.WriteLine("Could not locate server"); else { int a = Convert.ToInt32(argv[0]); int b = Convert.ToInt32(argv[1]); int c = obj.Add(a, b); Console.WriteLine("a + b = {0}", c); c = obj.Subtract(a, b); Console.WriteLine("a - b = {0}", c); c = obj.Multiply(a, b); Console.WriteLine("a * b = {0}", c); c = obj.Divide(a, b); Console.WriteLine("a / b = {0}", c); } return 0; } } public class MathClass : MarshalByRefObject { public int Add(int a, int b) { int c = a + b; return c; } public int Subtract(int a, int b) { int c = a - b; return c; } public int Multiply(int a, int b) { int c = a * b; return c; } public int Divide(int a, int b) { int c; if (b != 0) c = a / b; else c = 0; return c; } }
HttpChannel Math Server
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; public class MathServer { public static int Main() { HttpChannel chan = new HttpChannel(9050); ChannelServices.RegisterChannel(chan); RemotingConfiguration.RegisterWellKnownServiceType( Type.GetType("MathClass, MathClass"), "MyMathServer", WellKnownObjectMode.SingleCall); Console.WriteLine("Hit <enter> to exit..."); Console.ReadLine(); return 0; } } public class MathClass : MarshalByRefObject { public int Add(int a, int b) { int c = a + b; return c; } public int Subtract(int a, int b) { int c = a - b; return c; } public int Multiply(int a, int b) { int c = a * b; return c; } public int Divide(int a, int b) { int c; if (b != 0) c = a / b; else c = 0; return c; } }