Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2022 17:29
What would you like to do?
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);
var requestBytes = System.Text.Encoding.UTF8.GetBytes($"GET / HTTP/1.0\r\nHost: {Hostname}\r\nAccept: */*\r\n\r\n");
byte[] receivedBytes = new byte[1024];
socket.Receive(receivedBytes, 1024, SocketFlags.None);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment