【问题标题】:Sending Screenshots using NetComm dll to Host使用 NetComm dll 将屏幕截图发送到主机
【发布时间】:2016-04-05 09:04:09
【问题描述】:

我正在使用 NetComm dll 来处理多用户。我的问题是当我发送文本时它工作正常,但当我截屏时它不起作用。 我的客户代码是

ms = new MemoryStream();
            bmpScreenshot.Save(ms, ImageFormat.Png);
            byte[] buffer;
            buffer  =imageToByteArray(bmpScreenshot);
            client.SendData(buffer);

而将图像转换为字节数组的函数是:

 public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

在接收端我是这样处理的:

    Stream stream = new MemoryStream(Data); // Data is byte array 
    pictureBox1.Image = Image.FromStream(stream);
    pictureBox1.Refresh();

收到后我在图片框中显示图像。

使用 NetComm dll 我只能发送和接收字节数组格式的数据。

NetComm dll 为我提供了通过使用其 ID 从客户端与另一个客户端进行通信的工具。当服务器启动时,它等待客户端,一旦客户端连接,它就开始以某种方式为它们提供 id,如 abc1、abc2、abc3。当 abc1 想要与 abc3 通信时,它只需输入 abc3 作为 id 而不是 IP 并发送消息,该消息应传递给 abc3。

如您所见,有两个客户端连接到服务器,并且都获得了类似 jack1 和 jack2 的 id。现在,如果他们想相互交流,他们只需在其中输入各自的 id 并发送消息。

【问题讨论】:

  • 或任何关于客户与客户沟通的建议,我的意思是当客户想向其他客户发送个人信息时,我会尝试什么方法?
  • 在什么意义上它不起作用?
  • 这个问题缺乏太多细节...您确实需要提供更多信息,例如您如何使用 NetComm 的minimal reproducible example,以便我们重现该问题。
  • 我想使用一个有多个客户端的tcp服务器,所有的客户端都可以通过提供一个客户端id相互通信。
  • 可能还有其他方法可以实现这一点,或者我做错了。因为当我从客户发送图片时,我希望它会出现在任何其他客户身上,但不会发生。如果是文本,它的工作正常。

标签: c# .net sockets client-server


【解决方案1】:

我尝试制作一个简单的客户端 -> 客户端消息发送,这将是一个位图。当我尝试将它与不同的端口一起使用时遇到了问题,但是端口被阻塞了,这就是问题所在。也检查一下。检查代码,看看是否有帮助:

namespace CommTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        NetComm.Host host = new NetComm.Host(10010);
        NetComm.Client client1 = new NetComm.Client();
        NetComm.Client client2 = new NetComm.Client();

        private void Form1_Load(object sender, EventArgs e)
        {
            host.StartConnection();
            client1.Connect("localhost", 10010, "Jack");
            client2.Connect("localhost", 10010, "Jack2");
            client2.DataReceived += client2_DataReceived;
            client1.SendData(imageToByteArray(Image.FromFile("Bitmap1.bmp")), "Jack2");
        }

        void client2_DataReceived(byte[] Data, string ID)
        {
            Stream stream = new MemoryStream(Data); // Data is byte array 
            pictureBox1.Image = Image.FromStream(stream);
            // pictureBox1.Refresh(); works without it
        }

        public byte[] imageToByteArray(Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, ImageFormat.Gif);
            return ms.ToArray();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            host.CloseConnection();
        }
    }
}

【讨论】:

  • 我也不能从服务器实现两个客户端。我的意思是我必须编写两个不同的客户端程序。我需要解释一下。
【解决方案2】:

一个完整的服务器/客户端实现可能是这样的。您不需要实现 2 个不同的客户端程序,但客户端和服务器应该不同。这是一个如何使用广播的例子>

程序.cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] argv)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        // if You run the program like "CommTest.exe 10010", than it will be host.
        // if You run it like "CommTest.exe localhost 10010", than it will be client connecting to localhost.
        if (argv.Length == 1)
        {
            Application.Run(new Form2(new Host(int.Parse(argv[0]))));
        }
        else
        {
            Application.Run(new Form1(new Client(argv[0], int.Parse(argv[1]))));
        }
    }
}

Form1.cs

public partial class Form1 : Form
{
    public Form1(NetComm.Client client)
    {
        _client = client;
        InitializeComponent();
    }

    // there is a button to broadcast picture on the client.
    private void Button1_Click(object sender, EventArgs e)
    {
        // update the image that should be broadcasted as You like.
        _client.SendData(imageToByteArray(Image.FromFile("Bitmap1.bmp")));
    }

    NetComm.Client _client;

    private void Form1_Load(object sender, EventArgs e)
    {
        button1.Click += Button1_Click;
        _client.DataReceived += client_DataReceived;
    }

    void client_DataReceived(byte[] Data, string ID)
    {
        Stream stream = new MemoryStream(Data); // Data is byte array 
        pictureBox1.Image = Image.FromStream(stream);
    }

    public byte[] imageToByteArray(Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, ImageFormat.Gif);
        return ms.ToArray();
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        _client.Disconnect();
    }
}

客户端.cs

class Client: NetComm.Client
{
    public Client(string ip, int port):base()
    {
        // in this example, the ids are not considered. In a real world situation Clients should send a first message to the host,
        // and host should reply with a free id. That id should be in the place of Guid.NewGuid().ToString()
        Connect(ip, port, Guid.NewGuid().ToString());
    }
}

Form2.cs

public partial class Form2 : Form
{
    public Form2(NetComm.Host host)
    {
        _host = host;
        InitializeComponent();
    }

    NetComm.Host _host;

    private void Form2_Load(object sender, EventArgs e)
    {
        button1.Click += Button1_Click;
    }

    // there is a button to close the connection on the host form.
    private void Button1_Click(object sender, EventArgs e)
    {
        _host.CloseConnection();
    }

    private void Form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        _host.CloseConnection();
    }
}

Host.cs

class Host: NetComm.Host
{
    public Host(int port):base(port)
    {
        StartConnection();
        DataReceived += Host_DataReceived;
    }

    void Host_DataReceived(string ID, byte[] Data)
    {
        Brodcast(Data);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多