I need to send a response to the client after connecting 1 byte. The client connects, according to the manual, after connecting, I must send a response in the form of one byte (0x01), after sending nothing comes to me. Am I doing right, here's the code:

class Program { static void Main(string[] args) { Receiver(); } public static void Receiver() { byte answer = 0x01; Console.WriteLine("Enter port :"); int port = int.Parse(Console.ReadLine()); TcpListener server = new TcpListener(IPAddress.Any, port); server.Start(); do { Console.WriteLine("ОТиданиС ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ... "); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ входящСС ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ TcpClient client = server.AcceptTcpClient(); Console.WriteLine("ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ ΠΊΠ»ΠΈΠ΅Π½Ρ‚. Π’Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ запроса..."); StreamReader st = new StreamReader(client.GetStream(), Encoding.Default); StreamWriter sr = new StreamWriter(client.GetStream(), Encoding.ASCII); sr.AutoFlush = true; string answerFromBlock; while (client.Connected) { answerFromBlock = st.ReadLine(); if (!String.IsNullOrEmpty(answerFromBlock)) { Console.WriteLine("ΠžΡ‚Π²Π΅Ρ‚ ΠΎΡ‚ Π±Π»ΠΎΠΊΠ°: {0}", answerFromBlock); sr.Write(answer); string path = "logs.txt"; using (StreamWriter writer = new StreamWriter(path, true)) { writer.AutoFlush = true; writer.WriteLine(answerFromBlock); } Console.WriteLine("ОТиданиС Π½ΠΎΠ²Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ…."); } } } while (true); } } 

The client is a GPS unit on the car. My first experience of sending or receiving data.

  • so synchronously written ... (can it be better done through events and / or Task?); did not see that server was used somewhere except creation of the client. - dgzargo
  • @dgzargo synchronously for tests, no need to keep more connections for now. - Winteriscoming
  • I do not understand: the server should send the answer itself when you send 0x01? - dgzargo
  • @dgzargo no, when the block is connected, you need to send 1 byte to the block. The block is connected, his first message is to receive his identifier, after his message I send him 1 byte immediately, after that he should start sending new messages with coordinate information, but he is silent, so I asked this question to make sure that I am correct wrote. - Winteriscoming
  • write in more detail how everything should happen (the previous comment confuses). It is desirable to correct the question - dgzargo

0