2

我已经在服务器和客户端之间建立了连接,客户端向服务器发送数据并接收响应,但我想从服务器向客户端发送数据并从客户端接收响应,这可能吗?这是我的代码:[客户端]:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Multi_Client
{
    class Program
    {
        private static Socket _clientSocket = new Socket
            (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        static void Main(string[] args)
        {
            Console.Title = "Client";
            ConnectToServer();
            RequestLoop();
            Exit();
        }

        private static void ConnectToServer()
        {
            int attempts = 0;

            while (!_clientSocket.Connected)
            {
                try
                {
                    attempts++;
                    Console.WriteLine("Connection attempt " + attempts);
                    _clientSocket.Connect(IPAddress.Loopback, 100);
                }
                catch (SocketException)
                {
                    Console.Clear();
                }
            }

            Console.Clear();
            Console.WriteLine("Connected");
            _clientSocket.Send(Encoding.ASCII.GetBytes("C1"));
        }

        private static void RequestLoop()
        {
            Console.WriteLine(@"<Type ""exit"" to properly disconnect client>");

            SendRequest();
            ReceiveResponse();

        }

        /// <summary>
        /// Close socket and exit app
        /// </summary>
        private static void Exit()
        {
            SendString("exit"); // Tell the server we re exiting
            _clientSocket.Shutdown(SocketShutdown.Both);
            _clientSocket.Close();
            Environment.Exit(0);
        }

        private static void SendRequest()
        {
            Console.Write("Send a request: ");
            string request = Console.ReadLine();
            SendString(request);

            if (request.ToLower() == "exit")
            {
                Exit();
                return;
            }
        }

        private static void SendString(string text)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(text);
            _clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
        }

        private static void ReceiveResponse()
        {
            byte[] buffer = new byte[2048];
            int received = _clientSocket.Receive(buffer, SocketFlags.None);

            if (received == 0) return;

            byte[] data = new byte[received];
            Array.Copy(buffer, data, received);
            string text = Encoding.ASCII.GetString(data);
            Console.WriteLine(text);
        }
    }
}

[服务器] :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using NetworkCommsDotNet;
using System.Net.Sockets;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private static List<Socket> _clientSockets = new List<Socket>();
        private static readonly int _BUFFER_SIZE = 2048;
        private static byte[] _buffer = new byte[_BUFFER_SIZE];
        string text;
        Socket current;
        Socket test;
        delegate void SetTextCallback(string text);

        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.richTextBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.richTextBox1.Text += text + "\n";
            }
        }
        private void SetupServer()
        {
            SetText("Setting up server...");
            _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
            _serverSocket.Listen(5);
            _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
            SetText("Server Setup");
        }

        /// <summary>
        /// Close all connected client (we do not need to shutdown the server socket as its connections
        /// are already closed with the clients)
        /// </summary>
        private static void CloseAllSockets()
        {
            foreach (Socket socket in _clientSockets)
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }

            _serverSocket.Close();
        }

        private void AcceptCallback(IAsyncResult AR)
        {
            Socket socket = null;

            try
            {
                socket = _serverSocket.EndAccept(AR);
            }
            catch (ObjectDisposedException) // I cannot seem to avoid this (on exit when properly closing sockets)
            {
                return;
            }

            _clientSockets.Add(socket);
            socket.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
            SetText("Client connected, waiting for request...");
            test = (Socket)AR.AsyncState;
            _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
        private void ReceiveCallback(IAsyncResult AR)
        {
            current = (Socket)AR.AsyncState;
            int received = 0;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException)
            {
                SetText("Client forcefully disconnected");
                current.Close(); // Dont shutdown because the socket may be disposed and its disconnected anyway
                _clientSockets.Remove(current);
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(_buffer, recBuf, received);
            text = Encoding.ASCII.GetString(recBuf);
            SetText("Received Text: " + text);
            if (text.ToLower() == "get tim") // Client requested time
            {
                SetText("Text is a get time request");
                byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
                current.Send(data);
                SetText("Time sent to client");
            }
            else if (text.ToString() == "C1") // Client wants to exit gracefully
            {
                SetText("Received :: C1");
                byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
                current.Send(data);
            }

            else
            {
                SetText("Server is sending invalid packet");
                SetText("Warning Sent");
            }
            current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveCallback), current);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetupServer();
        }
        private void Send_Received(string mess)
        {
            byte[] data = Encoding.ASCII.GetBytes(mess);
            test.Send(data);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            CloseAllSockets();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Send_Received(textBox1.Text);
        }


    }
}
4

3 回答 3

3

简短的回答:是的,这是可能的。

长答:是的。您当前的代码是这样实现的:

  • 服务器初始化并准备接收连接。
  • 客户端尝试连接。
  • 服务器接受连接,添加到 clientSockets 集合。
  • 客户端发送“C1”。
  • 服务器接收内容、解释并发送回答案。
  • 客户收到答复。

然后你进入一个循环,因为没有其他内容被发送。您实现了一个同步协议 - 客户端对话,服务器对话,客户端将其转储到控制台。

让我建议一个快速实现,以便您可以测试服务器->客户端->服务器交换:

  • 在您的服务器上实现一个异步计时器。
  • 10 秒后,如果没有来自特定客户端的传入消息到达,则发送“PING?” 给它留言。
  • 在客户端解析传入的内容。如果消息等于“PING?”,则回答“PONG!”。

这是一个非常简单的 QoS 协议 - 您将测试连接的健康状况。

如果您设法实施它,请告诉我们。=)

于 2013-08-19T03:46:16.373 回答
0

实际上,我知道问题出在哪里,当服务器向客户端发送一些东西而客户端还没有发送一些东西时,客户端在发送另一个东西之前不会显示这个东西:O示例(我知道这很复杂):服务器发送 C1 并在控制台中显示 C1 发送给客户端:客户端什么都不做(他应该在控制台 C1O 中显示)客户端发送 Pa(随机)并在控制台中显示 CAO:服务器接收 Pa 客户端发送 Ka(随机)并显示在控制台(无效请求,因为服务器已收到 Pa,因此它发送无效请求文本)我希望它是干净的,即使我知道它不是:S

于 2013-08-20T02:20:36.543 回答
0

我通过编辑 RequestLoop 函数解决了这个问题:

private static void RequestLoop()
    {
        ReceiveResponse();

    }

就是这样:)

于 2013-08-24T01:58:58.137 回答