Skip to content

Instantly share code, notes, and snippets.

@matarillo
Created August 1, 2019 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matarillo/d1a6e2da05eb27ab51765e1c0903e1db to your computer and use it in GitHub Desktop.
Save matarillo/d1a6e2da05eb27ab51765e1c0903e1db to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Sock
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "watch")
{
Watcher("test.sock");
}
else
{
Listener("test.sock");
}
}
static void Watcher(string file)
{
for (int i = 0; i < 20; i++)
{
if (File.Exists(file))
{
WriteAttributes(file);
}
Task.Delay(500).Wait();
}
}
static void Listener(string file)
{
Socket socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
if (File.Exists(file))
{
File.Delete(file);
}
socket.Bind(new UnixDomainSocketEndPoint(file));
Console.WriteLine("Bind");
WriteAttributes(file);
Task.Delay(1000).Wait();
socket.Listen(backlog: 10);
Console.WriteLine("Listen");
WriteAttributes(file);
Task.Delay(1000).Wait();
Task<Socket> task = socket.AcceptAsync();
Console.WriteLine("Accept");
WriteAttributes(file);
Task.Delay(1000).Wait();
socket.Close();
Console.WriteLine("Close");
WriteAttributes(file);
Task.Delay(1000).Wait();
File.Delete(file);
}
static void WriteAttributes(string file)
{
FileAttributes attr = File.GetAttributes(file);
Console.WriteLine($"{file}: {attr}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment