建立连接后如何从服务器(使用连接列表)向客户端发送命令?

How send a command from Server (using a List of connections) to Client after connection established?

我有这两个 类,它们是我的服务器应用程序(桌面)的一部分,需要在建立连接后向客户端发送回命令。

当我尝试这样做时:

clients[i].Send("info");

Send() 例程(Listener.cs)可以访问,但我有以下语法错误:

如何解决?

Listener.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

class Listener
{
    Socket s;
    string IP;
    public List<Info> clients;

    public delegate void ReceivedEventHandler(Listener l, Info i, string received);
    public event ReceivedEventHandler Received;
    public delegate void DisconnectedEventHandler(Listener l, Info i);
    public event DisconnectedEventHandler Disconnected;

    bool listening = false;
    public Listener()
    {
        clients = new List<Info>();
        s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    }

    public bool Running
    {
        get { return listening; }
    }

    public void BeginListen(int port)
    {
        s.Bind(new IPEndPoint(IPAddress.Any, port));
        s.Listen(100);
        s.BeginAccept(new AsyncCallback(AcceptCallback), s);
        listening = true;
    }

    public void StopListen()
    {
        if (listening == true)
        {
            s.Close();
        }
    }

    void AcceptCallback(IAsyncResult ar)
    {
        Socket handler = (Socket)ar.AsyncState;
        Socket sock = handler.EndAccept(ar);
        Info i = new Info(sock);
        clients.Add(i);

        Console.WriteLine("New Connection: " + i.ID.ToString());
        clients[i].Send("info");
        sock.BeginReceive(i.buffer, 0, i.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), i);
        handler.BeginAccept(new AsyncCallback(AcceptCallback), handler);
    }

    void ReadCallback(IAsyncResult ar)
    {
        Info i = (Info)ar.AsyncState;
        try
        {
            int rec = i.sock.EndReceive(ar);
            if (rec != 0)
            {
                string data = Encoding.ASCII.GetString(i.buffer, 0, rec);
                Received(this, i, data);
            }
            else
            {
                Disconnected(this, i);
                return;
            }

            i.sock.BeginReceive(i.buffer, 0, i.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), i);
        }
        catch
        {
            Disconnected(this, i);
            i.sock.Close();
            clients.Remove(i);
        }
    }
}

Info.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class Info
{
    public Socket sock;
    public Guid ID;
    public string RemoteAddress;
    public byte[] buffer = new byte[8192];

    public Info(Socket sock)
    {
        this.sock = sock; 
        ID = Guid.NewGuid(); 
        RemoteAddress = sock.RemoteEndPoint.ToString(); 
    }

    public void Send(string data)
    {
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
        {
            sock.EndSend(ar);
        }), buffer);
    }
}

Project of reference

clients[i].Send("info");

这种说法是不正确的。变量 'i' 引用信息 class 的一个实例。它不是列表中的整数索引。正确的调用如下。

i.Send("info");