I am trying to create the following functionality in inno setup.
The user is asked to enter a port they wish my application to communicate on.
Once they have entered a port they can hit a check button.
This check button will run some code to see whether the port on the installation machine is available.
So far I am fine with creating the input box for the user to enter the port they wish to test. I have the following code to test whether the port is available.
int port = 456; //<--- This is your value
bool isAvailable = true;
// Evaluate current system tcp connections. This is the same information provided
// by the netstat command line application, just in .Net strongly-typed object
// form. We will look through the list, and if our port we would like to use
// in our TcpClient is occupied, we will set isAvailable to false.
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
{
if (tcpi.LocalEndPoint.Port == port)
{
isAvailable = false;
break;
}
}
Console.WriteLine("Port is available");
My first question is: is it possible to return to inno setup the true or false value from the port test code? If its false then I need to let the user know.
My second question is: is the port test code correct?
My third question is: am I right in thinking I will then potentially need to open that port in the firewall.
Related
I am new to the .NET framework.
Currently working on a program that checks if the port is ready to use.
I have used IPGlobalProperties to get the port info which was specified here
IPGlobalProperties ipGP = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] tcpEndPoints= ipGP.GetActiveTcpListeners();
IPEndPoint[] updEndPoints = ipGP.GetActiveUdpListeners();
// port validation
Since "ESTABLISHED" & "LISTENING" states refer to open ports
as per this post
I also need a solution to get the port that is in the "ESTABLISHED" state.
Is there a way to get the ports that are in the "ESTABLISHED" state in C#
Not sure what you want to see exactly, but this will show you all the active local established sockets and ports
var active = result.GetActiveTcpConnections().Where(c => c.State == TcpState.Established);
active.Select(c => $"{c.LocalEndPoint.Address} {c.LocalEndPoint.Port}")
.ToList()
.ForEach(Console.WriteLine);
I am writing a program with c# and .net core 3.1 which connecting to a UPS (Uninterruptible power supply) via Telnet connection. For Telnet connection I use codes from Quick tool : A minimalistic Telnet library - CodeProject .
//create a new telnet connection to hostname "gobelijn" on port "23"
TelnetConnection tc = new TelnetConnection("gobelijn", 23);
//login with user "root",password "rootpassword", using a timeout of 100ms,
//and show server output
string s = tc.Login("root", "rootpassword",100);
Console.Write(s);
// server output should end with "$" or ">", otherwise the connection failed
string prompt = s.TrimEnd();
prompt = s.Substring(prompt.Length -1,1);
if (prompt != "$" && prompt != ">" )
throw new Exception("Connection failed");
prompt = "";
// while connected
while (tc.IsConnected && prompt.Trim() != "exit" )
{
// display server output
Console.Write(tc.Read());
// send client input to server
prompt = Console.ReadLine();
tc.WriteLine(prompt);
// display server output
Console.Write(tc.Read());
}
Console.WriteLine("***DISCONNECTED");
Console.ReadLine();
It worked for some simple one line-many parameter commands; like Settime [YYMMDD-HHmmss] that set time of the device. But When I want to execute a command like nsave that save changes in the device, command prompt of Telnet becomes like this:
>>nsave
Are you sure? (y/n)
I want to send y but I can’t.
Besides that, after each command I execute exit command to disconnect Telnet connection, but in this scenario (nsave) the connection will remain open and my program cannot connect to Telnet to execute further commands.
How can I include yes or other inputs in similar conditions?
I've made a Bootloader implementation in a SAMD21J18A mcu, based on a
Atmel example. I can connect to the bootloader via USB CDC and it is recognized in windows as a COM port with the name: AT91 USB to Serial Converter, not showing any problems.
The COM-port works fine, I've made a working Windows Form C# program to download code to the bootloader. In the working program I've just manually set the PortName.
Now, I want to make the program more user friendly and want to make a ComboBoxto enable the user to pick the COM-port to use. Therefore I'm calling the GetPortNames function:
string[] Ports = null;
Ports = SerialPort.GetPortNames();
This works fine, as long as I don't plugin my bootloader.
If I attach eg. a FTDI device, it detects the device and adds it to the list, but if I attach the Bootloader("AT91 USB to Serial Converter"), then I get a System.StackOverflowException on the SerialPort.GetPortNames() function.
Does anyone know what is wrong with GetPortNames or the USB CDC driver?
Thanks for your help mjwills :-)
The complete program were too large to show here, so I decided to make a short program showing the problem. However, the new short program didn't have this problem. So, found out the problem didn't directly originate from the line refered to by visual studio.
Anyway, here's the function which created the problem:
// Update comport pull-down list:
private void UpdateComPortList()
{
string[] Ports = null;
Ports = SerialPort.GetPortNames();
int index = -1;
string ComPortName = null;
comboBoxComPort.Items.Clear();
do
{
index += 1;
comboBoxComPort.Items.Add(Ports[index]);
//if(Properties.Settings.Default.comPort == Ports[index]) comboBoxComPort.SelectedIndex = index;
}
while (!((Ports[index] == ComPortName) || (index == Ports.GetUpperBound(0))));
comboBoxComPort.Items.Add("update list...");
}
If I remove the line which I've now commented, the program works.
I think the problem is that, I try to select an item while at the same time adding items...
My efforts to connect to the ZKTeco device for access by key card goes unsuccessful for couple of days now. I am using .dll file that came with the device zkemkeeper.dll, add the reference in my simple .NET console API.
This my simple code.
static void Main(string[] args)
{
zkemkeeper.CZKEM machineObj = new zkemkeeper.CZKEM();
var status = machineObj.Connect_Net("Device IP", 4370);
Console.WriteLine(status);
Console.ReadLine();
}
As you can see its just Connect_Net("Device IP", 4370) that does all the work, but for some case I keep getting false for status value.
Can someone help, thank you.
The default IP address is 192.168.1.124
Are you able to ping successfully? If not check your network settings. Also note a crossover cable or switch is required.
Are you able to connect with the provided Access software?
This is the situation. I have a Windows machine and a Linux machine. There is a shared drive between these two machines (which is mapped to Q:). I am trying to figure out how to create an SSH session at the Q: drive (shared drive) from C#. I am trying to use the SharpSsh library to do this.
This is what I have so far, however, it is giving me an error:
try
{
ssh = new SshStream(host, username, password);
Console.WriteLine("OK ({0}/{1})", ssh.Cipher, ssh.Mac);
Console.WriteLine("Server version={0}, Client version={1}", ssh.ServerVersion, ssh.ClientVersion);
Console.WriteLine("-Use the 'exit' command to disconnect.");
Console.WriteLine();
//Sets the end of response character
ssh.Prompt = "#";
//Remove terminal emulation characters
ssh.RemoveTerminalEmulationCharacters = true;
//Reads the initial response from the SSH stream
Console.Write(ssh.ReadResponse()); // Blocking here
while (true)
{
string command = Console.ReadLine();
if (command.ToLower().Equals("exit"))
break;
//Write command to the SSH stream
ssh.Write(command);
//Read response from the SSH stream
Console.Write(ssh.ReadResponse());
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
if(ssh != null)
{
ssh.Close();
}
I have added the Tamir.SharpSSH.dll as a reference to the project, and I am using it in the project. There are two other dll's that are included with SharpSSH, do I need to add them as well to the references? The examples I have seen only have the Tamir.SharpSSH.dll as a reference.
I am not sure how to initiate the connection in the correct location, and how to submit commands properly to the ssh.
UPDATE
I realized I needed to close the SSH connection before ending the program. The error does not exist anymore, however, I am still not getting any information from my "ls" command.
UPDATE
I updated the code with what I have now. It seems like the ssh.ReadResponse() is blocking, which leads me to believe the server is not responding. Is that correct?