簡體   English   中英

創建 TCP 連接並獲取 Header

[英]Creating TCP connection and getting the Header

我正在制作一個需要接收 TCP 連接的應用程序,該連接附帶包含一些 header 數據的請求,我需要稍后讀取並發送回更多信息。 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建立穩定的 TCP 連接。 我似乎能夠建立連接的只有兩種方法是使用普通 TCP 客戶端和普通 sockets(示例)`

    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);
    }

`但是在運行 Raw 時你不能使用 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. 我一直對獲得該死的 header 有多難感到困惑。

...附帶包含一些 header 的請求...

您在這里混淆了: TCP 沒有請求, TCP 只是一個非結構化的 stream 字節。 “消息”、“請求”或類似的任何語義都純粹在應用程序級別,即在非結構化字節 stream 之上定義結構語義的應用程序協議。 此類應用協議的一個示例是HTTP ,它將消息定義為具有可變大小的 header 和正文,並將請求和響應的概念定義為特定類型的消息。

... it seems the only way to get the Header from a TCP connection is with using sockets specifically Raw sockets and enabling "SocketOptionName.HeaderIncluded"

根據您的最后一個問題,您實際上並不是在尋找 TCP header,而是在尋找包含一些 header 的應用程序消息。 引用:

...如何在 c# 中獲得 TCP header。 我知道我將收到的消息將包含兩個標頭,一個 16 字節的 header 和一個 48 字節的 header。

Given that the TCP header is always 20 bytes you cannot mean the real TCP header here, but this is about some kind of message header for some unknown kind of message format. 在這種情況下,不需要原始 sockets,但您只需讀取與消息/請求一樣多的字節,然后從您閱讀的內容中刪除 header。

但是您需要為請求讀取多少字節以及如何從請求中剝離 header 完全取決於應用程序協議。 如果總是有一些第一個 header 有 16 個字節,則從套接字讀取 16 個字節。 如果總是有另一個 48 字節的 header 讀取這 48 字節。 如果這些 header 以某種方式定義了消息的 rest 有多大,請提取此信息並讀取那么多字節。

因此,您需要的第一個信息是找出協議是如何定義的,即消息或請求的結構如何映射到 stream 字節。 如果沒有這些信息,就不可能執行您的任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM