问题描述
我有这些字节:
1 68 78 77 51 51 49 32 49 50 49 51 48 55 13 10 71 71 32 69 89 86 73 89 68 89 88 13 10 49 50 49 51 48 55 32 69 89 86 73 90 80 90 66 13 10 2 71 79 79 68 32 68 65 89 44 13 10 68 73 83 82 69 71 65 82 68 32 84 72 73 83 32 65 82 82 32 77 83 71 32 66 69 76 79 87 13 10 40 65 82 82 45 76 89 84 73 84 47 65 55 48 49 51 45 69 89 86 73 45 69 89 86 73 49 50 52 54 41 13 10 86 78 79 32 66 82 73 69 70 73 78 71 13 10 11 3 84 32 49 50 48 53 13 10 45 83 83 82 67 79 68 69 32 65 55 55 49 51 13 10 45 83 69 81 85 69 78 67 69 68 65 84 65 13 10 45 84 88 84 73 77 69 32 50 48 48 56 49 50 49 51 48 52 49 56 13 10 45 78 85 77 32 52 54 13 10 11 3
另一个例子:
1 68 78 77 51 51 54 32 49 50 49 51 49 55 13 10 70 70 32 69 89 75 65 89 68 89 88 32 69 89 86 73 89 68 89 88 13 10 49 50 49 51 49 55 32 69 85 67 72 90 77 70 80 13 10 2 40 68 69 80 45 76 89 70 84 71 45 69 89 75 83 49 51 48 56 45 69 89 75 83 45 68 79 70 47 50 48 48 56 49 50 41 13 10 11 3 49 83 50 32 68 79 70 47 50 48 48 56 49 50 13 10 32 82 69 71 47 89 76 67 83 68 32 69 69 84 47 69 80 87 87 48 48 49 53 32 67 79 68 69 47 53 48 50 67 67 68 32 82 86 82 47 50 48 48 32 79 80 82 47 66 84 73 32 79 82 71 78 47 69 86 82 65 66 84 73 79 13 10 32 80 69 82 47 67 32 82 77 75 47 80 72 79 78 69 32 67 84 67 58 32 48 48 51 55 49 54 55 55 56 56 52 50 54 41 13 10 11 3 51 54 32 69 71 78 77 13 10 45 80 66 78 47 65 49 66 49 67 49 68 49 76 49 79 49 79 50 83 50 32 68 79 70 47 50 48 48 56 49 50 32 82 69 71 47 72 65 76 83 66 32 69 69 84 47 69 86 82 82 48 48 51 57 32 69 89 86 76 48 48 51 57 13 10 69 83 65 65 48 48 52 50 32 69 75 68 75 48 49 49 48 32 69 68 86 86 48 49 50 52 32 69 72 65 65 48 49 52 51 32 69 71 84 84 48 50 48 49 32 67 79 68 69 47 52 55 49 69 69 49 32 82 86 82 47 48 55 53 13 10 79 80 82 47 87 90 90 32 79 82 71 78 47 69 68 68 70 87 90 90 88 32 80 69 82 47 67 32 82 77 75 47 84 67 65 83 41 13 10 11 3
数据读取(ascii控制字符在此处不可见,因此我在下面添加图片,应将符合要求的部分写入文件):
DNM336 121317
FF EYKAYDYX EYVIYDYX
121317 EUCHZmfp
(DEP-LYFTG-EYKS1308-EYKS-DOF/200812)
1S2 DOF/200812
REG/YLCSD EET/EPWW0015 CODE/502CCD RVR/200 OPR/BTI ORGN/EVRABTIO
PER/C RMK/PHONE CTC: 0037167788426)
36 EGNM
-PBN/A1B1C1D1L1O1O2S2 DOF/200812 REG/HALSB EET/EVRR0039 EYVL0039
ESAA0042 EKDK0110 EDVV0124 EHAA0143 EGTT0201 CODE/471EE1 RVR/075
OPR/WZZ ORGN/EDDFWZZX PER/C RMK/TCAS)
和一个程序,侦听这些传入的字节,并在它们匹配条件时将它们写入文件: 邮件以SOH(1字节)开头,以EYX(3字节)结尾,正文中包含STX(2字节)
当我在Mac上进行测试时,它似乎对我来说很好用,但是我们在Windows上的服务器上进行了部署,有时似乎没有消息写... 同一字节数组中可能会出现几条消息。这是我的程序的样子:
class Program
{
private const string V_AFTN = "";
private const string K_AFTN = "";
private const string P_AFTN = "";
static void Main(string[] args)
{
string folderProduction = @"";
string folderTesting = @"";
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json")
.AddEnvironmentvariables()
.Build();
Parallel.Invoke(() => Run(folderProduction,80,"Production"),() => Run(folderTesting,configuration.GetValue<int>("PortSettingsTesting:Port"),"Testing"));
}
private static void Run(string folderName,int port,string env)
{
connection:
try
{
string[] switchStrings = {V_AFTN,K_AFTN,P_AFTN};
string pathString = "";
string airportFolder = "";
string fileName = "";
TcpListener server = new TcpListener(IPAddress.Any,port);
// Start listening for client requests.
server.Start();
Console.WriteLine(env + " TCP listener started on port: " + port);
// Enter the listening loop.
while (true)
{
try
{
Console.WriteLine(env + " Waiting for connection");
// Perform a blocking call to accept requests.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine(env + " Connected!");
Console.WriteLine("---------------------------------------------");
string result = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
Console.WriteLine("Buffer size: " + client.ReceiveBufferSize);
// Buffer for reading data
byte[] bytes = new byte[client.ReceiveBufferSize];
while (stream.Read(bytes,bytes.Length) != 0)
{
var bytesArray = bytes.TakeWhile(x => x != 0).ToArray();
for (int i = 0; i < bytesArray.Length; i++)
{
Console.Write(bytesArray[i] + " ");
}
var data = Encoding.ASCII.GetString(bytesArray);
Console.WriteLine("\r\n DATA READ: \r\n" + data);
Console.WriteLine();
for (int j = 0; j < bytesArray.Length; j++)
{
string bytetoString = "";
if (result == null && (bytesArray[j] == 0 || String.IsNullOrWhiteSpace(Encoding.ASCII.GetString(new[] {bytesArray[j]}))))
continue;
if (bytesArray[j] == 1)
{
bytetoString = "\x01";
}
else if (bytesArray[j] == 3 && result != null)
{
bytetoString = "\x03";
}
else if (bytesArray[j] == 2 && result != null)
{
bytetoString = "\x02";
}
else if (result != null)
{
bytetoString = Encoding.ASCII.GetString(new[] {bytesArray[j]});
}
result += bytetoString;
if (result.StartsWith("\x01") && result.Contains("\x02") && result.Contains("\x03"))
{
switch (switchStrings.FirstOrDefault<string>(s => result.Contains(s)))
{
case V_AFTN:
fileName = V_AFTN;
break;
default:
fileName = "UnkNown";
break;
}
pathString = Path.Combine(folderName,airportFolder);
if (!Directory.Exists(pathString))
{
Directory.CreateDirectory(pathString);
}
var id = GenerateIncrementalId(folderName,fileName);
CreateTextFile(pathString,fileName + "_" + DateTime.Now.ToString("yyMMdd_hhmm") + $"_{id}" + ".txt",result);
Console.WriteLine(env + " File created");
result = null;
}
}
}
Console.WriteLine(env + " Finished");
// Shutdown and end connection
client.Close();
}
catch (SocketException e)
{
Console.WriteLine(env + " SocketException: {0}",e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
}
catch (Exception e)
{
Console.WriteLine(env + " Failed to connect: " + e.Message);
goto connection;
}
}
private static int GenerateIncrementalId(string path,string airport)
{
DirectoryInfo d = new DirectoryInfo(path);
var files = d.GetFiles("*.txt").Where(x => x.Name.Contains(airport));
var fileInfos = files as FileInfo[] ?? files.ToArray();
if (!fileInfos.Any())
{
// if there's no file yet,return 1
return 1;
}
// return next incremental ID
return fileInfos.Count() + 1;
}
private static void CreateTextFile(string path,string textTitle,string data)
{
var pathToWrite = Path.Combine(path,textTitle);
if (!File.Exists(pathToWrite))
{
var charsToRemove = new [] {' ','\n','\r'};
data = data.TrimEnd(charsToRemove);
File.Create(pathToWrite).Close();
using var sw = new StreamWriter(pathToWrite);
Console.WriteLine("WRITING DATA: \r\n" + data);
sw.WriteLine(data);
Console.WriteLine("Writing data to: " + pathToWrite);
data = null;
sw.Flush();
}
}
}
我知道操作系统应该不是问题,但我对此一无所知,不知道这里可能有什么问题
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)