From cc844168fcdfb64ef96f140b1e4c024e8d9d72e6 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 15 Nov 2022 18:16:17 +0900 Subject: [PATCH] update comm logic --- SocketStudy/Client/Client.csproj | 20 +++ SocketStudy/ClientForm/ClientForm.csproj | 20 +++ SocketStudy/ClientForm/MainForm.Designer.cs | 13 ++ SocketStudy/ClientForm/MainForm.cs | 64 +++++++-- SocketStudy/PComm/PClient.cs | 121 +++++++++++++----- SocketStudy/PComm/PComm.csproj | 24 ++++ SocketStudy/PComm/PDataType.cs | 6 +- SocketStudy/PObject/PFileData.cs | 16 +++ SocketStudy/PObject/PObject.csproj | 19 +++ SocketStudy/PUtil/PFileManager.cs | 47 +++++++ SocketStudy/PUtil/PUtil.cs | 113 +++++++++++++++- SocketStudy/PUtil/PUtility.csproj | 23 +++- SocketStudy/Server/Server_Single.csproj | 20 +++ SocketStudy/ServerForm/DataManager.cs | 15 ++- SocketStudy/ServerForm/ServerForm.csproj | 20 +++ SocketStudy/ServerForm/mainForm.cs | 17 ++- SocketStudy/Server_Async/Server_Async.csproj | 20 +++ .../Server_Client_Listener.csproj | 20 +++ SocketStudy/SocketStudy.sln | 38 ++++++ 19 files changed, 572 insertions(+), 64 deletions(-) create mode 100644 SocketStudy/PObject/PFileData.cs create mode 100644 SocketStudy/PUtil/PFileManager.cs diff --git a/SocketStudy/Client/Client.csproj b/SocketStudy/Client/Client.csproj index 37c153a..df09fd7 100644 --- a/SocketStudy/Client/Client.csproj +++ b/SocketStudy/Client/Client.csproj @@ -32,6 +32,26 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + true + diff --git a/SocketStudy/ClientForm/ClientForm.csproj b/SocketStudy/ClientForm/ClientForm.csproj index 8a23807..0aa697b 100644 --- a/SocketStudy/ClientForm/ClientForm.csproj +++ b/SocketStudy/ClientForm/ClientForm.csproj @@ -32,6 +32,26 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + true + diff --git a/SocketStudy/ClientForm/MainForm.Designer.cs b/SocketStudy/ClientForm/MainForm.Designer.cs index fa9102f..abf2651 100644 --- a/SocketStudy/ClientForm/MainForm.Designer.cs +++ b/SocketStudy/ClientForm/MainForm.Designer.cs @@ -33,6 +33,7 @@ namespace ClientForm this.stringButton = new System.Windows.Forms.Button(); this.objectButton = new System.Windows.Forms.Button(); this.imageButton = new System.Windows.Forms.Button(); + this.fileButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // logConsole @@ -77,11 +78,22 @@ namespace ClientForm this.imageButton.UseVisualStyleBackColor = true; this.imageButton.Click += new System.EventHandler(this.imageButton_Click); // + // fileButton + // + this.fileButton.Location = new System.Drawing.Point(379, 39); + this.fileButton.Name = "fileButton"; + this.fileButton.Size = new System.Drawing.Size(75, 23); + this.fileButton.TabIndex = 2; + this.fileButton.Text = "file"; + this.fileButton.UseVisualStyleBackColor = true; + this.fileButton.Click += new System.EventHandler(this.fileButton_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(498, 450); + this.Controls.Add(this.fileButton); this.Controls.Add(this.imageButton); this.Controls.Add(this.objectButton); this.Controls.Add(this.stringButton); @@ -100,6 +112,7 @@ namespace ClientForm private System.Windows.Forms.Button stringButton; private System.Windows.Forms.Button objectButton; private System.Windows.Forms.Button imageButton; + private System.Windows.Forms.Button fileButton; } } diff --git a/SocketStudy/ClientForm/MainForm.cs b/SocketStudy/ClientForm/MainForm.cs index 2817c9e..fc55681 100644 --- a/SocketStudy/ClientForm/MainForm.cs +++ b/SocketStudy/ClientForm/MainForm.cs @@ -17,11 +17,13 @@ namespace ClientForm { public partial class MainForm : Form { + private readonly string SERVER_IP = "124.56.13.173"; + private PClient heartBeatClient; private PClient client; private System.Timers.Timer heartBeatTimer; - private System.Timers.Timer testTimer; + private System.Timers.Timer GarbageTimer; public MainForm() { @@ -34,21 +36,21 @@ namespace ClientForm string clientId = PUtil.GetRandomString(5); string heartBeatClientId = clientId + "_hb"; - heartBeatClient = new PClient("127.0.0.1", 7778, heartBeatClientId); + heartBeatClient = new PClient(SERVER_IP, 37778, heartBeatClientId); heartBeatClient.OnReceived += HeartBeatClient_OnReceived; - client = new PClient("127.0.0.1", 7777, clientId); + client = new PClient(SERVER_IP, 37777, clientId); client.OnReceived += Client_OnReceived; heartBeatTimer = new System.Timers.Timer(); - heartBeatTimer.Interval = 5000; + heartBeatTimer.Interval = 1000 * 10; heartBeatTimer.Elapsed += HeartBeatTimer_Elapsed; heartBeatTimer.Start(); - testTimer = new System.Timers.Timer(); - testTimer.Interval = 500; - testTimer.Elapsed += TestTimer_Elapsed; - testTimer.Start(); + GarbageTimer = new System.Timers.Timer(); + GarbageTimer.Interval = 1000 * 10; + GarbageTimer.Elapsed += GarbageTimer_Elapsed; + GarbageTimer.Start(); } private void WriteLog(string log) @@ -79,7 +81,12 @@ namespace ClientForm string msg = Encoding.UTF8.GetString(data, 0, data.Length); if (msg != PServer.HB_CHECK) + { WriteLog("WARNING!!! BAD HEARTBEAT!!!"); + client.Reconnect(); + heartBeatClient.Reconnect(); + } + } private void Client_OnReceived(PClient sender, PDataType dataType, byte[] data) @@ -87,9 +94,9 @@ namespace ClientForm throw new NotImplementedException(); } - private void TestTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + private void GarbageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { - //button1_Click(sender, e); + GC.Collect(); } private void stringButton_Click(object sender, EventArgs e) @@ -107,7 +114,7 @@ namespace ClientForm }; byte[] data = PSerializer.Serialize(person); - client.Send(PDataType.Person, data); + client.Send(PDataType.PersonObject, data); } private void imageButton_Click(object sender, EventArgs e) @@ -118,8 +125,39 @@ namespace ClientForm return; Image img = Image.FromFile(dlg.FileName); - byte[] data = PUtil.ImageToByteArray(img); - client.Send(PDataType.Image, data); + byte[] data = PUtil.ImageToBytes(img); + client.Send(PDataType.ShowImage, data); + + GC.Collect(); + } + + private void fileButton_Click(object sender, EventArgs e) + { + OpenFileDialog dlg = new OpenFileDialog(); + dlg.Filter = "All File|*.*"; + if (dlg.ShowDialog() == DialogResult.Cancel) + return; + + string filePath = dlg.FileName; + string fileName = dlg.SafeFileName; + + PFileData fileData = new PFileData() + { + FileName = fileName, + Data = PUtil.FileToBytes(filePath) + }; + + if (fileData.Data.Length >= 1000000000) + { + MessageBox.Show("1GB를 초과하는 데이터는 전송할 수 없습니다."); + + GC.Collect(); + return; + } + + + byte[] data = PSerializer.Serialize(fileData); + client.Send(PDataType.SaveFile, data); GC.Collect(); } diff --git a/SocketStudy/PComm/PClient.cs b/SocketStudy/PComm/PClient.cs index 59941b5..95f7e87 100644 --- a/SocketStudy/PComm/PClient.cs +++ b/SocketStudy/PComm/PClient.cs @@ -1,7 +1,9 @@ -using System; +using PUtility; +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.IO.Compression; using System.Linq; using System.Net; using System.Net.Sockets; @@ -36,7 +38,7 @@ namespace PComm socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.ID = id; this.EndPoint = new IPEndPoint(IPAddress.Parse(ip), port); - socket.Connect(EndPoint); + socket.Connect(this.EndPoint); // send ID this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID)); @@ -54,23 +56,61 @@ namespace PComm socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); } + public void Reconnect() + { + if (socket.Connected) + return; + + socket.Connect(this.EndPoint); + + this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID)); + + socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); + } + + private int SendData(byte[] data) + { + byte[] toSendData; + // 데이터 크기 100B 이하 또는 50MB 이상 압축하지 않음 (CPU 부하시간 더 큼) + // 압축한 경우 1 전송, 아닌경우 0 전송 + if (data.Length <= 100 || data.Length >= 50000000) + { + byte[] isCompressed = BitConverter.GetBytes(false); + socket.Send(isCompressed, SocketFlags.None); + toSendData = data; + } + else + { + byte[] isCompressed = BitConverter.GetBytes(true); + socket.Send(isCompressed, SocketFlags.None); + toSendData = PUtil.CompressBytes(data, CompressionLevel.Optimal); + } + + byte[] length = BitConverter.GetBytes(toSendData.Length); + socket.Send(length, 0, 4, SocketFlags.None); + + return socket.Send(toSendData, SocketFlags.None); + } + public int Send(string msg) { socket.Send(BitConverter.GetBytes((int)PDataType.SimpleString), 0, 4, 0); byte[] data = Encoding.UTF8.GetBytes(msg); - byte[] length = BitConverter.GetBytes(data.Length); - socket.Send(length, 0, 4, 0); + //byte[] length = BitConverter.GetBytes(data.Length); + //socket.Send(length, 0, 4, 0); - return socket.Send(data, SocketFlags.None); + //return socket.Send(data, SocketFlags.None); + return SendData(data); } public int Send(PDataType dataType, byte[] data) { socket.Send(BitConverter.GetBytes((int)dataType), 0, 4, 0); - byte[] length = BitConverter.GetBytes(data.Length); - socket.Send(length, 0, 4, 0); + //byte[] length = BitConverter.GetBytes(data.Length); + //socket.Send(length, 0, 4, 0); - return socket.Send(data, SocketFlags.None); + //return socket.Send(data, SocketFlags.None); + return SendData(data); } private void AcceptCallback(IAsyncResult ar) @@ -84,10 +124,16 @@ namespace PComm socket.Receive(dataTypeBuff, dataTypeBuff.Length, SocketFlags.None); PDataType dataType = (PDataType)BitConverter.ToInt32(dataTypeBuff, 0); + // check compressed + byte[] isCompressedBuff = new byte[1]; + socket.Receive(isCompressedBuff, isCompressedBuff.Length, SocketFlags.None); + bool isCompressed = BitConverter.ToBoolean(isCompressedBuff, 0); + // get data size byte[] sizeBuff = new byte[4]; socket.Receive(sizeBuff, sizeBuff.Length, SocketFlags.None); int dataSize = BitConverter.ToInt32(sizeBuff, 0); + using (MemoryStream ms = new MemoryStream()) { @@ -105,44 +151,51 @@ namespace PComm dataSize -= receiveSize; } - byte[] data = ms.ToArray(); + byte[] receivedData = ms.ToArray(); + byte[] data; + if (isCompressed) + data = PUtil.DecompressBytes(receivedData); + else + data = receivedData; if (OnReceived != null) OnReceived(this, dataType, data); - } - - socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); - - //byte[] buff = new byte[BUFF_SIZE]; - //int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None); - //if (receiveSize <= 0) - //{ - // Close(); - - // if (Disconnected != null) - // Disconnected(this); - //} - //else if (receiveSize < buff.Length) - //{ - // Array.Resize(ref buff, receiveSize); - //} - //if (OnReceived != null) - // OnReceived(this, buff); - - //socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); + socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); + } } catch (Exception ex) { - this.Close(); + Debug.WriteLine($"[ERROR] CleintAcceptCallback: {ex.Message}"); + PFileManager.Instance.WriteLog($"[ERROR] CleintAcceptCallback: {ex.Message}"); + + if (CheckSocketConnection(socket)) + { + socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); + } + else + { + Debug.WriteLine($"[ERROR] Close socket {socket.RemoteEndPoint.ToString()}"); + PFileManager.Instance.WriteLog($"[ERROR] Close socket {socket.RemoteEndPoint.ToString()}"); - if (Disconnected != null) - Disconnected(this); + Close(); - Debug.WriteLine($"[ERROR] CleintAcceptCallback: {ex.Message}"); + if (Disconnected != null) + Disconnected(this); + } } } + private bool CheckSocketConnection(Socket socket) + { + bool availability = socket.Available == 0; + bool poll = socket.Poll(1000, SelectMode.SelectRead); + if (availability && poll) + return false; + else + return true; + } + public void Close() { socket.Close(); diff --git a/SocketStudy/PComm/PComm.csproj b/SocketStudy/PComm/PComm.csproj index fc1ff5b..a8f4a7c 100644 --- a/SocketStudy/PComm/PComm.csproj +++ b/SocketStudy/PComm/PComm.csproj @@ -30,6 +30,24 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + @@ -49,5 +67,11 @@ + + + {b6e7f933-eb73-4d83-94fa-fb17774f1d52} + PUtility + + \ No newline at end of file diff --git a/SocketStudy/PComm/PDataType.cs b/SocketStudy/PComm/PDataType.cs index d531994..9c7adff 100644 --- a/SocketStudy/PComm/PDataType.cs +++ b/SocketStudy/PComm/PDataType.cs @@ -12,8 +12,10 @@ namespace PComm SimpleString = 10, - Person = 100, + PersonObject = 100, - Image = 200, + ShowImage = 200, + + SaveFile = 300, } } diff --git a/SocketStudy/PObject/PFileData.cs b/SocketStudy/PObject/PFileData.cs new file mode 100644 index 0000000..9d93312 --- /dev/null +++ b/SocketStudy/PObject/PFileData.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PObject +{ + [Serializable] + public class PFileData + { + public string FileName { get; set;} + + public byte[] Data { get; set; } + } +} diff --git a/SocketStudy/PObject/PObject.csproj b/SocketStudy/PObject/PObject.csproj index 785f76b..1503dd5 100644 --- a/SocketStudy/PObject/PObject.csproj +++ b/SocketStudy/PObject/PObject.csproj @@ -30,6 +30,24 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + @@ -42,6 +60,7 @@ + diff --git a/SocketStudy/PUtil/PFileManager.cs b/SocketStudy/PUtil/PFileManager.cs new file mode 100644 index 0000000..dd86396 --- /dev/null +++ b/SocketStudy/PUtil/PFileManager.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace PUtility +{ + public class PFileManager + { + public static PFileManager Instance { get; set; } = new PFileManager(); + + public string DirPath { get; set; } = Application.StartupPath + @"\Files"; + public string LogDirPath { get; set; } = Application.StartupPath + @"\Logs"; + + private void CheckFolder(string dirPath) + { + DirectoryInfo directoryInfo = new DirectoryInfo(dirPath); + if (!directoryInfo.Exists) + directoryInfo.Create(); + } + + public void SaveFile(byte[] data, string fileName) + { + CheckFolder(this.DirPath); + + if (string.IsNullOrEmpty(fileName)) + fileName = Guid.NewGuid().ToString(); + + string fileSaveName = PUtil.CheckFileDuplication(this.DirPath, fileName); + string filePath = Path.Combine(this.DirPath, fileSaveName); + PUtil.BytesToFile(filePath, data); + } + + public void WriteLog(string log) + { + CheckFolder(this.LogDirPath); + + DateTime now = DateTime.Now; + string logPath = Path.Combine(this.LogDirPath, $"[{now.ToString("yyyyMMdd")}]log.txt"); + + File.AppendAllText(logPath, $"[{now.ToString("yyyyMMdd HH:mm:ss")}] {log + Environment.NewLine}"); + } + } +} diff --git a/SocketStudy/PUtil/PUtil.cs b/SocketStudy/PUtil/PUtil.cs index b1ef5ac..c02500f 100644 --- a/SocketStudy/PUtil/PUtil.cs +++ b/SocketStudy/PUtil/PUtil.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; +using System.IO.Compression; +using System.Diagnostics; namespace PUtility { @@ -35,22 +37,69 @@ namespace PUtility target.ScrollToCaret(); } - public static byte[] ImageToByteArray(Image image) + public static byte[] CompressBytes(byte[] data, CompressionLevel level = CompressionLevel.Fastest) { + byte[] compressedData; + using (MemoryStream ms = new MemoryStream()) + { + using (GZipStream gs = new GZipStream(ms, level)) + { + gs.Write(data, 0, data.Length); + } + + compressedData = ms.ToArray(); + } + + Debug.WriteLine($"Compressed{data.Length} to {compressedData.Length} ({(float)compressedData.Length / (float)data.Length * 100f}%)"); + + return compressedData; + } + + public static byte[] DecompressBytes(byte[] data) + { + byte[] decompressedData; + using (MemoryStream resultStream = new MemoryStream()) + { + using (MemoryStream ms = new MemoryStream(data)) + { + using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress)) + { + gs.CopyTo(resultStream); + gs.Close(); + } + } + + decompressedData = resultStream.ToArray(); + } + + //Debug.WriteLine($"Decompressed{data.Length} to {decompressedData.Length} ({(float)decompressedData.Length / (float)data.Length * 100f}%)"); + + return decompressedData; + + } + + + public static byte[] ImageToBytes(Image image) + { + byte[] data; using (MemoryStream ms = new MemoryStream()) { image.Save(ms, image.RawFormat); - return ms.ToArray(); + data = ms.ToArray(); } + + return data; } - public static Bitmap ByteToImage(byte[] imgByteArray) + public static Bitmap BytesToImage(byte[] imgByteArray) { + Bitmap bitmap; using (MemoryStream ms = new MemoryStream(imgByteArray)) { - Bitmap img = Image.FromStream(ms) as Bitmap; - return img; + bitmap = Image.FromStream(ms) as Bitmap; } + + return bitmap; } public static void SaveImage(string fileName, Image img, ImageFormat format) @@ -60,5 +109,59 @@ namespace PUtility saveImage.Save(fileName, format); } } + + public static byte[] FileToBytes(string filePath) + { + return File.ReadAllBytes(filePath); + + //byte[] data; + //using (FileStream fs = new FileStream(filePath, FileMode.Open)) + //{ + // data = new byte[fs.Length]; + // fs.Read(data, 0, data.Length); + //} + + //return data; + } + + public static void BytesToFile(string filePath, byte[] data) + { + File.WriteAllBytes(filePath, data); + + //using (FileStream fs = new FileStream(filePath, FileMode.Create)) + //{ + // fs.Write(data, 0, data.Length); + //} + } + + public static string CheckFileDuplication(string dirPath, string fileName) + { + if (string.IsNullOrEmpty(dirPath) || string.IsNullOrEmpty(fileName)) + return null; + + string resultFileName = fileName; + int dotIdx = fileName.LastIndexOf("."); + string name = fileName.Substring(0, dotIdx); + string ext = fileName.Substring(dotIdx); + + bool fileExist = true; + int fileCount = 0; + string dirMapPath = ""; + while (fileExist) + { + dirMapPath = dirPath; + string combinedPath = Path.Combine(dirMapPath, resultFileName); + if (!File.Exists(combinedPath)) + { + fileExist = false; + break; + } + + fileCount++; + resultFileName = $"{name}({fileCount}){ext}"; + } + + return resultFileName; + } } } diff --git a/SocketStudy/PUtil/PUtility.csproj b/SocketStudy/PUtil/PUtility.csproj index 913f21b..aadc2a4 100644 --- a/SocketStudy/PUtil/PUtility.csproj +++ b/SocketStudy/PUtil/PUtility.csproj @@ -7,8 +7,8 @@ {B6E7F933-EB73-4D83-94FA-FB17774F1D52} Library Properties - PUtil - PUtil + PUtility + PUtility v4.8.1 512 true @@ -30,6 +30,24 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + @@ -43,6 +61,7 @@ + diff --git a/SocketStudy/Server/Server_Single.csproj b/SocketStudy/Server/Server_Single.csproj index 265f1e0..dbad1b5 100644 --- a/SocketStudy/Server/Server_Single.csproj +++ b/SocketStudy/Server/Server_Single.csproj @@ -32,6 +32,26 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + true + diff --git a/SocketStudy/ServerForm/DataManager.cs b/SocketStudy/ServerForm/DataManager.cs index 9858964..989177d 100644 --- a/SocketStudy/ServerForm/DataManager.cs +++ b/SocketStudy/ServerForm/DataManager.cs @@ -32,23 +32,26 @@ namespace ServerForm log = Encoding.UTF8.GetString(data, 0, data.Length); logHandler(clientInfo.Client, log); break; - case PDataType.Person: + case PDataType.PersonObject: Person person = PSerializer.Deserialize(data); log = $"Name: {person.Name}, Age: {person.Age}"; logHandler(clientInfo.Client, log); break; - case PDataType.Image: - Image image = PUtil.ByteToImage(data); - //string fileName = $@"{System.IO.Path.GetTempPath()}{Guid.NewGuid()}.bmp"; - //PUtil.SaveImage(fileName, image, image.RawFormat); + case PDataType.ShowImage: + Image image = PUtil.BytesToImage(data); log = $"Receive {data.Length.ToString("#,###")}bytes image"; logHandler(clientInfo.Client, log); ImageForm imgForm = new ImageForm(image); formHandler(imgForm); + break; + case PDataType.SaveFile: + PFileData fileData = PSerializer.Deserialize(data); + PFileManager.Instance.SaveFile(fileData.Data, fileData.FileName); - GC.Collect(); + log = $"Receive {data.Length.ToString("#,###")}bytes file and save it"; + logHandler(clientInfo.Client, log); break; default: logHandler(clientInfo.Client, "ERROR!!! UNEXPECTED DATA TYPE!!!"); diff --git a/SocketStudy/ServerForm/ServerForm.csproj b/SocketStudy/ServerForm/ServerForm.csproj index 88d138e..0b90893 100644 --- a/SocketStudy/ServerForm/ServerForm.csproj +++ b/SocketStudy/ServerForm/ServerForm.csproj @@ -32,6 +32,26 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + true + diff --git a/SocketStudy/ServerForm/mainForm.cs b/SocketStudy/ServerForm/mainForm.cs index 1821b7f..ce1a4d9 100644 --- a/SocketStudy/ServerForm/mainForm.cs +++ b/SocketStudy/ServerForm/mainForm.cs @@ -19,8 +19,8 @@ namespace ServerForm public partial class MainForm : Form { - private readonly int PORT = 7777; - private readonly int HB_PORT = 7778; + private readonly int PORT = 37777; + private readonly int HB_PORT = 37778; private PServer mainServer; private PServer heartBeatServer; @@ -29,6 +29,7 @@ namespace ServerForm private FormHandler formHandler; private System.Timers.Timer clientCheckTimer; + private System.Timers.Timer GarbageTimer; public MainForm() { @@ -38,6 +39,8 @@ namespace ServerForm private void InitInstance() { + string path = Application.StartupPath; + heartBeatServer = new PServer(HB_PORT); mainServer = new PServer(PORT); @@ -54,6 +57,11 @@ namespace ServerForm clientCheckTimer.Interval = 1000; clientCheckTimer.Elapsed += ClientCheckTimer_Elapsed; clientCheckTimer.Start(); + + GarbageTimer = new System.Timers.Timer(); + GarbageTimer.Interval = 1000 * 10; + GarbageTimer.Elapsed += GarbageTimer_Elapsed; + GarbageTimer.Start(); } private void WriteLog(PClient client, string log) @@ -129,5 +137,10 @@ namespace ServerForm CheckClientInfo(clientInfoList); } + + private void GarbageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + GC.Collect(); + } } } diff --git a/SocketStudy/Server_Async/Server_Async.csproj b/SocketStudy/Server_Async/Server_Async.csproj index b94abdd..7b04a9c 100644 --- a/SocketStudy/Server_Async/Server_Async.csproj +++ b/SocketStudy/Server_Async/Server_Async.csproj @@ -32,6 +32,26 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + true + diff --git a/SocketStudy/Server_Listener/Server_Client_Listener.csproj b/SocketStudy/Server_Listener/Server_Client_Listener.csproj index 98f3e72..140adfb 100644 --- a/SocketStudy/Server_Listener/Server_Client_Listener.csproj +++ b/SocketStudy/Server_Listener/Server_Client_Listener.csproj @@ -32,6 +32,26 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + true + diff --git a/SocketStudy/SocketStudy.sln b/SocketStudy/SocketStudy.sln index 6c40ecc..641b22b 100644 --- a/SocketStudy/SocketStudy.sln +++ b/SocketStudy/SocketStudy.sln @@ -24,45 +24,83 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|x64.ActiveCfg = Debug|x64 + {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|x64.Build.0 = Debug|x64 {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|Any CPU.ActiveCfg = Release|Any CPU {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|Any CPU.Build.0 = Release|Any CPU + {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|x64.ActiveCfg = Release|x64 + {5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|x64.Build.0 = Release|x64 {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|Any CPU.Build.0 = Debug|Any CPU + {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|x64.ActiveCfg = Debug|x64 + {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|x64.Build.0 = Debug|x64 {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|Any CPU.ActiveCfg = Release|Any CPU {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|Any CPU.Build.0 = Release|Any CPU + {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|x64.ActiveCfg = Release|x64 + {408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|x64.Build.0 = Release|x64 {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|x64.ActiveCfg = Debug|x64 + {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|x64.Build.0 = Debug|x64 {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|Any CPU.ActiveCfg = Release|Any CPU {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|Any CPU.Build.0 = Release|Any CPU + {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|x64.ActiveCfg = Release|x64 + {94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|x64.Build.0 = Release|x64 {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|x64.ActiveCfg = Debug|x64 + {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|x64.Build.0 = Debug|x64 {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|Any CPU.ActiveCfg = Release|Any CPU {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|Any CPU.Build.0 = Release|Any CPU + {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|x64.ActiveCfg = Release|x64 + {420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|x64.Build.0 = Release|x64 {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|x64.ActiveCfg = Debug|x64 + {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|x64.Build.0 = Debug|x64 {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|Any CPU.ActiveCfg = Release|Any CPU {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|Any CPU.Build.0 = Release|Any CPU + {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|x64.ActiveCfg = Release|x64 + {A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|x64.Build.0 = Release|x64 {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|Any CPU.Build.0 = Debug|Any CPU + {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|x64.ActiveCfg = Debug|x64 + {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|x64.Build.0 = Debug|x64 {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|Any CPU.ActiveCfg = Release|Any CPU {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|Any CPU.Build.0 = Release|Any CPU + {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|x64.ActiveCfg = Release|x64 + {80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|x64.Build.0 = Release|x64 {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|x64.ActiveCfg = Debug|x64 + {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|x64.Build.0 = Debug|x64 {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|Any CPU.ActiveCfg = Release|Any CPU {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|Any CPU.Build.0 = Release|Any CPU + {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|x64.ActiveCfg = Release|x64 + {B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|x64.Build.0 = Release|x64 {BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|x64.ActiveCfg = Debug|x64 + {BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|x64.Build.0 = Debug|x64 {BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|Any CPU.ActiveCfg = Release|Any CPU {BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|Any CPU.Build.0 = Release|Any CPU + {BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|x64.ActiveCfg = Release|x64 + {BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|x64.Build.0 = Release|x64 {ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|x64.ActiveCfg = Debug|x64 + {ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|x64.Build.0 = Debug|x64 {ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|Any CPU.ActiveCfg = Release|Any CPU {ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|Any CPU.Build.0 = Release|Any CPU + {ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|x64.ActiveCfg = Release|x64 + {ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE