Skip to content

Instantly share code, notes, and snippets.

@fzankl
Created January 28, 2022 17:29
Show Gist options
  • Save fzankl/3e914497ee4beab4bb8d7815ce960fd9 to your computer and use it in GitHub Desktop.
Save fzankl/3e914497ee4beab4bb8d7815ce960fd9 to your computer and use it in GitHub Desktop.
Request API over unix socket using Socket class
using System.Net.Sockets;
const string Hostname = "localhost";
const string UnixSocketPath = "/tmp/foo.sock";
using var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
var endpoint = new UnixDomainSocketEndPoint(UnixSocketPath);
socket.Connect(endpoint);
var requestBytes = System.Text.Encoding.UTF8.GetBytes($"GET / HTTP/1.0\r\nHost: {Hostname}\r\nAccept: */*\r\n\r\n");
socket.Send(requestBytes);
byte[] receivedBytes = new byte[1024];
socket.Receive(receivedBytes, 1024, SocketFlags.None);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(receivedBytes));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment