/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.Net; using System.Net.Sockets; using System.Text; public class MultiRecv { public static void Main() { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Console.WriteLine("Ready to receive..."); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050); EndPoint ep = (EndPoint)iep; sock.Bind(iep); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1"))); byte[] data = new byte[1024]; int recv = sock.ReceiveFrom(data, ref ep); string stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString()); sock.Close(); } }
C# Network
Multi Send
using System; using System.Net; using System.Net.Sockets; using System.Text; public class MultiSend { public static void Main() { Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050); byte[] data = Encoding.ASCII.GetBytes("This is a test message"); server.SendTo(data, iep); server.Close(); } }
New Multi Send
using System; using System.Net; using System.Net.Sockets; using System.Text; public class NewMultiSend { public static void Main() { Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9051); IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050); server.Bind(iep); byte[] data = Encoding.ASCII.GetBytes("This is a test message"); server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1"))); server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 50); server.SendTo(data, iep2); server.Close(); } }
Creating Socket Connections
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; using System.Net; using System.Net.Sockets; using System.Text; namespace Client.Chapter_14___Networking_and_WWW { public class CreatingSocketConnections { [STAThread] static void Main(string[] args) { TcpClient MyClient = new TcpClient(); MyClient.Connect("http://www.kutayzorlu.com/java2s/com", 10000); NetworkStream MyNetStream = MyClient.GetStream(); if(MyNetStream.CanWrite && MyNetStream.CanRead) { // Does a simple write. Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there"); MyNetStream.Write(sendBytes, 0, sendBytes.Length); // Reads the NetworkStream into a byte buffer. byte[] bytes = new byte[MyClient.ReceiveBufferSize]; MyNetStream.Read(bytes, 0, (int) MyClient.ReceiveBufferSize); // Returns the data received from the host to the console. string returndata = Encoding.ASCII.GetString(bytes); Console.WriteLine("This is what the host returned to you: " + returndata); } else if (!MyNetStream.CanRead) { Console.WriteLine("You can not write data to this stream"); MyClient.Close(); } else if (!MyNetStream.CanWrite) { Console.WriteLine("You can not read data from this stream"); MyClient.Close(); } } } }
Socket property
using System; using System.Net; using System.Net.Sockets; public class SockProp { public static void Main() { IPAddress ia = IPAddress.Parse("127.0.0.1"); IPEndPoint ie = new IPEndPoint(ia, 8000); Socket test = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("AddressFamily: {0}",test.AddressFamily); Console.WriteLine("SocketType: {0}",test.SocketType); Console.WriteLine("ProtocolType: {0}",test.ProtocolType); Console.WriteLine("Blocking: {0}", test.Blocking); test.Blocking = false; Console.WriteLine("new Blocking: {0}",test.Blocking); Console.WriteLine("Connected: {0}", test.Connected); test.Bind(ie); IPEndPoint iep = (IPEndPoint)test.LocalEndPoint; Console.WriteLine("Local EndPoint: {0}",iep.ToString()); test.Close(); } }
Socket Exception
using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketExcept { public static void Main() { IPAddress host = IPAddress.Parse("192.168.1.1"); IPEndPoint hostep = new IPEndPoint(host, 8000); Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); try { sock.Connect(hostep); } catch (SocketException e) { Console.WriteLine("Problem connecting to host"); Console.WriteLine(e.ToString()); sock.Close(); return; } try { sock.Send(Encoding.ASCII.GetBytes("testing")); } catch (SocketException e) { Console.WriteLine("Problem sending data"); Console.WriteLine( e.ToString()); sock.Close(); return; } sock.Close(); } }
Socket Connect, Send
using System; using System.Net; using System.Net.Sockets; using System.Text; class SocketExcept { public static void Main() { IPAddress host = IPAddress.Parse("192.168.1.1"); IPEndPoint hostep = new IPEndPoint(host, 8000); Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); try { sock.Connect(hostep); } catch (SocketException e) { Console.WriteLine("Problem connecting to host"); Console.WriteLine(e.ToString()); sock.Close(); return; } try { sock.Send(Encoding.ASCII.GetBytes("testing")); } catch (SocketException e) { Console.WriteLine("Problem sending data"); Console.WriteLine(e.ToString()); sock.Close(); return; } sock.Close(); } }