Im making an application that needs to receive a TCP connection that comes with a request that contains some header data that i need to read and send back with more info later. The issue is that after googling everywhere for days and countless tests it seems the only way to get the Header from a TCP connection is with using sockets specifically Raw sockets and enabling "SocketOptionName.HeaderIncluded" But when using raw sockets i cant find a way to make a stable TCP connection. Only two ways i seem to be able to make a connection is with normal TCP client and with normal sockets (example)`
private static readonly Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
private const int BUFFER_SIZE = 2048;
private const int PORT = 52000;
private static readonly byte[] buffer = new byte[BUFFER_SIZE];
private static readonly List<Socket> clientSockets = new List<Socket>();
public static void ServerMode()
{
Console.WriteLine("Setting up server...");
serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT));
serverSocket.Listen(0);
serverSocket.BeginAccept(AcceptCallback, null);
Console.WriteLine("Server setup complete");
while (true) { Thread.Sleep(10); }
}
private static void AcceptCallback(IAsyncResult AR)
{
Socket socket;
try
{
socket = serverSocket.EndAccept(AR);
}
catch (ObjectDisposedException)
{
return;
}
clientSockets.Add(socket);
socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket);
Console.WriteLine("Client connected, waiting for request...");
serverSocket.BeginAccept(AcceptCallback, null);
}
private static void ReceiveCallback(IAsyncResult AR)
{
Socket current = (Socket)AR.AsyncState;
int received;
try
{
received = current.EndReceive(AR);
}
catch (SocketException)
{
Console.WriteLine("Client forcefully disconnected");
// Don't shutdown because the socket may be disposed and its disconnected anyway.
current.Close();
clientSockets.Remove(current);
return;
}
byte[] recBuf = new byte[received];
Array.Copy(buffer, recBuf, received);
string text = Encoding.ASCII.GetString(recBuf);
Console.WriteLine("Received Text: " + text);
current.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
}
`But when running Raw your cant use socket.Listen(); Im hoping someone has experienced anything like this and has a solution, either a way to establish a TCP connection with raw sockets or a way to get the header from a TCP connection. Im constantly baffled by how hard it is to get the god damm header.