0

I'm working on a chatbox app using C# on Visual Studio. I have a console server and a chat with a graphical interface. I succeed to send data : chatbox -> server. And also server -> chatbox but only for the chatbox that has sent the info (When chatbox has sent an info, the server has to send it to everyone, but it works only for the sender). I want that all app receive the info. Actually : App 1 -> Server Server -> App 1 Not App 2 I don't understand why. Thanks for your future help.

        public void start()
    {
        TcpListener l = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), port);
        l.Start();

        while(true)
        {
            TcpClient comm = l.AcceptTcpClient();
            Console.WriteLine("Connection established @" + comm);
            new Thread(new Receiver(comm).doOperation).Start();
        }
    }

class Receiver
    {
        private TcpClient comm;

        public Receiver(TcpClient s)
        {
            comm = s;
        }

        public void doOperation()
        {
            Semaphore s = new Semaphore(1, 1);
          while (true)
             {
                s.WaitOne();
                NetworkStream nwStream = comm.GetStream();
                byte[] buffer = new byte[comm.ReceiveBufferSize];

                //---read incoming stream---
                int bytesRead = nwStream.Read(buffer, 0, comm.ReceiveBufferSize);

                //---convert the data received into a string---
                string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received " + dataReceived);

                s.Release();
                s.WaitOne();

                Console.WriteLine("Send to all : " + dataReceived);
                nwStream.Write(buffer, 0, bytesRead);
                s.Release();


            }

        }
    }

And I receive the info on the chatbox:

public Form1(string h, int p)
    {
       ...

        comm = new TcpClient(h, p);
        nwStream = comm.GetStream();


        Thread th1 = new Thread(receive);
        th1.Start();



    }

    public void receive()
    {
        while (true)
        {
            byte[] bytesToRead = new byte[comm.ReceiveBufferSize];
            int bytesRead = nwStream.Read(bytesToRead, 0, comm.ReceiveBufferSize);
            MessageBox.Show("Appli info received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));

        }
    }

1 Answers1

0

What kind of protocol are you using between [sever <-> client] ?

You need to implement WebSocket protocol on the both side. Server will be able to send events to clients.

After the protocol recognition question, you just need to build / call events to C# back-end side and reveivers javascript side.

Jmo
  • 41
  • 4
  • I use TCP. The server is able to send events to clients, only for the one who sent it. If I run 2 app and the first send an event, the server send him back, but not for the second app. –  Dec 08 '18 at 23:09
  • Depending about how you manage the actives connections. I have the same subject in this time. But ur App just store the clients sessions, when we want to send an event, we have just to iterate on the clients sessions Id and send an event, permitted because of WebSocket protocol. – Jmo Dec 08 '18 at 23:25
  • How can I do that ? To help me, I used this : https://stackoverflow.com/questions/10182751/server-client-send-receive-simple-text –  Dec 09 '18 at 08:20
  • You can build a List server object. Add every client when he connect to server, delete when he leave. If you need to send a message to every connected client, loop on this object with a foreach and send message to everyone. Here is a websocket subject https://stackoverflow.com/questions/10200910/creating-a-hello-world-websocket-example – Jmo Dec 09 '18 at 08:44