Hello everyone, I was finally able to make a server to which many clients can connect, but now the problem is different, how to send a message? When sending an error appears:

Request to send or receive data (when sending a datagram socket using a sendto call) no address was supplied

Server code:

static void Main(string[] args) { Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sck.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4001)); sck.Listen(0); TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 4000); TcpClient client = null; listener.Start(); while (true) { using (client = listener.AcceptTcpClient()) { Console.WriteLine("Подключен новый клиент"); byte[] buffer = Encoding.Default.GetBytes("бла блал бла"); sck.Send(buffer, 0, buffer.Length, 0); } } } 

Client code:

 static void Main(string[] args) { Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sck.Connect("127.0.0.1", 4001); TcpClient client = new TcpClient ("127.0.0.1", 4000); if (client.Connected) { Console.WriteLine("Подключено"); byte[] buff = new byte[255]; int num = sck.Receive(buff); string message = Encoding.Default.GetString(buff); Console.WriteLine(message); } Console.ReadKey(); } 

How to fix this error?

1 answer 1

It seems you have mixed sockets and higher level TcpListener and TcpClient . I would venture to suggest that you do not need sck at all. In the server code, you must send messages to the client through the client object; in the client code, use the client object to receive data.

The error occurs because you are trying to send data through a socket that is configured only as an inbound listener.