1、启动socket监听,监听测试平台发送的执行信息,启动socket监听的方法:
先判断端口是否占用
/// <summary>
/// 判断端口是占用
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public static bool PortInUse(int port)
{
bool inUse = true;
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
foreach (IPEndPoint endPoint in ipEndPoints)
{
if (endPoint.Port == port)
{
inUse = false;
break;
}
}
return inUse;
}
public void Initialize()
{
// logger.Info("初始化插件,启动socket监听和ftp服务");
ExeCuteJS.callback = callback;
if (PortInUse(SSATProperty.port))
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(SSATProperty.ip), SSATProperty.port);
severSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
severSocket.Bind(ipe);///绑定端口
severSocket.Listen(5);
new Thread(new ThreadStart(SocketListener)).Start();
Logger.Info("ftp上传服务启动");
FtpUploadListener();
PerformanceListenser();
}
}
public void SocketListener()
{
While(true){
Socket socket = severSocket.Accept();//为新建连接创建新的socket
NetworkStream networkStream = new NetworkStream(socket);//读取的流
//设置读取信息的相关参数recBytesLenth:存放读取出来的信息是一个Byte类型的数组;0:recBytesLenth 中开始将数据存储到的位置;size:要从 NetworkStream 中读取的字节数;
networkStream.Read(recBytesLenth, 0, int size);
//将存储信息的byte数组中的文件转换成string类型的文件
Encoding.ASCII.GetString(recBytesLenth, 0, recBytesLenth.Length);
}
}
2、C#中启动ftp上传服务
初始化一个webclient对象
WebClient webClient = new WebClient();
//绑定ftp的用户名密码
webClient.Credentials = new NetworkCredential(SSATProperty.strUser, SSATProperty.strPassword);
//address为ftp服务器的url地址,例如:ftp://localhost/samplefile.txt,file那么为需要上传的文件,例如:samplefile.txt
webClient.UploadFile(address, file);
3、通过ftp上传文件时,不可避免需要在ftp服务器路径下新建路径
https://www.cnblogs.com/big-lll/p/9224476.html
该方法是通过获取路径下的每一个路劲信息,查找是否存在,如果不存在则要新建,需要注意的是若ftp服务器部署在linux服务器中,
新建路径的时候需要加上服务器的默认地址,但是在windows服务器中不需要,会自动创建默认路径
4、C#中的线程启动的方法
(1)启动普通的线程的方法
new Thread(new ThreadStart(SocketListener)).Start();
(2)启动带有参数的方法:
new Thread(new ParameterizedThreadStart(SocketListener)).Start(“B”); //"B"为传入方法SocketListener的参数
5、C#项目可以添加java项目的webservice服务,右击工程,点击“添加”中的“服务引用”按钮,左下角“高级”,左下角“添加web服务”,在url中输入地址http://localhost:8080/AT/services/AgentTransCaseService?wsdl AgentTransCaseService为类名,后面添加“?wsdl”即可引用对应地址中的方法在某一个类中,引入刚刚添加的服务,初始化一个服务对象,就可以调用webservice中的方法了,需要注意的是,在C#中没有和java中的map对应的类型,因此在传输这样类型的变量时,可以将其专程string或者json类型的对象使用。
6、C#获取电脑的内存使用率和cpu使用率
在用下面的方法获取cpu时,第一次获取到的cpu为0%,在第二次获取时才是正常的,并且在在一个循环中获取时,需要设间隔时间,否则在之后的获取过程中,获取的数据有错误(获取到的cpu的使用率不是0就是100)。
public class GetRate
{
PerformanceCounter cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
MEMORY_INFO MemInfo;
public float percentage;
//获取cpu的使用率
public float GetCpuRate()
{
percentage = cpu.NextValue();
Console.WriteLine(percentage + "%的CPU正在使用\n");
return percentage;
}
//获取内存使用率
public float GetMemeryRate()
{
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
return MemInfo.dwMemoryLoad;
}
[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
[DllImport("kernel32")]
public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
//定义CPU的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
//定义内存的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
//定义系统时间的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME_INFO
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
}
7、 await 和 async
在.net中当一个方法被async关键字修饰之后,方法就变成了异步方法,在被async修饰的方法内部通过await修饰另一个被saync修饰的方法,因为被async修饰后又task的返回值的方法执行时必须要加await关键字。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("主线程测试开始..");
AsyncMethod();//这里是异步执行的
Thread.Sleep(1000);
Console.WriteLine("主线程测试结束..");
Console.ReadLine();
}
static async void AsyncMethod()
{
//此方法内部的三个语句还是同步执行的
Console.WriteLine("开始异步代码");
var result = await MyMethod();//
Console.WriteLine("异步代码执行完毕");
}
static async Task<int> MyMethod()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("异步执行" + i.ToString() + "..");
await Task.Delay(1000); //模拟耗时操作
}
return 0;
}
}