连接建立后如何从服务器(使用连接列表)向客户端发送命令? [英] How send a command from Server (using a List of connections) to Client after connection established?

查看:29
本文介绍了连接建立后如何从服务器(使用连接列表)向客户端发送命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have these two classes that is part of my Server application (desktop) and need send a command back to Client after connection was established.

当我尝试这样做时:

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

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

the Send() routine (of Listener.cs) is accessible, but i have this following sintaxe error:

如何解决这个问题?

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

参考项目

推荐答案

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

这个说法是错误的.变量i"指的是 Info 类的一个实例.它不是列表中的整数索引.正确的调用如下.

This statement is incorrect. The variable 'i' refers to an instance of the Info class. It is not an integer index into the list. The correct call would be as below.

i.Send("info");

这篇关于连接建立后如何从服务器(使用连接列表)向客户端发送命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆