簡體   English   中英

有沒有辦法讓 tcp 服務器發送響應而不是回顯傳入消息?

[英]Is there a way to get tcp server to send responses other than echoing incoming messages?

我顯然是 TCP 服務器的新手。 下面的代碼工作得很好——它“只”回應了它收到的消息。 但我的問題是“簡單的”:我怎樣才能向我的客戶發送響應——而不是像下面那樣簡單地回應請求? 例如,如果我想發回數據(特別是對我來說,XML 中的“OFML”數據,類似於刑事司法最終用戶的表格)。

但我會滿足於“Hello world!”!

我所做的所有嘗試都導致我的客戶端崩潰(我無法共享其專有代碼) - 以及一些自定義錯誤消息,例如“未找到數據包”。

任何建議將不勝感激 - 或參考一些關於如何完成此任務的明確文檔。

哦 - 我可能會補充一點,我只是想創建一個簡單的“模擬”服務器,用於客戶端的本地調試 - 即這永遠不會用於“生產”。 謝謝!

using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Threading;


namespace FoxTalkMOCK
{
    class Program
    {
        public static void Main()
        {
            TcpListener server = null;
            try
            {
                Int32 port = 8080;
                IPAddress localAddr = IPAddress.Parse("10.116.45.49");
 
                server = new TcpListener(localAddr, port);
 
                server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[18];
                String data = null;

                // Enter the listening loop.
                while (true)
                {
                    Console.Write("Waiting for a connection... ");
 
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");
                    data = null;
                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();                   
                    int i;                   
                    // Loop to receive all the data sent by the client.
                    try
                    {
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);

                        // Process the data sent by the client.
                        data = data.ToUpper();

                            string bitString = BitConverter.ToString(bytes);
                            bitString = bitString.Replace("-", ", 0x");
                            bitString = "0x" + bitString;
                            Console.WriteLine(bitString);

                            // *******************Send response*********************
                            stream.Write(bytes, 0, bytes.Length);
                            Console.WriteLine("Sent: {0}", System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }
            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }
    }
}```

該示例連接並接收數據,然后將該數據發回並斷開連接。

如果要保留插座,請保留它而不會損壞。

每當你保存它並再次寫入它時,你可以通過異常處理來管理套接字是否被維護。

簡單返回“hello world”的答案如下:

var strBuffer = "Hello World!";
byte[] array = new byte[strBuffer.Length];
array = System.Text.Encoding.ASCII.GetBytes(strBuffer);
stream.Write(array, 0, array.Length)

暫無
暫無

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

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