Showing posts with label visual studio command prompt. Show all posts
Showing posts with label visual studio command prompt. Show all posts

Tuesday, December 14, 2010

Network Broadcasting with C#

What Is Broadcasting?

IP broadcasting is used by network devices to send a single packet of information that can be accessible by every device on the network. Because TCP communication requires that two devices have a dedicated connection, it is not possible to send broadcast messages in a strictly TCP environment. Instead, UDP packets must be used because that protocol has the capability of sending messages without a specific connection being defined.

Broadcst.cs

The Broadcst program creates two separate IPEndPoint objects, one using the IPAddress.Broadcast address and one using a local broadcast address 192.168.1.255. Next, the local hostname of the device is determined using the standard Dns GetHostName() method, and it is sent out in two separate broadcast messages.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Broadcst
{
  public static void Main()
  {
   Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                ProtocolType.Udp);
   IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);
   IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);
   string hostname = Dns.GetHostName();
   byte[] data = Encoding.ASCII.GetBytes(hostname);
   sock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast, 1);
   sock.SendTo(data, iep1);
   sock.SendTo(data, iep2);
   sock.Close();
  }
}


RecvBroadcst.cs

The RecvBroadcst program is a simple UDP program that creates a socket and binds it to all network interfaces on the device, using port 9050, the same port that the Broadcst program uses to send its broadcast messages. After binding the socket to the UDP port, the program attempts to receive two messages sent to the port and displays the message and the sending host address information.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class RecvBroadcst
{
  public static void Main()
  {
   Socket sock = new Socket(AddressFamily.InterNetwork,
           SocketType.Dgram, ProtocolType.Udp);
   IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
   sock.Bind(iep);
   EndPoint ep = (EndPoint)iep;
   Console.WriteLine("Ready to receive…");
   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());
   data = new byte[1024];
   recv = sock.ReceiveFrom(data, ref ep);
   stringData = Encoding.ASCII.GetString(data, 0, recv);
   Console.WriteLine("received: {0} from: {1}",stringData, ep.ToString());
   sock.Close();
  }
}


For compilation either can use visual studio or visual studio command prompt. Here have used visual studio command prompt. Open up two Visual Studio Command prompts, one for the RecvBroadcst.cs and other one for the Broadcst.cs

Compile and run:

Broadcst

csc Broadcst.cs
Broadcst.exe

RecvBroadcst

csc RecvBroadcst.cs
RecvBroadcst.exe

output

Sunday, December 5, 2010

Command Line Parameters C#

Following code shows how command line parameters can accessed in C#.

//CommandLine3.cs
// Commandline arguments: A B C
using System;

namespace CommandLine3
{             
        public class CommandLine3
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("You have entered the following {0} command line arguments:",args.Length);
                for (int i = 0; i < args.Length; i++)
                {
                    Console.WriteLine("{0}", args[i]);
                }
                Console.ReadLine();
            }
        } 
}

Note that:
  • The Length property is used to obtain the length of the array.
  • Length is a read-only property
Following expanation will be a guide on building and running the code within Visual Studio.
  • Right-click the project(in the above example CommandLine3 will be the project) and click Properties this will open up Open the configuration Properties window and click Debugging tab.





  • In the Command Line Arguments property, type "A B C" and save the project





  • Run the project, make sure project has set as the startup project(to do this right click on the project and click on set as startup project

For running the code using command-line use following commands
  • csc CommandLine.cs
  • CommandLine.exe A B C

output


references : http://msdn.microsoft.com/

Friday, November 26, 2010

Visual Studio Command Prompt


C# codes can be compiled using "Visual Studio Command Prompt". Most of the people facing difficulties when it comes to find this command prompt. It resides in "C:\Windows\System32" as cmd.exe. For windows7 users it is possible to search for it through the start menu typing "visual studio command prompt".

Assume test.cs,

How to compile

csc test.cs

How to execute

test.exe