| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Security;
- using System.Net.Sockets;
- using System.Text;
- namespace ErgometerLibrary
- {
- public class NetHelper
- {
- public static bool SendNetCommand(TcpClient client, NetCommand command)
- {
- /*
- byte[] b = Encoding.Unicode.GetBytes(command.ToString());
- client.GetStream().Write(b, 0, b.Length);
- client.GetStream().Flush();
- */
- return SendString(client, command.ToString());
- }
- public static bool SendString(TcpClient client, string command)
- {
- /*
- byte[] b = Encoding.Unicode.GetBytes(command);
- client.GetStream().Write(b, 0, b.Length);
- client.GetStream().Flush();
- */
- if (client != null && client.Connected)
- {
- try
- {
- StreamWriter wr = new StreamWriter(client.GetStream(), Encoding.Unicode);
- wr.WriteLine(AESEncrypt.EncryptToString(command));
- wr.Flush();
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- else
- return false;
- }
- public static NetCommand ReadNetCommand(TcpClient client)
- {
- /*
- byte[] bytesFrom = new byte[(int) client.ReceiveBufferSize];
- client.GetStream().Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
- string response = Encoding.Unicode.GetString(bytesFrom);
- NetCommand net = NetCommand.Parse(response);
- return net;
- */
- string str;
- NetCommand net;
- str = ReadString(client);
- if (str != "")
- {
- try
- {
- str = AESEncrypt.DecryptString(str);
- }
- catch (Exception e)
- {
- str = "";
- net = new NetCommand(NetCommand.CommandType.ERROR, 0);
- }
- try {
- net = NetCommand.Parse(str);
- }
- catch (Exception e)
- {
- net = new NetCommand(NetCommand.CommandType.ERROR, 0);
- }
- }
- else
- net = new NetCommand(NetCommand.CommandType.ERROR, 0);
- return net;
- }
- public static string ReadString(TcpClient client)
- {
- /*
- byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
- client.GetStream().Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
- string response = Encoding.Unicode.GetString(bytesFrom);
- return response;
- */
- if (client != null && client.Connected)
- {
- try {
- StreamReader rd = new StreamReader(client.GetStream(), Encoding.Unicode);
- string str = rd.ReadLine();
- return str;
- }
- catch(Exception e)
- {
- return "";
- }
- }
- else
- return "";
- }
- public static IPAddress GetIP(string ipstring)
- {
- IPAddress ip;
- bool ipIsOk = IPAddress.TryParse(ipstring, out ip);
- if (!ipIsOk) { return null; }
- return ip;
- }
- public static IPAddress GetIP()
- {
- IPHostEntry host;
- string localIP = "?";
- host = Dns.GetHostEntry(Dns.GetHostName());
- foreach (IPAddress ip in host.AddressList)
- {
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- localIP = ip.ToString();
- }
- }
- return GetIP(localIP);
- }
- }
- }
|