new Socket

   
  

using System;
using System.Net;
using System.Net.Sockets;
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(test.AddressFamily);
        Console.WriteLine(test.SocketType);
        Console.WriteLine(test.ProtocolType);
        Console.WriteLine(test.Blocking);
        test.Blocking = false;
        Console.WriteLine(test.Blocking);
        Console.WriteLine(test.Connected);
        test.Bind(ie);
        IPEndPoint iep = (IPEndPoint)test.LocalEndPoint;
        Console.WriteLine(iep.ToString());
        test.Close();
    }
}

   
     


Simple SNMP

   

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;


public class SimpleSNMP
{
   public static void Main(string[] argv)
   {
      int commlength, miblength, datatype, datalength, datastart;
      int uptime = 0;
      string output;
      SNMP conn = new SNMP();
      byte[] response = new byte[1024];

      Console.WriteLine("Device SNMP information:");

      // Send sysName SNMP request
      response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.5.0");
      if (response[0] == 0xff)
      {
         Console.WriteLine("No response from {0}", argv[0]);
         return;
      }

      // If response, get the community name and MIB lengths
      commlength = Convert.ToInt16(response[6]);
      miblength = Convert.ToInt16(response[23 + commlength]);

      // Extract the MIB data from the SNMP response
      datatype = Convert.ToInt16(response[24 + commlength + miblength]);
      datalength = Convert.ToInt16(response[25 + commlength + miblength]);
      datastart = 26 + commlength + miblength;
      output = Encoding.ASCII.GetString(response, datastart, datalength);
      Console.WriteLine("  sysName - Datatype: {0}, Value: {1}",
              datatype, output);

      // Send a sysLocation SNMP request
      response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.6.0");
      if (response[0] == 0xff)
      {
         Console.WriteLine("No response from {0}", argv[0]);
         return;
      }

      // If response, get the community name and MIB lengths
      commlength = Convert.ToInt16(response[6]);
      miblength = Convert.ToInt16(response[23 + commlength]);

      // Extract the MIB data from the SNMP response
      datatype = Convert.ToInt16(response[24 + commlength + miblength]);
      datalength = Convert.ToInt16(response[25 + commlength + miblength]);
      datastart = 26 + commlength + miblength;
      output = Encoding.ASCII.GetString(response, datastart, datalength);
      Console.WriteLine("  sysLocation - Datatype: {0}, Value: {1}", datatype, output);

      // Send a sysContact SNMP request
      response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.4.0");
      if (response[0] == 0xff)
      {
         Console.WriteLine("No response from {0}", argv[0]);
         return;
      }

      // Get the community and MIB lengths
      commlength = Convert.ToInt16(response[6]);
      miblength = Convert.ToInt16(response[23 + commlength]);

      // Extract the MIB data from the SNMP response
      datatype = Convert.ToInt16(response[24 + commlength + miblength]);
      datalength = Convert.ToInt16(response[25 + commlength + miblength]);
      datastart = 26 + commlength + miblength;
      output = Encoding.ASCII.GetString(response, datastart, datalength);
      Console.WriteLine("  sysContact - Datatype: {0}, Value: {1}",
              datatype, output);
      
      // Send a SysUptime SNMP request
      response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.3.0");
      if (response[0] == 0xff)
      {
         Console.WriteLine("No response from {0}", argv[0]);
         return;
      }

      // Get the community and MIB lengths of the response
      commlength = Convert.ToInt16(response[6]);
      miblength = Convert.ToInt16(response[23 + commlength]);

      // Extract the MIB data from the SNMp response
      datatype = Convert.ToInt16(response[24 + commlength + miblength]);
      datalength = Convert.ToInt16(response[25 + commlength + miblength]);
      datastart = 26 + commlength + miblength;

      // The sysUptime value may by a multi-byte integer
      // Each byte read must be shifted to the higher byte order
      while(datalength > 0)
      {
         uptime = (uptime << 8) + response&#91;datastart++&#93;;
         datalength--;
      }
      Console.WriteLine("  sysUptime - Datatype: {0}, Value: {1}",
             datatype, uptime);

   }
}


class SNMP
{
   public SNMP()
   {

   }

   public byte&#91;&#93; get(string request, string host, string community, string mibstring)
   {
      byte&#91;&#93; packet = new byte&#91;1024&#93;;
      byte&#91;&#93; mib = new byte&#91;1024&#93;;
      int snmplen;
      int comlen = community.Length;
      string&#91;&#93; mibvals = mibstring.Split(&#039;.&#039;);
      int miblen = mibvals.Length;
      int cnt = 0, temp, i;
      int orgmiblen = miblen;
      int pos = 0;

      // Convert the string MIB into a byte array of integer values
      // Unfortunately, values over 128 require multiple bytes
      // which also increases the MIB length
      for (i = 0; i < orgmiblen; i++)
      {
         temp = Convert.ToInt16(mibvals&#91;i&#93;);
         if (temp > 127)
         {
            mib[cnt] = Convert.ToByte(128 + (temp / 128));
            mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
            cnt += 2;
            miblen++;
         } else
         {
            mib[cnt] = Convert.ToByte(temp);
            cnt++;
         }
      }
      snmplen = 29 + comlen + miblen - 1;  //Length of entire SNMP packet

      //The SNMP sequence start
      packet[pos++] = 0x30; //Sequence start
      packet[pos++] = Convert.ToByte(snmplen - 2);  //sequence size

      //SNMP version
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP version 1

      //Community name
      packet[pos++] = 0x04; // String type
      packet[pos++] = Convert.ToByte(comlen); //length
      //Convert community name to byte array
      byte[] data = Encoding.ASCII.GetBytes(community);
      for (i = 0; i < data.Length; i++)
      {
         packet&#91;pos++&#93; = data&#91;i&#93;;
      }

      //Add GetRequest or GetNextRequest value
      if (request == "get")
         packet&#91;pos++&#93; = 0xA0;
      else
         packet&#91;pos++&#93; = 0xA1;

      packet&#91;pos++&#93; = Convert.ToByte(20 + miblen - 1); //Size of total MIB

      //Request ID
      packet&#91;pos++&#93; = 0x02; //Integer type
      packet&#91;pos++&#93; = 0x04; //length
      packet&#91;pos++&#93; = 0x00; //SNMP request ID
      packet&#91;pos++&#93; = 0x00;
      packet&#91;pos++&#93; = 0x00;
      packet&#91;pos++&#93; = 0x01;

      //Error status
      packet&#91;pos++&#93; = 0x02; //Integer type
      packet&#91;pos++&#93; = 0x01; //length
      packet&#91;pos++&#93; = 0x00; //SNMP error status

      //Error index
      packet&#91;pos++&#93; = 0x02; //Integer type
      packet&#91;pos++&#93; = 0x01; //length
      packet&#91;pos++&#93; = 0x00; //SNMP error index

      //Start of variable bindings
      packet&#91;pos++&#93; = 0x30; //Start of variable bindings sequence

      packet&#91;pos++&#93; = Convert.ToByte(6 + miblen - 1); // Size of variable binding

      packet&#91;pos++&#93; = 0x30; //Start of first variable bindings sequence
      packet&#91;pos++&#93; = Convert.ToByte(6 + miblen - 1 - 2); // size
      packet&#91;pos++&#93; = 0x06; //Object type
      packet&#91;pos++&#93; = Convert.ToByte(miblen - 1); //length

      //Start of MIB
      packet&#91;pos++&#93; = 0x2b;
      //Place MIB array in packet
      for(i = 2; i < miblen; i++)
         packet&#91;pos++&#93; = Convert.ToByte(mib&#91;i&#93;);
      packet&#91;pos++&#93; = 0x05; //Null object value
      packet&#91;pos++&#93; = 0x00; //Null

      //Send packet to destination
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                       ProtocolType.Udp);
      sock.SetSocketOption(SocketOptionLevel.Socket,
                      SocketOptionName.ReceiveTimeout, 5000);
      IPHostEntry ihe = Dns.Resolve(host);
      IPEndPoint iep = new IPEndPoint(ihe.AddressList&#91;0&#93;, 161);
      EndPoint ep = (EndPoint)iep;
      sock.SendTo(packet, snmplen, SocketFlags.None, iep);

      //Receive response from packet
      try
      {
         int recv = sock.ReceiveFrom(packet, ref ep);
      } catch (SocketException)
      {
         packet&#91;0&#93; = 0xff;
      }
      return packet;
   }

   public string getnextMIB(byte&#91;&#93; mibin)
   {
      string output = "1.3";
      int commlength = mibin&#91;6&#93;;
      int mibstart = 6 + commlength + 17; //find the start of the mib section
      //The MIB length is the length defined in the SNMP packet
     // minus 1 to remove the ending .0, which is not used
      int miblength = mibin&#91;mibstart&#93; - 1;
      mibstart += 2; //skip over the length and 0x2b values
      int mibvalue;

      for(int i = mibstart; i < mibstart + miblength; i++)
      {
         mibvalue = Convert.ToInt16(mibin&#91;i&#93;);
         if (mibvalue > 128)
         {
            mibvalue = (mibvalue/128)*128 + Convert.ToInt16(mibin[i+1]);
            //ERROR here, it should be mibvalue = (mibvalue-128)*128 + Convert.ToInt16(mibin[i+1]);
            //for mib values greater than 128, the math is not adding up correctly   
            
            i++;
         }
         output += "." + mibvalue;
      }
      return output;
   }
}


           
          


Simple Ping

   

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SimplePing
{
   public static void Main(string[] argv)
   {
      byte[] data = new byte[1024];
      int recv;
      Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
                 ProtocolType.Icmp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Parse(argv[0]), 0);
      EndPoint ep = (EndPoint)iep;
      ICMP packet = new ICMP();

      packet.Type = 0x08;
      packet.Code = 0x00;
      packet.Checksum = 0;
      Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.Message, 0, 2);
      Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.Message, 2, 2);
      data = Encoding.ASCII.GetBytes("test packet");
      Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
      packet.MessageSize = data.Length + 4;
      int packetsize = packet.MessageSize + 4;

      UInt16 chcksum = packet.getChecksum();
      packet.Checksum = chcksum;

      host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
      host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
      try
      {
         data = new byte[1024];
         recv = host.ReceiveFrom(data, ref ep);
      } catch (SocketException)
      {
         Console.WriteLine("No response from remote host");
         return;
      }
      ICMP response = new ICMP(data, recv);
      Console.WriteLine("response from: {0}", ep.ToString());
      Console.WriteLine("  Type {0}", response.Type);
      Console.WriteLine("  Code: {0}", response.Code);
      int Identifier = BitConverter.ToInt16(response.Message, 0);
      int Sequence = BitConverter.ToInt16(response.Message, 2);
      Console.WriteLine("  Identifier: {0}", Identifier);
      Console.WriteLine("  Sequence: {0}", Sequence);
      string stringData = Encoding.ASCII.GetString(response.Message, 4, response.MessageSize - 4);
      Console.WriteLine("  data: {0}", stringData);
      
      host.Close();
   }
}

class ICMP
{
   public byte Type;
   public byte Code;
   public UInt16 Checksum;
   public int MessageSize;
   public byte[] Message = new byte[1024];

   public ICMP()
   {
   }

   public ICMP(byte[] data, int size)
   {
      Type = data[20];
      Code = data[21];
      Checksum = BitConverter.ToUInt16(data, 22);
      MessageSize = size - 24;
      Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
   }

   public byte[] getBytes()
   {
      byte[] data = new byte[MessageSize + 9];
      Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1);
      Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1);
      Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2);
      Buffer.BlockCopy(Message, 0, data, 4, MessageSize);
      return data;
   }

   public UInt16 getChecksum()
   {
      UInt32 chcksm = 0;
      byte[] data = getBytes();
      int packetsize = MessageSize + 8;
      int index = 0;

      while ( index < packetsize)
      {
         chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
         index += 2;
      }
      chcksm = (chcksm >> 16) + (chcksm &amp; 0xffff);
      chcksm += (chcksm >> 16);
      return (UInt16)(~chcksm);
   }
}


           
          


Ping Success and Send

   
 
using System;
using System.Net.NetworkInformation;

class MainClass {
    public static void Main(string[] args) {
        using (Ping ping = new Ping()) {
            Console.WriteLine("Pinging:");

            foreach (string comp in args) {
                try {
                    Console.Write("    {0}...", comp);
                    PingReply reply = ping.Send(comp, 100);
                    if (reply.Status == IPStatus.Success) {
                        Console.WriteLine("Success - IP Address:{0}", reply.Address, reply.RoundtripTime);
                    } else {
                        Console.WriteLine(reply.Status);
                    }
                } catch (Exception ex) {
                    Console.WriteLine("Error ({0})",
                        ex.InnerException.Message);
                }
            }
        }
    }
}

    


Ping and PingReply

   
 

using System;
using System.Net.NetworkInformation;

class MainClass {
    public static void Main(string[] args) {
        using (Ping ping = new Ping()) {
            foreach (string comp in args) {
                try {
                    Console.Write("    {0}...", comp);
                    PingReply reply = ping.Send(comp, 100);
                    if (reply.Status == IPStatus.Success) {
                        Console.WriteLine("Success - IP Address:{0}", reply.Address, reply.RoundtripTime);
                    } else {
                        Console.WriteLine(reply.Status);
                    }
                } catch (Exception ex) {
                    Console.WriteLine("Error ({0})",
                        ex.InnerException.Message);
                }
            }
        }
    }
}

    


Success

   
 

using System;
using System.Net.NetworkInformation;

class MainClass {
    public static void Main(string[] args) {
        using (Ping ping = new Ping()) {
            Console.WriteLine("Pinging:");

            foreach (string comp in args) {
                try {
                    Console.Write("    {0}...", comp);
                    PingReply reply = ping.Send(comp, 100);
                    if (reply.Status == IPStatus.Success) {
                        Console.WriteLine("Success - IP Address:{0}", reply.Address, reply.RoundtripTime);
                    } else {
                        Console.WriteLine(reply.Status);
                    }
                } catch (Exception ex) {
                    Console.WriteLine("Error ({0})",
                        ex.InnerException.Message);
                }
            }
        }
    }
}

    


implements a NetworkStream client 2

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example15_12b.cs implements a NetworkStream client
*/

using System;
using System.IO;
using System.Net.Sockets ;

public class Example15_12b
{

public static void Main()
{

// create a client socket
TcpClient newSocket = new TcpClient(“localhost”, 50001);

// create a NetworkStream to read from the host
NetworkStream ns = newSocket.GetStream();

// fill a byte array from the stream
byte[] buf = new byte[100];
ns.Read(buf, 0, 100);

// convert to a char array and print
char[] buf2 = new char[100];
for(int i=0;i<100;i++) buf2[i]=(char)buf[i]; Console.WriteLine(buf2); // clean up ns.Close(); newSocket.Close(); } } [/csharp]