how to convert C# console app code to windows form button
class TcpClientSample
{
public static void Main()
{
byte[] data = new byte[1024]; // creating a buffer of 1MB to read
and write data
string stringData;
TcpClient server; // initializing tcpclient object
named "server"
try
{
server = new TcpClient("192.168.1.1", 23); // connecting
tcpclient object with controller
}
catch (SocketException)
{
Console.WriteLine("Unable to connect to server");
return;
}
NetworkStream ns = server.GetStream(); //Getstream object to
send and receive data to and fro from controller
ns.Flush();
Thread.Sleep(500);
int recv =ns.Read(data, 0, data.Length); //start to read data
from the controller using network stream
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
ns.Flush();
string login = "admin\n"; //login with admin
data = Encoding.ASCII.GetBytes(login);
ns.Write(data, 0, data.Length);
ns.Flush();
Thread.Sleep(500);
data = new byte[1024]; //again read the data from the
controller
recv = ns.Read(data, 0, data.Length);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
ns.Flush();
string password = "admin\n"; //set password to admin
data = Encoding.ASCII.GetBytes(password);
ns.Write(data, 0, data.Length);
Thread.Sleep(500);
ns.Flush();
data = new byte[1024]; //again read data further from
the controller and write it to the Console
recv = ns.Read(data, 0, data.Length);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
Thread.Sleep(1000);
string config= "config t\n"; //after login & password send
command "config t" or any command can be send
data = Encoding.ASCII.GetBytes(config);
ns.Write(data, 0, data.Length);
Thread.Sleep(1000);
string wireless = "wireless\n"; //send wireless command to
enter in wireless mode
data = Encoding.ASCII.GetBytes(wireless);
ns.Write(data, 0, data.Length);
Thread.Sleep(1000);
string enable = "enable\n"; //send "enable" command to enable
wlan
data = Encoding.ASCII.GetBytes(enable);
ns.Write(data, 0, data.Length);
string ApProfile = "ap profile 1\n"; //send wireless command
to enter in wireless mode
data = Encoding.ASCII.GetBytes(ApProfile);
ns.Write(data, 0, data.Length);
Thread.Sleep(1000);
string hwType = "hwtype 22\n"; //send wireless command to
enter in wireless mode
data = Encoding.ASCII.GetBytes(hwType);
ns.Write(data, 0, data.Length);
Console.Write("Wait for 2 mins while AP and controller negotiate
each other");
Thread.Sleep(120000);
/*
string shwireless = "show wireless\n"; //send wireless command
to enter in wireless mode
data = Encoding.ASCII.GetBytes(shwireless);
ns.Write(data, 0, data.Length);
*/
//Thread.Sleep(1000);
string shwireless = "show wireless ap status\n"; //send
wireless command to enter in wireless mode
data = Encoding.ASCII.GetBytes(shwireless);
ns.Write(data, 0, data.Length);
Thread.Sleep(1000);
data = new byte[1024]; //write data to the console
recv = ns.Read(data, 0, data.Length);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
}
I have above C# code ,it is working fine for console application,i want to
write it for windows application and i want to code it behind the button
?? How could i do that?? Any help would be highly appreciable.
No comments:
Post a Comment