unable to connect remote server using c# .net - c#

I am using the below code snippet while trying to connect to remote server in visual studio, but it is not working, please suggest.
private void bttnConnect_Click(object sender, EventArgs e)
{
try
{
RDP_client.Server = txtServerName.Text;
RDP_client.UserName = txtUserName.Text;
IMsTscNonScriptable secured = (IMsTscNonScriptable)RDP_client.GetOcx();
secured.ClearTextPassword = txtPassword.Text;
RDP_client.Connect();
if (RDP_client.Connected.ToString() == "1")
{
MessageBox.Show("connected");
}
else
{
MessageBox.Show("not connected");
}
}
catch (Exception ex)
{
MessageBox.Show("unable to connect"+ex);
}
}

Related

IPWorks WebSockets 2020 .NET Edition c# - How to control the username and password on the server side?

Based on IPWorks WebSockets 2020 .NET Edition - Demo Application, all connection requests are accepted. The following code:
private void wsserver1_OnConnectionRequest(object sender, WebsocketserverConnectionRequestEventArgs e)
{
tbLog.AppendText("Connection request from " + e.Address + ". Accepting.\r\n");
e.Accept = true;
}
I added the username and password to the client side code as below:
private void bConnect_Click(object sender, System.EventArgs e)
{
bConnect.Enabled = false;
try
{
if (bConnect.Text == "Connect")
{
websocketclient1.Timeout = 10;
websocketclient1.InvokeThrough = this;
websocketclient1.User = "ali";//****
websocketclient1.Password = "ali";//****
websocketclient1.Connect(tbServer.Text);
bConnect.Text = "Disconnect";
}
else
{
websocketclient1.Disconnect();
}
}
catch (IPWorksWSException ipwse)
{
MessageBox.Show(this, ipwse.Message, "Error", MessageBoxButtons.OK);
bConnect.Text = "Connect";
bConnect.Enabled = true;
}
}
How to control username and password on server side? i.e:
if (username=="ali" && password=="ali") e.Accept = true;
How to get a username and password on the server side when OnConnectionRequest fired?

Unable to open sql database error - to read lines into datagridview

I have an application that opens a database from a user inputted path, the user then scans a barcode into a textbox and the app should search for this number in the database and return the corresponding row of data. Currently the app works perfectly for Access databases using oleDB and I am writing the code for accessing sqlite databases using sqLite but I am getting an error message from the code Error : unable to open the database. Below is the code, any help appreciated as I am not overly familiar with databases!
private void txtScannedValue_TextChanged(object sender, EventArgs e)
{
if (txtScannedValue.Text.Length == 15)
{
try
{
string dbPath = databasePathBox.Text;
string extension = Path.GetExtension(dbPath);
//open database to read
if (extension == ".mdb" || extension == ".accdb")
{
queryAccessDB(dbPath);
}
else if (extension == ".db")
{
querySqlDB(dbPath);
}
else
{
MessageBox.Show("database extension not recognised");
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
}
public void querySqlDB(string sqlDBpath)
{
// create a new database connection:
string cnctstring = string.Format("data source={0};version=3;new=true;read only=true;", sqlDBpath);
SQLiteConnection sqlite_conn = new SQLiteConnection(cnctstring);
// open the connection:
sqlite_conn.Open();
populateDGVfromSQl(sqlite_conn);
sqlite_conn.Close();
}
public void populateDGVfromSQl(SQLiteConnection sqlite_conn)
{
try
{
SQLiteCommand sqlite_cmd = sqlite_conn.CreateCommand();
sqlite_cmd.CommandText = "Select * from Production where IMEINumber=$scanned";
sqlite_cmd.Parameters.AddWithValue("$scanned", txtScannedValue.Text);
SQLiteDataReader sqlite_datareader = sqlite_cmd.ExecuteReader();
while (sqlite_datareader.Read())
{
DataTable dt = new DataTable();
dt.Load(sqlite_datareader);
if (dt.Rows.Count > 0)
{
if (dataGridView1.DataSource != null)
{
((DataTable)dataGridView1.DataSource).ImportRow(dt.Rows[0]);
}
else
{
dataGridView1.DataSource = dt;
}
}
else
{
MessageBox.Show("No Data Found");
}
//reset textBox
txtScannedValue.Text = "";
}
sqlite_conn.Close();
}
catch(SqlException ex)
{
MessageBox.Show($"Can not open connection ! ErrorCode: {ex.ErrorCode} Error: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show("Error 3:" + ex.Message);
}
}

GCP Computer Engine disconnect me c# MSTSCLib

I created an RDP client in c# and when I try to connect to a GCP instance GCP Computer this its disconect me instant I using MSTSCLib.
I used this program with other servers and only with GCP instances I have this problem.
try
{
rdp.Server = "ip";
rdp.UserName = "user";
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = "password";
rdp.Connect();
}
catch (Exception Ex)
{
MessageBox.Show("Error Connecting", "Error connecting to remote desktop " + " Error: " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
disconnect function
private void Rdp_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
{
this.close();
}

Receiving continuous data using 32feet in .Net (BLUETOOTH)

I am creating a Windows Form application, where it is connecting to a device through bluetooth. I am able to send commands to the device and I am receiving the data continuously. The problem I am facing is that I am not able to show the continuous data in the text box. The text box only shows the first line of characters the application is receiving. Here is my code:
CONNECT BUTTON ACTION:
private void btnConnect_Click(object sender, EventArgs e)
{
if (listBox.SelectedItem != null)
{
lblProgress.Text = "";
btnStart.Enabled = true;
cBoxAvailablePorts.Enabled = cBoxAvailableBaudRates.Enabled = true;
try
{
int pos = listBox.SelectedIndex;
deviceInfo = array.ElementAt(pos);
if (pairDevice())
{
Thread thread = new Thread(() => connectThread());
thread.Start();
}
else
{
MessageBox.Show("Pair failed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("Please connect to a device!");
}
}
THREAD ACTION
private void connectThread()
{
//BluetoothClient client = new BluetoothClient();
bc.BeginConnect(deviceInfo.DeviceAddress, serviceClass, this.connectCallBack, bc);
}
CALLBACK ACTION:
private void connectCallBack(IAsyncResult result)
{
//BluetoothClient client = (BluetoothClient)result.AsyncState;
try
{
if (bc.Connected)
{
MessageBox.Show("Connected!");
}
else
{
MessageBox.Show("Connection Failed!");
}
}
catch (Exception)
{
MessageBox.Show("Not able to identify Bluetooth devices! Please try again.!");
}
}
START BUTTON ACTION:
Here I send a command "S".
In button action I call sendMessage("S").
The function that is called is shown below:
public void sendMessage(string msg)
{
try
{
if (bc.Connected)
{
Stream stream = bc.GetStream();
stream.ReadTimeout = 1000;
StreamWriter streamWriter = new StreamWriter(stream);
streamWriter.WriteLine(msg);
streamWriter.Flush();
// Read operation
StreamReader streamReader = new StreamReader(stream);
string result = streamReader.ReadLine();
txtResult.Text = result;
}
else
{
MessageBox.Show("Sending failed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
I wrote the StreamReader part in a loop, and it gave me Socket Exception.
I also tried to get the data from Serial Port and used DataReceived event just in case, but still it didn't help.
Any help would be appreciated.
Thank you!
OKAY! I solved the problem. Without getting in trouble with 32feet library (though it is fun to code with 32feet), I thought to make communication through serial port. I connected the device with my laptop and got to know the outgoing COMPORT in bluetooth setting of my laptop. The two-way communication can only be done through outgoing COMPORT, not the incoming COMPORT.
Suppose the outgoing COMPORT is COM12 and the baud rate that I have set is 9600.
So here is my code:
public delegate void updateDelegate(string text);
private updateDelegate objDelegate;
private SerialPort serialPort;
public View() // constructor
{
InitializeComponent();
this.WindowState = FormWindowState.Normal;
this.StartPosition = FormStartPosition.CenterScreen;
this.objDelegate = new updateDelegate(getText);
serialPort = new SerialPort("COM12", 9600);
serialPort.Handshake = Handshake.None;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DtrEnable = true;
serialPort.RtsEnable = true;
}
START BUTTON ACTION
private void btnStart_Click(object sender, EventArgs e)
{
sendData("S");
}
// SEND COMMAND
public void sendData(string msg)
{
try
{
if (!serialPort.IsOpen)
{
serialPort.Open();
//serialPort.Close();
}
if (serialPort.IsOpen)
{
serialPort.Write(msg);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// READ DATA
public void readData()
{
try
{
serialPort.DataReceived += SerialPort_DataReceived;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string res = serialPort.ReadExisting();
Thread.Sleep(500);
txtResult.Invoke(this.objDelegate, new object[] {res});
}
public void getText(string text)
{
txtResult.Text = text;
}
I hope this will help someone! Thank you!!!

Cannot connect to local openfire server from C#

I've setup a openfire server in a vm, and its accessible from internet(I've done the port-forwarding to point to the ip of the vm).
I can chat using spark so openfire is working perfectly.
Now I am trying to connect my c# app to the server and I am getting error.
Code :
private void button1_Click(object sender, System.EventArgs e)
{
try
{
using (var client = new XmppClient("192.168.0.109", "pbc92", "12345"))
{
try
{
client.Connect();
client.StatusChanged += client_StatusChanged;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void client_StatusChanged(object sender, S22.Xmpp.Im.StatusEventArgs e)
{
MessageBox.Show(e.Jid.ToString());
}
The error :
Try with Sharp.Xmpp, it is a fork of the currently frozen S22.Xmpp.
https://github.com/pgstath/Sharp.Xmpp

Categories

Resources