使用套接字响应客户端

本文关键字:客户端 响应 套接字 | 更新日期: 2023-09-27 18:35:34

我有两个基本的控制台应用程序,即使所有通信都在本地计算机上进行"通过网络"通信。

客户端代码:

public static void Main()
{
    while (true)
    {
        try
        {
            TcpClient client = new TcpClient();
            client.Connect("127.0.0.1", 500);
            Console.WriteLine("Connected.");
            byte[] data = ASCIIEncoding.ASCII.GetBytes(new FeederRequest("test", TableType.Event).GetXmlRequest().ToString());
            Console.WriteLine("Sending data.....");
            using (var stream = client.GetStream())
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
                Console.WriteLine("Data sent.");
            }
            client.Close();
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.StackTrace);
            Console.ReadLine();
        }
    }
}

服务器代码:

public static void Main()
{
    try
    {
        IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
        Console.WriteLine("Starting TCP listener...");
        TcpListener listener = new TcpListener(ipAddress, 500);
        listener.Start();
        Console.WriteLine("Server is listening on " + listener.LocalEndpoint);
        while (true)
        {
            Socket client = listener.AcceptSocket();
            Console.WriteLine("'nConnection accepted.");
            var childSocketThread = new Thread(() =>
                {
                    Console.WriteLine("Reading data...'n");
                    byte[] data = new byte[100];
                    int size = client.Receive(data);
                    Console.WriteLine("Recieved data: ");
                    for (int i = 0; i < size; i++)
                        Console.Write(Convert.ToChar(data[i]));
                    //respond to client

                    Console.WriteLine("'n");
                    client.Close();
                    Console.WriteLine("Waiting for a connection...");
                });
            childSocketThread.Start();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.StackTrace);
        Console.ReadLine();
    }
}

如何更改这两个应用程序,以便在服务器收到数据时,它通过某种确认响应客户端?

提前感谢!

使用套接字响应客户端

这里有一个简短的例子,我将如何做到这一点:

服务器:

class Server
    {
        static void Main(string[] args)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, 1500);
            listener.Start();
            TcpClient client = listener.AcceptTcpClient();
            NetworkStream stream = client.GetStream();
            // Create BinaryWriter for writing to stream
            BinaryWriter binaryWriter = new BinaryWriter(stream);
            // Creating BinaryReader for reading the stream
            BinaryReader binaryReader = new BinaryReader(stream);
            while (true) 
            {
                // Read incoming information
                byte[] data = new byte[16];
                int receivedDataLength = binaryReader.Read(data, 0, data.Length);
                string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
                // Write incoming information to console
                Console.WriteLine("Client: " + stringData);
                // Respond to client
                byte[] respondData = Encoding.ASCII.GetBytes("respond");
                Array.Resize(ref respondData, 16); // Resizing to 16 byte, because in this example all messages have 16 byte to make it easier to understand.
                binaryWriter.Write(respondData, 0, 16);
            }
        }
    }

客户:

class Client
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Press any key to start Client");
            while (! Console.KeyAvailable)
            {
            }

            TcpClient client = new TcpClient();
            client.Connect("127.0.0.1", 1500);
            NetworkStream networkStream = client.GetStream();
            // Create BinaryWriter for writing to stream
            BinaryWriter binaryWriter = new BinaryWriter(networkStream);
            // Creating BinaryReader for reading the stream
            BinaryReader binaryReader = new BinaryReader(networkStream);
            // Writing "test" to stream
            byte[] writeData = Encoding.ASCII.GetBytes("test");
            Array.Resize(ref writeData, 16); // Resizing to 16 byte, because in this example all messages have 16 byte to make it easier to understand.
            binaryWriter.Write(writeData, 0, 16);
            // Reading response and writing it to console
            byte[] responeBytes = new byte[16];
            binaryReader.Read(responeBytes, 0, 16);
            string response = Encoding.ASCII.GetString(responeBytes);
            Console.WriteLine("Server: " + response);

            while (true)
            {
            }
        }
    }

我希望这有帮助! ;)

您可以在同一流上执行读取和写入。发送所有数据后,只需呼叫流即可。阅读如

        using (var stream = client.GetStream())
        {
            stream.Write(data, 0, data.Length);
            stream.Flush();
            Console.WriteLine("Data sent.");
            stream.Read(....); //added sync read here                
        }

关于 TcpClient 的 MSDN 文档也有一个示例 http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

如果要反馈,例如报告 # 到目前为止收到的字节数,则必须使用异步方法。

以下是您想要执行的操作(我认为)的示例:

    static void Main(string[] args) {
        var server = new Task(Server);
        server.Start();
        System.Threading.Thread.Sleep(10); // give server thread a chance to setup
        try {
            TcpClient client = new TcpClient();
            client.Connect("127.0.0.1", 1500);
            Console.WriteLine("Connected.");
            var data = new byte[100];
            var hello = ASCIIEncoding.ASCII.GetBytes("Hello");
            Console.WriteLine("Sending data.....");
            using (var stream = client.GetStream()) {
                stream.Write(hello, 0, hello.Length);
                stream.Flush();
                Console.WriteLine("Data sent.");
                // You could then read data from server here:
                var returned = stream.Read(data, 0, data.Length);
                var rec = new String(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length));
                rec = rec.TrimEnd(''0');
                if (rec == "How are you?") {
                    var fine  = ASCIIEncoding.ASCII.GetBytes("fine and you?");
                    stream.Write(fine, 0, fine.Length);
                    }
                }
            client.Close();
            Console.ReadLine();
            }
        catch (Exception e) {
            Console.WriteLine("Error: " + e.StackTrace);
            Console.ReadLine();
            }
        }
    public static void Server() {
        try {
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            Console.WriteLine("*Starting TCP listener...");
            TcpListener listener = new TcpListener(ipAddress, 1500); // generally use ports > 1024
            listener.Start();
            Console.WriteLine("*Server is listening on " + listener.LocalEndpoint);
            Console.WriteLine("*Waiting for a connection...");
            while (true) {
                Socket client = listener.AcceptSocket();
                while (client.Connected) {
                    Console.WriteLine("*Connection accepted.");
                    Console.WriteLine("*Reading data...");
                    byte[] data = new byte[100];
                    int size = client.Receive(data);
                    Console.WriteLine("*Recieved data: ");
                    var rec = new String(ASCIIEncoding.ASCII.GetChars(data, 0, size));
                    rec = rec.TrimEnd(''0');
                    Console.WriteLine(rec);
                    if (client.Connected == false) {
                        client.Close();
                        break;
                        }
                    // you would write something back to the client here
                    if (rec == "Hello") {
                        client.Send(ASCIIEncoding.ASCII.GetBytes("How are you?"));
                        }
                    if (rec == "fine and you?") {
                        client.Disconnect(false);
                        }
                    }
                }
            listener.Stop();
            }
        catch (Exception e) {
            Console.WriteLine("Error: " + e.StackTrace);
            Console.ReadLine();
            }
        }
    }

请记住,通过套接字发送的数据可能会分段到达(在不同的数据包中)。 数据包很小时通常不会发生这种情况。