How to find COM port for a specific USB devices connected. of C# 2.0 - c#-2.0

I'm trying to get the list of com port name and the device's name connected to it. (eg: I want to automatically find the COM port for a specific USB device of Fastrack modem M1206B series). In the case where it finds multiple possible ports and i want only finds the port depending on what other usb devices are connected. I tried the following where I get only the list of com ports.
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
cboCOM.Items.Add(port);
}
But I need get COMPort only finds the port depending on what other usb devices are connected.

USB does not lie on COM ports. However you can find the port name caption like
getPorts("SAMSUNG X7j4j"); // your device name
void getPorts(string usbDeviceName)
{
var searcher = new ManagementObjectSearcher(#"Select * From Win32_USBHub");
ManagementObjectCollection collection = searcher.Get();
foreach (var device in collection)
{
string deviceId = device["DeviceID"].ToString();
string port = device["Caption"].ToString();
if (deviceId == usbDeviceName)
MessageBox.Show("Port for " + usbDeviceName + " is " + port);
//MessageBox.Show(deviceId + "\n" + port + "\n" );
}
}

Related

tcpListener on Win 10 iOT not working

I have a UWA on RPi 3 with Win 10 version 10.0.14393.0 and VS 2015 Update 3. I'm trying to run a TCPListener on my RPi, code runs with no exception but never can connect it, seems that some things block my connection. there is no hardware or software Firewall in path. I tried both background and foreground app but no result.
My code is as below :
namespace TestBackPort
{
public sealed class StartupTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
TcpListener tcpListener = null;
tcpListener = new TcpListener(IPAddress.Parse("192.168.1.9"), 1100);
tcpListener.Start();
var task = HandleConnectionsAsync(tcpListener);
task.Wait();
}
int connectionNumber = 0;
async Task HandleConnectionsAsync(TcpListener listener)
{
while (true)
{
var client = await listener.AcceptTcpClientAsync();
// Console.WriteLine("OK #" + connectionNumber);
connectionNumber++;
}
}
}
}
First of all, check your network state using "netstat" utility.
Connect to your raspberry pi using putty or powershell
Do "netstate -a" to verify that the TCP server is actually listening on that port.
When you have your server running, you should see something similar to below
Secondly, make sure you have the Internet Server capability enabled in the project manifest file. It could be either the Internet or the Private Networks, like below.

Cannot connect to private ip (behind nat) even with PortMapping added to upnp router

I'm trying to make a p2p connection from a device outside a private network to a device inside a private network and behind a NAT.
I'm using lib net.sbbi.upnp to add a Port Mapping to my router.
import net.sbbi.upnp.impls.InternetGatewayDevice;
import net.sbbi.upnp.messages.UPNPResponseException;
.....
uPnPPortMapper = new UPnPPortMapper();
try {
uPnPPortMapper.openRouterPort(externalIP, externalPort,internalIP,internalPort, ApplicationConstants.ADD_PORT_DESCRIPTION);
} catch (IOException e) {
e.printStackTrace();
} catch (UPNPResponseException e) {
e.printStackTrace();
}
public boolean openRouterPort(String externalRouterIP,int externalRouterPort, String internalIP,int internalPort, String description) throws IOException, UPNPResponseException {
/** Upnp devices - router search*/
if(internetGatewayDevices == null){
internetGatewayDevices = InternetGatewayDevice.getDevices(ApplicationConstants.SCAN_TIMEOUT);
}
if(internetGatewayDevices != null){
for (InternetGatewayDevice addIGD : internetGatewayDevices) {
addIGD.addPortMapping(description, externalRouterIP, internalPort, externalRouterPort, internalIP, 0, ApplicationConstants.TCP_PROTOCOL);
addIGD.addPortMapping(description, externalRouterIP, internalPort, externalRouterPort, internalIP, 0, ApplicationConstants.UDP_PROTOCOL);
}
return true;
}else{
return false;
}
}
This code do add a port mapping inside my router like you can see in below UPnP table of my TP Link TL-WR940ND:
Current UPnP Settings List
ID App Description External Port Protocol Internal Port IP Address Status
1 Skype UDP at 192.168.0.101:3105 31051 UDP 31051 192.168.0.101 Enabled
2 Skype TCP at 192.168.0.101:3105 31051 TCP 31051 192.168.0.101 Enabled
3 OPEN PORT ON IGD USING UNPN 31052 TCP 31052 192.168.0.102 Enabled
4 OPEN PORT ON IGD USING UNPN 31052 UDP 31052 192.168.0.102 Enabled
The problem is that when I try to establish a connection from outside my network to my public ip + port 31052 I receive a timeout.
I'm using the site (http://www.ipfingerprints.com/portscan.php) to check port status and the port 31052 is marked as 'filtered' while skype's port is marked as 'open':
PORT STATE SERVICE
31051/tcp open unknown
31052/tcp filtered unknown
I didn't find any other settings at my router that skype could be changing like a firewall rule to make the port open.
I've already tested some apps like 'Droid UPnP Port Mapper' (android app) and 'UPnP PortMapper' (windows app) but all of them sucessfully create a port mapping inside my router but none of them let the port 'open'.
My router is not behind a double NAT since my external ip given by http://www.ipfingerprints.com/ is 189.xx.xxx.xxx and also is my router WAN IP.

getting ip address from host name in BlackBerry 6

Please let me know whether there is way to get the IP address of server from it's domain name.
for example:
Domain name is : http://www.gmail.com
then i want api like
public String getIPAddress(String hostname)
{
Some code here
return ipAddress;
}
I am using Blackberry 6 api, which does not have InetAddress class.
I dont think there is an API available in BB
http://supportforums.blackberry.com/t5/Java-Development/is-there-a-way-to-get-the-remote-ip-address-given-its-hostname/td-p/171164

Get Client Machine ID

I need to get Client's Machine ID and their Country in my web application...
Is it possible get succeed in this?
using System.Globalization;
string culture = CultureInfo.CurrentCulture.EnglishName;
string country = culture.Substring(culture.IndexOf('(')
+ 1, culture.LastIndexOf(')') - culture.IndexOf('(')-
Client Country in C#
Get Client Computer Name,
How to get the client machine name from a server
You will get most of the details of
the client machine using the
"Request.ServerVariables"
// Try the following C# code
System.Net.IPHostEntry host = new System.Net.IPHostEntry();
host = System.Net.Dns.GetHostByAddress(Request.ServerVariables["REMOTE_HOST"]);
lbl.Text = host.HostName;
Host name:
Request.ServerVariables["REMOTE_HOST"]
See http://www.w3schools.com/asp/coll_servervariables.asp
Resolve country:
public static RegionInfo ResolveCountry()
{
CultureInfo culture = ResolveCulture();
if (culture != null)
return new RegionInfo(culture.LCID);
return null;
}
from http://madskristensen.net/post/Get-language-and-country-from-a-browser-in-ASPNET.aspx
This uses the PC's setup Language/Country.
By IP try an example at:
http://dotnetguts.blogspot.com/2008/06/finding-country-from-visitors-ip-in.html
Which involves checking the requesting IP adresses against a database of IP locations.
You could also use an IP for a service that already supports this such as:
http://www.ipgeo.com/api/

Why am I getting ::1 as the IP address in ASP.NET, and how to get the proper IP address?

I am running an ASP.NET MVC app in the localhost - dev server given with a Visual Studio. I want to get the IP address. I tried
Request.UserHostAddress
and
Request.ServerVariables("REMOTE_ADDR")
In both cases, I am getting::1 as a result. What is it? Why am I getting it? How can I get 127.0.0.1 or 192.168.1.xxx?
You are getting a valid IP Address; ::1 is localhost for IPv6.
What you're seeing when calling 'localhost' is valid. ::1 is the IPv6 loopback address. Equivalent to 127.0.0.1 for IPv4.
Instead of calling:
http://localhost/...
Call:
http://{machinename}/...
or
http://127.0.0.1/...
or
http://192.168.1.XXX/...
[Replace {machinename} with your machine's computer name. Replace XXX with your computer's IP address.]
Anyone calling into your machine to the MVC app will have their valid IP address as a result. If the client is an IPv6 host it will save their IPv6 IP address. If the client is an IPv4 host it will save their IPv4 IP address.
If you always want to save an IPv4 address take a look at this article on how they accomplished it with a simple class https://web.archive.org/web/20211020102847/https://www.4guysfromrolla.com/articles/071807-1.aspx. You should be able to take their example and build a quick helper method to accomplish this.
Request.Params["REMOTE_ADDR"]
instead of Request.ServerVariables("REMOTE_ADDR")
If you want localhost return 127.0.0.1, maybe you need to change your "hosts" file.
You can find it in "%systemdrive%\Windows\System32\drivers\etc"
It works for me, now I get 127.0.0.1 with "Request.ServerVariables["REMOTE_ADDR"]". I uncomment 127.0.0.1 (remove #).
Here you can find default hosts file
http://support.microsoft.com/kb/972034
My file
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
below code i have used for finding ip
public static string GetIp()
{
var Request = HttpContext.Current.Request;
try
{
Console.WriteLine(string.Join("|", new List<object> {
Request.UserHostAddress,
Request.Headers["X-Forwarded-For"],
Request.Headers["REMOTE_ADDR"]
})
);
var ip = Request.UserHostAddress;
if (Request.Headers["X-Forwarded-For"] != null)
{
ip = Request.Headers["X-Forwarded-For"];
Console.WriteLine(ip + "|X-Forwarded-For");
}
else if (Request.Headers["REMOTE_ADDR"] != null)
{
ip = Request.Headers["REMOTE_ADDR"];
Console.WriteLine(ip + "|REMOTE_ADDR");
}
return ip;
}
catch (Exception ex)
{
Log.WriteInfo("Message :" + ex.Message + "<br/>" + Environment.NewLine +
"StackTrace :" + ex.StackTrace);
}
return null;
}
If you get ipv6 address , after that you can find it valid ipv4 address map for ipv6 address.
c # code is below;
public static string GetIP4Address(string ip) {
try {
var hostNames = Dns.GetHostEntry(ip);
var ipv4 = hostNames.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
if(ipv4 != null) {
return ipv4.ToString();
}
} catch(Exception ex) {
log.WarnFormat("Error When Getting Client Ipv4");
}
return ip;
}

Resources