Send a flag from server to clients using c# mouse events

戏子无情 提交于 2019-12-05 13:11:42

Firstly, about the MouseClick event. Since you are having exclusive qualification for the event (that is, one left click and another right click), you could combine them both into a single event

public Form1()
{
    InitializeComponent();
    this.MouseClick += mouseClick1; //one event is enough

    Thread thread = new Thread(() => StartServer(message));
    thread.Start();  // server is begining
}

private void mouseClick1(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        try
        {
            obj = new Capturer(dirPath + name + "_" + surname, 20); //captures the kinect streams
        }
        catch (Exception e1)
        {
            Console.WriteLine("The process failed: {0}", e1.ToString());
        }
    } 
    else if (e.Button == MouseButtons.Right)
    {
        obj.flag2 = true; // flag that handle the recording, true value stops the recording, possible I want that value to be send to the client in order the same thing happen.
    }
}

And it is going to be ok.

Next, answering your questions:

Q: How can I parse the flag into the server, in order to send that value in the clients?

A: in your mouseClick1 event, simply use sync send which you do in your accept callback, change the msg with something else (say byte[] val = new byte[] { 1 };)

foreach (Socket socket1 in clientSockets) //Do this in your `if (e.Button == Mouse.Left and Mouse.Right) blocks
    socket1.Send(Encoding.ASCII.GetBytes(msg));

Q: Is it possible to change here the static type of server functions?

A: Yes, definitely! it is windows form, you do not need to use static type at all! Unlike the Console App in your previous question. I would even suggest you to make nothing static whenever possible

Q: As it is now the code begin the server which send just the string "start" to the clients. How can I send the string message of a bool flag?

A: since you use socket, you cannot really send bool flag so to speak. You will send byte[]. But you can always check in your client implementation. If the byte[] is of certain value, simply change it to bool. For instance, consider of sending just 1 or 0 from your server. Then in your client endReceiveCallback, you could simply check the data coming and see if it is 1 then it is true, and if it is 0 then it is false

Q: My issue lies in the static type of my callback functions. Is it possible to add as an argument the message for example in the AsyncCallBack

A: this is winform, you could get rid of all static var! And yes, just pass it as replacement of the null in your Begin callback. Then you pass the object too.

//Change null with something else
serverSocket.BeginAccept(new AsyncCallback(acceptCallback), myobj);

Then in your acceptCallback, take your object by using the IAsyncResult.AsyncState

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!