I tried to create TcpClients and TcpServers in my C# program using VSPE COM service. Everything seemed okay except the TcpServer/TcpClient created took so much CPU time when running-- 2 of them can take 100% of my cpu time. Anyone can help?
Server creation:
string comName = "COM2";
string tcpPortNum = "5555";
string ipAddr = LocalServerIpAddress.ToString();
string initString = tcpPortNum + ";" + comName.Remove(0, 3) + ";19200,0,8,1,0,0;" + ipAddr + ";0;0";
// Create
int result = vspe.vspe_createDevice("TcpServer", initString);
Client Creation:
string comName = "COM3";
string tcpPortNum = "5555";
string ipAddr = remoteServerIpAddr.ToString();
string initString = ipAddr + ";" + tcpPortNum + ";" + comName.Remove(0, 3) + ";19200,0,8,1,0,0;0;30;;0;0";
// Create
int result = vspe.vspe_createDevice("TcpClient", initString);
Related
I need to convert an MAc Address - 88:E9:FE:A8:1F:2F
for this format '0xA8FEE988 0x00002F1F 0x00000000 0x00000000'
I did this code below, but it is not elegant. Someone can help me?
string txtMacAddr = "88:E9:FE:A8:1F:2F";
string cmdMAc = "";
var macReverso = Util.Mac_Reverso(txtMacAddr.Replace(":",""));
string[] macRev = new string[4];
macRev[0] = $"0x{macReverso.Substring(4, 8)}";
macRev[1] = $"0x0000{macReverso.Substring(0, 4)}";
macRev[2] = "0x00000000";
macRev[3] = "0x00000000";
foreach (var xl in macRev)
{
cmdMAc += xl + " ";
}
Logger.WriteLine(cmdMAc); //op '0xA8FEE988 0x00002F1F 0x00000000 0x00000000'
public static string Mac_Reverso(string macAddress)
{
string macRevertido = string.Empty;
string s = macAddress.Replace("0x", "");//'0xA8FEE988 0x00002F1F 0x00000000 0x00000000'
string[] macLista = s.Split(' ');
foreach (var mac in macLista)
{
for (var i = mac.Length; i > 0; i -= 2)
{
macRevertido += mac.Substring(i - 2, 2);
if (macRevertido.Length == 12)
{
return macRevertido; //2F1FA8FEE988
}
}
}
return macRevertido;
}
What do you mean by not elegant? The following code will get the same result and in my opinion its easier to read. The first option is to show it can be done just by using char references, if the mac address will always look the same. The second result is easier to read in my head.
Note: If 88 was changed to 08 and the zero was omitted for some reason, the the following input would break both of them: 8:E9:FE:A8:1F:2F Does this case matter to you?
Is the output you included in your post what you are trying to get? What exactly are you trying to do?
//Reference each char individually. Could break if char position changes.
string mac = "88:E9:FE:A8:1F:2F";
string reverseMac = "0x" + mac[9] + mac[10] + mac[6] + mac[7] + mac[3] + mac[4] + mac[0] + mac[1] + " 0x0000" + mac[15] + mac[16] + mac[12] + mac[13] + " 0x00000000 0x00000000";
//Output:0xA8FEE988 0x00002F1F 0x00000000 0x00000000
//Better Option: Split by the ':' delimiter and reference each group of chars.
string[] macChars = mac.Split(":"); //Breaks into groups
string newMac2 = "0x" + macChars[3] + macChars[2] + macChars[1] + macChars[0] + " 0x0000" + macChars[5] + macChars[4] + " 0x00000000 0x00000000";
//Output: 0xA8FEE988 0x00002F1F 0x00000000 0x00000000
Update: Based on what you are trying to do, two methods would be better. You could reuse your Mac_Reverso in place of RevertMac below.
//Input 88:E9:FE:A8:1F:2F
//Output 0xA8FEE988 0x00002F1F 0x00000000 0x00000000
public static string ConvertMac(string macAddress)
{
string[] macChars = macAddress.Split(":");
string macRevertido = "0x" + macChars[3] + macChars[2] + macChars[1] + macChars[0] + " 0x0000" + macChars[5] + macChars[4] + " 0x00000000 0x00000000";
return macRevertido;
}
//Input 0xA8FEE988 0x00002F1F 0x00000000 0x00000000
//Output 88:E9:FE:A8:1F:2F
public static string RevertMac(string mc)
{
string revertedMac = "" + mc[8] + mc[9] + ":" + mc[6] + mc[7] + ":" + mc[4] + mc[5] + ":" + mc[2] + mc[3] + ":" + mc[19] + mc[20] + ":" + mc[17] + mc[18];
return revertedMac;
}
This doesn't address the endian issues Neil mentioned. Your question mentions reversing bytes, and your output format implies reversing the order of bytes in the array. Is that your objective?
I made script task that's downloading and saving on disk two spreadsheets from Google Drive using file ID and prepared URL address.
This is main() from my C# code, there are no things outside of it:
public void Main()
{
string m_FileId = Dts.Variables["User::varFileId"].Value.ToString();
string m_RemoteUrl = "https://docs.google.com/spreadsheets/d/" + m_FileId + "/export?format=xlsx";
string m_FilePath = null;
WebClient client = new WebClient();
try
{
m_FilePath = Dts.Variables["User::varFilePath"].Value.ToString() + Dts.Variables["User::varFileName"].Value.ToString();
client.DownloadFile(new System.Uri(m_RemoteUrl), m_FilePath);
m_FilePath = "";
m_FileId = Dts.Variables["User::varFileId2"].Value.ToString();
m_RemoteUrl = "https://docs.google.com/spreadsheets/d/" + m_FileId + "/export?format=xlsx";
m_FilePath = Dts.Variables["User::varFilePath"].Value.ToString() + Dts.Variables["User::varFileName2"].Value.ToString();
client.DownloadFile(new System.Uri(m_RemoteUrl), m_FilePath);
}
catch(Exception e)
{
Dts.Events.FireError(0, "FileDownload", e.Message
+ "\r" + e.StackTrace
+ " \rUrl: " + m_RemoteUrl
+ " \rFilePath: " + m_FilePath
+ " \rPath: " + Dts.Variables["User::varFilePath"].Value.ToString()
+ " \rFileName2: " + Dts.Variables["User::varFileName2"].Value.ToString()
, string.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Problem occurs exactly on every second time I run this code and I don't know how to get rid of it. There's just exception in my script task. I'm printing all variables that are used in this code, and as you can see there's something wrong with m_FilePath, it's like multiplied despite of being printed just once.
[FileDownload] Error: An exception occurred during a WebClient request.
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at ST_84b63d1593dd449886eb2b32dff40b2d.ScriptMain.Main()
Url: https://docs.google.com/spreadsheets/d/----------/export?format=xlsx
FilePath: C:\Google Drive extract\ga_manual_cost_file.xlsxC:\Google Drive extract\ga_manual_cost_file.xlsx
Path: C:\Google Drive extract\ga_manual_cost_file.xlsx
FileName2: ga_manual_cost_file.xlsx
SSIS variables that I'm using are ReadOnly, and are used only in this script task(I tried running only this part of control flow), and their values are as follows:
A WLAN can have more than one BSSID. I need to discovery which BSSID I am connected with.
In the NativeWIFI API using the struct WlanAvailableNetwork I can discover the WLAN that I am connected to, but it didn't tell me which is the connected BSSID.
In the Struct WlanBssEntry I can get all the BSSID of any WLAN, but i also can't discover which one I am connected.
All that I need is the MAC addrees of the WLAN I am connected with. That's why I want to discover which BSSID I am connected with.
I already discovered it...
I will post here my solution in case anyone needs it in the future...
using NativeWifi;
public void GetLog(int Count)
{
string Conectividade = "Disc";//Initi variable Conectividade as Disconnected
string RRate = "0";//Initi variable RRate as 0
string TRate = "0";//Initi variable TRate as 0
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)// Get the WLANs available
{
Wlan.WlanAssociationAttributes conAttributes = wlanIface.CurrentConnection.wlanAssociationAttributes;//Get the Attributes of current connection
string ConnectedSSID = Encoding.ASCII.GetString(conAttributes.dot11Ssid.SSID).ToString();//Save the SSID of WLAN connected with
string ReceivedRate = conAttributes.rxRate.ToString();//Save the receipted rate of the connected WLAN
string TransmitededRate = conAttributes.txRate.ToString();//Save the transmitted rate of the connected WLAN
byte[] ConnectedMacAddr = conAttributes.dot11Bssid;//MAC of the BSSID in which the WLAN is connected with
string ConMac = "";
for (int i = 0; i < ConnectedMacAddr.Length; {
ConMac += ConnectedMacAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();//ConMac sera o MAC da BSSID conectada
}
Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList();//Vector with the BSS available
Wlan.WlanAvailableNetwork[] wlanAvailableNetwork = wlanIface.GetAvailableNetworkList(0);//Vector with the WLANS available
WriteLog("\"ID" + listSeparatorQuotes + "DateTime" + listSeparatorQuotes + "SSID" + listSeparatorQuotes + "MAC" + listSeparatorQuotes + "Type" + listSeparatorQuotes + "Auth" + listSeparatorQuotes + "Cipher" + listSeparatorQuotes + "Connection" + listSeparatorQuotes + "RecivRate" + listSeparatorQuotes + "TransmiRate" + listSeparatorQuotes + "SignalQuality" + listSeparatorQuotes + "NumberOfBSSIDS\"", path, "WLANs" + StartDay + StartHour + ".csv");
foreach (Wlan.WlanAvailableNetwork AVnetwork in wlanAvailableNetwork)
{
string SSIDatual = Encoding.ASCII.GetString(AVnetwork.dot11Ssid.SSID).ToString();//Actual SSID
if(SSIDatual.Equals(ConnectedSSID))
{
Conectividade = "Con";
RRate = ReceivedRate;
TRate = TransmitededRate;
}
//___________________________ wlanAvailableNetwork ___________________________
WriteLog(Count.ToString() + listSeparator + System.DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + listSeparator + SSIDatual + listSeparator + ConMac + listSeparator + AVnetwork.dot11BssType + listSeparator + AVnetwork.dot11DefaultAuthAlgorithm + listSeparator + AVnetwork.dot11DefaultCipherAlgorithm + listSeparator + Conectividade + listSeparator + RRate + listSeparator + TRate + listSeparator + AVnetwork.wlanSignalQuality + listSeparator + AVnetwork.numberOfBssids + "\"", path, "WLANs" + StartDay + StartHour + ".csv");//Its a function that's writes a log in the selected path...case you need this function send me a message!
//_________________________ End wlanAvailableNetwork _________________________
Conectividade = "Disc";//Reinitialize the value of Conectividade RRate = "0";//Reinitialize the value of RRate
TRate = "0";//Reinitialize the value of TRate
}
foreach (Wlan.WlanBssEntry network in wlanBssEntries)// Get all existent BSSIDs
{
int rss = network.rssi;
byte[] macAddr = network.dot11Bssid;
string tMac = "";
for (int i = 0; i < macAddr.Length; i++)
{
tMac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();
}
//___________________________ wlanBSSEntries __________________________
WriteLog(Count.ToString() + listSeparator + System.DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + listSeparator + System.Text.ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString() + listSeparator + network.dot11BssType + listSeparator + network.chCenterFrequency + listSeparator + network.linkQuality + listSeparator + rss.ToString() + listSeparator + tMac, path, "BSSIDs" + StartDay + StartHour + ".csv");
//_________________________ End wlanBSSEntries _________________________
}
Console.ReadLine();
}
}
OBS: I'm writing the LOG as a .csv file, and I'm using culture to get the items separator, this way it will work in any place and culture... for example in Brazil and German the item's separator is a ";", but in USA the separator is a ",". It will adapt for any situation...
Here is the code to get the culture and the separation...
using System.Globalization;
private static string listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
private static string listSeparatorQuotes = "\"" + listSeparator + "\"";
I need to update the Expiry Date and update the Cardholder Name on an existing card in Realex payments.
The hash value syntax should be in the following format:
Timestamp.merchantID.payerref.ref.expirydate.cardnumber
And here is an example of how it should look
20030516175919.yourmerchantid.mypayer.card01.1015.
When I run the following method I get the error:
"sha1hash incorrect - check your code and the Developers Documentation"
private string ReturnHash(string timeStamp, string merchantId, string payerRef, string reference, string expDate, string cardNum )
{
SHA1 hash = new SHA1Managed();
StringBuilder builder = new StringBuilder();
builder.Append(timeStamp).Append(".");
builder.Append(merchantId).Append(".");
builder.Append(payerRef).Append(".");
builder.Append(reference).Append(".");
builder.Append(expDate).Append(".");
builder.Append(cardNum );
string resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));
resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(resultingHash)));
return resultingHash;
}
What am I doing wrong?
Thank you for your message.
Could you try before running this line of code:
string resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));
To make "resultingHash" all lowercase?
Also before running:
resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(resultingHash)));
make "resultingHash" to lowercase as well.
Thanks,
Borja
var timeStamp = RealexDateFormatter.DateFormatForRealex();
var orderid = model.ORDER_ID;
var secret = ConfigurationManager.AppSettings["Appsecretkey"];
var merchantId = ConfigurationManager.AppSettings["AppMerchantId"];
var temp1 = FormsAuthentication.HashPasswordForStoringInConfigFile(
timeStamp + "." +
merchantId + "." +
orderid + "." +
model.AMOUNT + "." + "EUR", "sha1");
temp1 = temp1.ToLower();
var temp2 = temp1 + "." + secret;
var sha1hash = FormsAuthentication.HashPasswordForStoringInConfigFile(temp2, "sha1");
sha1hash = sha1hash.ToLower();`enter code here`
var url = "https://hpp.sandbox.realexpayments.com/pay?MERCHANT_ID="
+ ConfigurationManager.AppSettings["AppMerchantId"] +
"&ORDER_ID=" + orderid + "&CURRENCY=EUR" + "&AMOUNT=" + model.AMOUNT + "&TIMESTAMP=" + timeStamp + "&SHA1HASH=" + sha1hash + "&MERCHANT_RESPONSE_URL=http://deposit.projectstatus.in/Payment/Response";
i am having problem that this following script generates Email address when page is loaded and i want to parse that email how can i do that?
tr>
<td align='right' class='generalinfo_left' >Email Address:</td>
<td class='generalinfo_right'><script type="text/javascript">
//<![CDATA[
var o3752aaa9bb29d904adeb88838117fd7c = String.fromCharCode(109);var f03de7e643c296e211edddbc3197b33f6 = String.fromCharCode(97);var k7c3bf82468602c0f8dff4950e4b6ff1e = String.fromCharCode(105);var b3eaa633e44451be8df1fa47d75149934 = 'l';var ma2fa16c3a3f532b780aaf0fa5a5b75c6 = 't';var re0c13fc69c03925782867a0540f8c084 = 'o';var j335f1365672123d1fcaf9a83b76f1b7b = String.fromCharCode(58);var f32820e1c54cbc3fa0d418cd1c195eaec = String.fromCharCode(105);var y8c24ea00a7a1edf1c01f794d487697e3 = String.fromCharCode(110);var bcc0ad4f628e703f9ff6e25b87b77ec34 = 'f';var c985c961c7ee85fe6a25d5a66fb421745 = String.fromCharCode(111);var z5ab4e3bdc353d621cea5babcc5dca417 = String.fromCharCode(64);var s4e087167cd0bac466344e72016511172 = String.fromCharCode(97);var re26f6ae180723793af62bc36d5ab2530 = String.fromCharCode(108);var ye1b53d01de118079a38de5e951586731 = 'c';var g9fc5710c9266ce08afbe4da24702dfdd = String.fromCharCode(105);var k5cd5ea1bac40fdbb8b133b7e356809c6 = String.fromCharCode(118);var fcd6e4771e956e270c6897d24ca51c256 = String.fromCharCode(97);var y9d7854a5921fa2be88c8cd72c7e2884e = String.fromCharCode(114);var xa58bea1ecad6fe7d2c736aab1df2df44 = '.';var e4569f6c98804675f7117a84abb0b8d5c = 'c';var o4d2081e2344020922dcb924690c9972e = 'o';var af150185e5eef8ecd8dc1b0a4977c7d55 = String.fromCharCode(109);document.write("<a href='" + o3752aaa9bb29d904adeb88838117fd7c + f03de7e643c296e211edddbc3197b33f6 + k7c3bf82468602c0f8dff4950e4b6ff1e + b3eaa633e44451be8df1fa47d75149934 + ma2fa16c3a3f532b780aaf0fa5a5b75c6 + re0c13fc69c03925782867a0540f8c084 + j335f1365672123d1fcaf9a83b76f1b7b + f32820e1c54cbc3fa0d418cd1c195eaec + y8c24ea00a7a1edf1c01f794d487697e3 + bcc0ad4f628e703f9ff6e25b87b77ec34 + c985c961c7ee85fe6a25d5a66fb421745 + z5ab4e3bdc353d621cea5babcc5dca417 + s4e087167cd0bac466344e72016511172 + re26f6ae180723793af62bc36d5ab2530 + ye1b53d01de118079a38de5e951586731 + g9fc5710c9266ce08afbe4da24702dfdd + k5cd5ea1bac40fdbb8b133b7e356809c6 + fcd6e4771e956e270c6897d24ca51c256 + y9d7854a5921fa2be88c8cd72c7e2884e + xa58bea1ecad6fe7d2c736aab1df2df44 + e4569f6c98804675f7117a84abb0b8d5c + o4d2081e2344020922dcb924690c9972e + af150185e5eef8ecd8dc1b0a4977c7d55 + "'>" + f32820e1c54cbc3fa0d418cd1c195eaec + y8c24ea00a7a1edf1c01f794d487697e3 + bcc0ad4f628e703f9ff6e25b87b77ec34 + c985c961c7ee85fe6a25d5a66fb421745 + z5ab4e3bdc353d621cea5babcc5dca417 + s4e087167cd0bac466344e72016511172 + re26f6ae180723793af62bc36d5ab2530 + ye1b53d01de118079a38de5e951586731 + g9fc5710c9266ce08afbe4da24702dfdd + k5cd5ea1bac40fdbb8b133b7e356809c6 + fcd6e4771e956e270c6897d24ca51c256 + y9d7854a5921fa2be88c8cd72c7e2884e + xa58bea1ecad6fe7d2c736aab1df2df44 + e4569f6c98804675f7117a84abb0b8d5c + o4d2081e2344020922dcb924690c9972e + af150185e5eef8ecd8dc1b0a4977c7d55 + "</a>")
//]]>;
</script></td>
out put is like this
<td class="generalinfo_right">
<script type="text/javascript">
same above script plus following Line
</script>someID#email.com</td>
I wrote my own custom parser that will read the script and parse Email from it.here goes the code
If this code can be optimized or can be written more neatly please let me know
private string ReadEmail(string EmailScript)
{
string EncriptedEmail = "";
string dataPart = "";
dataPart = EmailScript.Substring(0, EmailScript.IndexOf("document.write")).Replace("//<![CDATA[\r", "").Replace("\"", "").Replace("\r\n","");
EncriptedEmail = EmailScript.Replace("\"","");
EncriptedEmail = EncriptedEmail.Substring(EncriptedEmail.IndexOf("'> + "), EncriptedEmail.IndexOf(" + </a>") - EncriptedEmail.IndexOf("'> +")).Replace("'> +", "").Trim();
string[] requiredVariables = EncriptedEmail.Split('+');
List<string> ExtractedDataFromRaw = new List<string>();
string email = "";
foreach (string variable in requiredVariables)
{
string temp = dataPart.Substring(dataPart.IndexOf(variable),dataPart.Length-dataPart.IndexOf(variable)).Replace(" ","");
string tempValueofVariable = temp.Substring(0, temp.IndexOf(";"));
tempValueofVariable = tempValueofVariable.Substring(tempValueofVariable.IndexOf("="), tempValueofVariable.Length - temp.IndexOf("=")).Replace("=","");
if (tempValueofVariable.Contains("String.fromCharCode"))
{
tempValueofVariable = GetCharacterFromASCII(tempValueofVariable.Replace("String.fromCharCode(", "").Replace(")", ""));
}
ExtractedDataFromRaw.Add(tempValueofVariable.Replace("'",""));
email += tempValueofVariable.Replace("'", "");
}
return email;
}
private string GetCharacterFromASCII(string value)
{
int result = 0;
int.TryParse(value, out result);
return char.ConvertFromUtf32(result);
}
That code is building the email address one character at a time from character codepoints and then assembling it later. I suppose this is an attempt to prevent email spam. Depending on what you need to do, it might be easiest to just pull the email address from the link using jQuery or something. $('a[href^=mailto]').attr('href').substring(7) or something ought to do it.