Usually when a client needs to connect to server it is assumed that a direct connection can be established. Nowadays though, due to security reasons or network topology, it is often necessary to use a proxy or bypass a firewall. This article describes different ways to connect to server with MyDirect .NET.
Direct connection to server means that server host is accessible from client without extra routing and forwarding. This is the simplest case. The only network setting you need is Host parameter of connection string. This is also the fastest and most reliable way of communicating with server. Use it whenever possible.
The following lines illustrate the simplicity:
[C#]
MySqlConnection conn = new MySqlConnection(); conn.ConnectionString = "Host=192.168.0.10;user=root;password=root"; conn.Open();
[Visual Basic]
Dim conn As MySqlConnection() conn.ConnectionString = "Host=192.168.0.10;user=root;password=root" conn.Open()
Sometimes it is necessary to connect to server in another network. For example, client address is 10.0.0.2, server address is 192.168.0.10, and the MySQL server listens on port 3307. The client and server reside in different networks, so the client can reach it only through proxy at address 10.0.0.1, which listens on port 808. In this case in addition to connection string parameter Host you have to setup MySqlConnection.ProxyOptions object as follows:
[C#]
MySqlConnection conn = new MySqlConnection(); conn.ConnectionString = "Host=192.168.0.10;port=3307;user=root;password=root"; conn.ProxyOptions.Host = "10.0.0.1"; conn.ProxyOptions.Port = 808; conn.ProxyOptions.User = "ProxyUser"; conn.ProxyOptions.Password = "ProxyPassword"; conn.Open();
[Visual Basic]
Dim conn As MySqlConnection() conn.ConnectionString = "Host=192.168.0.10;port=3307;user=root;password=root" conn.ProxyOptions.Host = "10.0.0.1" conn.ProxyOptions.Port = 808 conn.ProxyOptions.User = "ProxyUser" conn.ProxyOptions.Password = "ProxyPassword" conn.Open()
Note that setting parameters of MySqlConnection.ProxyOptions automatically enables use of proxy server. Connections through proxy are available when MySqlConnection.Protocol property is Tcp (default) or Http. You cannot use proxy when protocol is Pipe, Memory, Ssl or Ssh.
As usual, you can specify all settings in connection string:
Host=192.168.0.10;Port=3307;Proxy Host=10.0.0.1;Proxy Port=808;Proxy User=ProxyUser;Proxy Password=ProxyPassword;User Id=root;Password=root;
Sometimes client machines are shielded by a firewall that does not allow you to connect to server directly at specified port. If the firewall allows HTTP connections, you can use MyDirect .NET together with HTTP tunneling software to connect to MySQL server.
There are many HTTP tunneling servers available. We use GNU httptunnel, and its .NET port. You can use any compatible HTTP tunneling server.
The main idea of such software is that it creates a bidirectional virtual data connection tunneled in HTTP requests. It accepts the requests on some port, say, 8080, decodes the data and forwards it to some other host and port. Thus you can communicate with any server via HTTP traffic that is usually allowed.
Suppose, server address is 192.168.0.10, and the MySQL server listens on port 3307, which is blocked by a firewall. Assuming that you install HTTP tunneling server on host 192.168.0.1. and it listens on port 8080, forwards traffic to 192.168.0.10:3307. In this case you have to setup MySqlConnection.HttpOptions object as follows:
[C#]
MySqlConnection conn = new MySqlConnection(); conn.ConnectionString = "user=root;password=root"; conn.Protocol = MySqlProtocol.Http; conn.HttpOptions.Host = "192.168.0.1"; conn.HttpOptions.Port = 8080; conn.Open();
[Visual Basic]
Dim conn As MySqlConnection() conn.ConnectionString = "user=root;password=root" conn.Protocol = MySqlProtocol.Http conn.HttpOptions.Host = "192.168.0.1" conn.HttpOptions.Port = 8080 conn.Open()
Note that in this case you did not specify any host information in connection string. This is because MySQL server address and port are provided in HTTP tunneling server configuration. Note also that to enable using HTTP tunnel you have to explicitly state it either in Protocol property (conn.Protocol = MySqlProtocol.Http) or in connection string (protocol=http). All HTTP tunneling options can be specified in connection string or MySqlConnectionStringBuilder object as well.
Consider the previous case with one more complication: HTTP tunneling server is not directly accessible from client machine. Suppose, it can be communicated only via proxy at 10.0.0.1:808. So you have to specify proxy settings together with tunneling options:
[C#]
MySqlConnection conn = new MySqlConnection(); conn.ConnectionString = "user=root;password=root"; conn.ProxyOptions.Host = "10.0.0.1"; conn.ProxyOptions.Port = 808; conn.ProxyOptions.User = "ProxyUser"; conn.ProxyOptions.Password = "ProxyPassword"; conn.Protocol = MySqlProtocol.Http; conn.HttpOptions.Host = "192.168.0.1"; conn.HttpOptions.Port = 8080; conn.Open();
[Visual Basic]
Dim conn As MySqlConnection() conn.ConnectionString = "user=root;password=root" conn.ProxyOptions.Host = "10.0.0.1" conn.ProxyOptions.Port = 808 conn.ProxyOptions.User = "ProxyUser" conn.ProxyOptions.Password = "ProxyPassword" conn.Protocol = MySqlProtocol.Http conn.HttpOptions.Host = "192.168.0.1" conn.HttpOptions.Port = 8080 conn.Open()
Again, connection string misses the host information because MySQL server host and port are known to HTTP tunneling server.
Technically speaking, there is one more way to tunnel network traffic. The Secure Shell forwarding, or SSH, can be used for forwarding data. However, main purpose of SSH is traffic encryption rather than avoiding firewalls or network configuration problems. The article Using Secure Connections describes how to use SSH protocol in MyDirect .NET.
Keep in mind that traffic tunneling or encryption always increases CPU usage and network load. It is recommended that you use direct connection whenever possible.
MySqlConnection Class | Using Secure Connections
© 2002-2008 Devart. All rights reserved.