Send command to a device over internet by using its IP address - c#

Currently I am working on a project in which I have to send commands to a device by calling its IP address. I have already created a code that send the command by using the IP address on LAN. Here is code snippets.
private void cmdConnectRobot_Click(object sender, EventArgs e)
{
try
{
// Create object and connect with VRC
caoCtrl = caoWs.AddController("Robot", "CaoProv.DENSO.RC8", "", "Server=192.168.0.100");
caoRobot = caoCtrl.AddRobot("VS", "");
// Valiable of status of Machine lock SW
//caoVal = caoCtrl.AddVariable("#LOCK","");
caoValP = caoCtrl.AddVariable("P1", "");
you can see the robot can be accessed using LAN by putting server IP address. I want to call this device by using this IP address but over the internet.
Thank you!

Related

How to connect to a server if its IP address is accessed from a database

So, I'm trying to make a chat application where when you are able to select what IP address you want to connect to. These IP addresses are stored in a database and for whatever reason when I extract the IP from a database the program doesn't connect to it, whereas when I directly give the IP in the serverIP variable as: serverIP = "127.0.0.1"; it works. I have no issues extracting the IP from the database, the problem is that when you try to connect to the server with the extracted IP it fails.
This is the code I used to use to connect to a server using an IP from the database:
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Address WHERE ID = 1;", conn); //query
OleDbDataReader cusReader = cmd.ExecuteReader();
while (cusReader.Read())
{
ip = cusReader.GetValue(0).ToString();
}
serverIP = ip;
cusReader.Close();
TcpClient client = new TcpClient(serverIP, port);
If you want the ip column, then specify it in the SQL statement:
SELECT ip FROM Address WHERE ID = 1;
or explicitly look up the column by name when you read the results. I'm not sure a while loop is appropriate if you are expecting at most one row.

Pinging an IP and port with C# [duplicate]

I have to check remote IP and Port is available or not.If its is available it will move to next form.If not available it should come to the initial state.I tried using this
while (true)
{
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
-------
-------
-------
}
I am showing the example coding.it was checking local IP and port and moving to next form.it will check local port and IP is available.if port and IP not available it will come to the initial stage and it was working fine.same thing i have to check in remote Port and IP.
Use the Ping class of .NET to find out if the system is up and connected, the use the PortScanner to check if the port is open. check these links for further reading and exploring.
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs.110%29.aspx
http://social.msdn.microsoft.com/Forums/vstudio/en-US/8e4410bd-307f-4264-9575-cd9882653945/help-with-portscanner-in-c?forum=csharpgeneral
OR
public static bool PingHost(string hostUri, int portNumber)
{
try
{
using (var client = new TcpClient(hostUri, portNumber))
return true;
}
catch (SocketException ex)
{
MessageBox.Show("Error pinging host:'" + hostUri + ":" + portNumber.ToString() + "'");
return false;
}
}

ChilkatDotNet2: connect to server SFTP

In my C# project I have included ChilkatDotNet2.dll in order to connect to a SFTP server. For the moment, I have tried to implemt a simple connection method like the following:
Chilkat.Ftp2 F = null;
F = (Chilkat.Ftp2)new Chilkat.Ftp2();
F.Hostname = "sftp.domain.com";
F.Username = "username";
F.Password = "password";
F.Port = 22;
F.AuthTls = true;
if (!(F.Connect())) {
MainFrm.set_AlertMessage(F.ConnectFailReason);
}
and it always fails connecting and I always get the error 200 which means
Connected, but failed to receive greeting from FTP server.
according to Chilkat documentation.
What am I missing? I have tried to connect to a simple ftp server test (without SSL/TLS) and it connects correctly so I think I am missing something.
The credentials I was given are correct as I tried to connect to the SFTP server with Filezilla.
Thank you.
UPDATE
Chilkat.SFtp v_SFTP = null;
v_SFTP = (Chilkat.SFtp)new Chilkat.SFtp();
if (v_SFTP.Connect("sftp.domain.com", 22) {
if (v_SFTP.AuthenticatePw("username", "password")) {
IDVariant v_SUCCESS = null;
bool v_SUCCESS = v_SFTP.InitializeSftp();
}
}
else {
MainFrm.set_AlertMessage(v_SFTP.LastErrorText);
}
F = (Chilkat.Ftp2)new Chilkat.Ftp2();
Chilkat.Ftp2 is an FTP client. It supports the FTP protocol. SFTP is a completely different protocol which just happens to have a similar name.
If you want to connect to an SFTP server using Chilkat, look at Chilkat.SFtp.

How to check remote IP and Port is available?

I have to check remote IP and Port is available or not.If its is available it will move to next form.If not available it should come to the initial state.I tried using this
while (true)
{
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
-------
-------
-------
}
I am showing the example coding.it was checking local IP and port and moving to next form.it will check local port and IP is available.if port and IP not available it will come to the initial stage and it was working fine.same thing i have to check in remote Port and IP.
Use the Ping class of .NET to find out if the system is up and connected, the use the PortScanner to check if the port is open. check these links for further reading and exploring.
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs.110%29.aspx
http://social.msdn.microsoft.com/Forums/vstudio/en-US/8e4410bd-307f-4264-9575-cd9882653945/help-with-portscanner-in-c?forum=csharpgeneral
OR
public static bool PingHost(string hostUri, int portNumber)
{
try
{
using (var client = new TcpClient(hostUri, portNumber))
return true;
}
catch (SocketException ex)
{
MessageBox.Show("Error pinging host:'" + hostUri + ":" + portNumber.ToString() + "'");
return false;
}
}

C# Listenin 5060 Port [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Listening to Port 5060
I am developing a SIP client.And I have a question.
I want listen 5060 port for catch the SIP Server Message.For this,I coding something.(Also I take admin rights in program)
But I get SocketException: "An attempt was made to access a socket in a way forbidden by its access permissions" (Native error code: 10013)...
My Code:
private void ListenPort() {
WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
TcpListener server = null;
Int32 port = 5060;
IPAddress localAddr = IPAddress.Parse("192.168.1.33");
server = new TcpListener(localAddr, port);
Byte[] bytes = new Byte[1000];
String data = null;
while (hasAdministrativeRight == true)
{
server.Start();
int i = 0;
while (1==1)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
data = null;
i = stream.Read(bytes, 0, bytes.Length);
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
label3.Text += data;
this.Refresh();
Thread.Sleep(500);
}
}
}
Where do you think the problem?
Have you checked that no other program is already using port 5060? That's what this error can mean.
See this discussion on the matter: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/d23d471c-002a-4958-829e-eb221c8a4b76/
You need to call server.Start() outside the while loop and before the first AcceptTcpClient call.
Also try using IPAddress.Any instead of IPAddress.Parse("192.168.1.33") for your listener ip
Make sure any other SIP Server program not installed as a Windows service which has using according port.
Type in netstat -an and it will show you the “listening” ports or try to googling port check softwares.
And check your SIP Server configuration for is it running over TCP or UDP.

Categories

Resources