title: WiFi communication between C# and ESP8266
body: <p>I'm working on school project right now and I'm looking for the simplest way to connect C# with ESP8266 not via SerialPort (COM4) but via WiFi module on ESP8266 (I have to use this method). I have to build simple project to sending measured data from ESP to C# and also receiving my own defined control commands (strings like "LEDON" "LEDOFF" etc.) from C# to ESP just like remote control of measurement project. I have low knowledge in C# same as basics of servers/internet and things like that. I have everything done in Arduino IDE code but I'm stuck on C# cause I never before programmed there. I hope you understand my bad English and concept of my question. :)</p>
  
  <p>EDIT:</p>
  
  <p>Well, so I did some changes in my school project and now I am on this stage, where I need to solve this part of code. I hope that is last step to finish my project. All I have to do is solve Writing data from C# to ESP using unfilled method named "Writing" in following code:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  
  namespace ESP8266
  {
      public class Projekt
      {
          public event MessageEventHandler Message;
          public delegate void MessageEventHandler(Projekt sender, string Data);
  
          public TcpListener server;
          public Thread W_Thread;
          public Thread R_Thread;
          public bool IsLiserning = true;
  
          public TcpClient client;
          public StreamReader clientdata;
          public StreamWriter serverdata;
          public Projekt()
          {
              server = new TcpListener(IPAddress.Parse(Dns.GetHostEntry(Dns.GetHostName().ToString()).AddressList[1].ToString()), 5000);
              server.Start();
  
              W_Thread = new Thread(new ThreadStart(Writing));
              W_Thread.Start();
  
              R_Thread = new Thread(new ThreadStart(Reading));
              R_Thread.Start();
          }
  
          public void Reading()
          {
              while (IsLiserning == true)
              {
                  if (server.Pending() == true)
                  {
                      client = server.AcceptTcpClient();
                      clientdata = new StreamReader(client.GetStream());
                  }
                  try
                  {
                      Message?.Invoke(this, clientdata.ReadLine());
                  }
                  catch (Exception){}
              }
          }
  
          public void Writing()
          {
              while (IsLiserning == true)
              {
                  if (server.Pending() == true)
                  {
                      client = server.AcceptTcpClient();
                      serverdata = new StreamWriter(client.GetStream());
                  }
                  try
                  {
                      //NEED_TO_SOLVE_THIS_PART
                  }
                  catch (Exception){}
              }
          }
      }
  }
  </code></pre>
  
  <p>Maybe there is missing something more than that part of code and you guys are able to help me I hope :) Thanks for all answers by the way :)</p>