H
H
HolterST2021-08-31 12:07:34
C++ / C#
HolterST, 2021-08-31 12:07:34

TCP C# / How to send a message to a client in a method?

Currently, when connecting to the server, a message is sent to the client.
How can I implement a method that, when called, will send some other message to the connected user?
What to write in the Send method to send any message to the client?
That is, when calling the Send method, you need to send some message to the connected client, for example, "New message"

public class Server : MonoBehaviour

    {

        public TcpListener tcpListener;
        public TcpClient tcpClient;


    void Awake()
    {
                   
        tcpListener = new TcpListener(IPAddress.Parse("91.281.562.30"),4672);
        tcpListener.Start();
        tcpListener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback),null);
                
    }





          public void DoAcceptTcpClientCallback(IAsyncResult ar)
    {
  

    TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
        Debug.Log("Client connected completed");
    
    
    //===========ОТПРАВКА СООБЩЕНИЯ ПОДКЛЮЧИВШЕМУСЯ КЛИЕНТУ====================
    NetworkStream stream = tcpClient.GetStream();
    string message = "Вы подключились к серверу!";
    byte[] bytes = Encoding.ASCII.GetBytes(message);
    stream.Write(bytes, 0, bytes.Length);
    stream.Flush();
    //=========================================================================
    tcpListener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback),null);
    
    }
    
    
    public void Send()
    {
      //???????????????????????????????????
    }
    
    
    
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2021-08-31
@sarapinit

Look here https://metanit.com/sharp/net/4.2.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question