title: Send data to server and store in SQL Server using sockets
body: <p>I am trying to use socket programming for receiving some information from client side (for example: disk usage or something like that).</p>
  <p>I am using async method for multiple clients and tried to write to SQL Server but I didn't store whole information to SQL Server. Communication stream over byte array between client and server but how can I store this byte array to SQL Server during the communication?</p>
  <hr />
  <p>Sorry for the late response guys,</p>
  <p>Here is the code. There are 2 projects. one of them is server and the other one is client. there are 2 classes in server project.</p>
  <p>Classes;</p>
  <pre><code>class Listener
  {
      private Socket _socket;
  
      public bool Listening { get; private set; }
      public int Port { get; private set; }
  
      public Listener(int port)
      {
          Port = port;
          _socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
      }
  
      public void Start()
      {
          if (Listening)
          {
              return;
          }
  
          _socket.Bind(new IPEndPoint(0,Port));
          _socket.Listen(0);
          _socket.BeginAccept(callback, null);
          Listening = true;
      }
  
      public void Stop()
      {
          if (!Listening)
          {
              return;
          }
  
          _socket.Close();
          _socket.Dispose();
          _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      }
  
      void callback(IAsyncResult ar)
      {
          try
          {
              Socket socket = this._socket.EndAccept(ar);
  
              if (SocketAccepted!=null)
              {
                  SocketAccepted(socket);
              }
              this._socket.BeginAccept(callback, null);
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  
      public delegate void SocketAcceptedHandler(Socket e);
  
      public event SocketAcceptedHandler SocketAccepted;
  }
  
  
  class Client
  {
      public string ID { get; private set; }
      public IPEndPoint EndPoint { get; private set; }
  
      private Socket sck;
  
      public Client(Socket accepted)
      {
          sck = accepted;
          ID = Guid.NewGuid().ToString();
          EndPoint = (IPEndPoint)sck.RemoteEndPoint;
          sck.BeginReceive(new byte[] {0}, 0, 0, 0, callback, null);
      }
  
      void callback(IAsyncResult ar)
      {
          try
          {
              sck.EndReceive(ar);
  
              byte[] buf=new byte[8192];
              int rec = sck.Receive(buf, buf.Length, 0);
  
              if (rec&lt;buf.Length)
              {
                  Array.Resize&lt;byte&gt;(ref buf, rec);
              }
              if (Received!=null)
              {
                  Received(this, buf);
              }
  
              sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
  
          }
          catch (Exception ex)
          {
              //MessageBox.Show(ex.Message);
              sck.Close();
  
              if (Disconnected!=null)
              {
                  Disconnected(this);
              }
          }
          
      }
      
      public void Close()
      {
          sck.Close();
          sck.Dispose();
      }
      public delegate void ClientReceivedHandler(Client sender, byte[] data);
      public delegate void ClientDiscconnectedHandler(Client sender);
  
      public event ClientReceivedHandler Received;
      public event ClientDiscconnectedHandler Disconnected;
  }
  </code></pre>
  <p>Server project Main Form:</p>
  <p><a href="https://i.stack.imgur.com/IBcyZ.png" rel="nofollow noreferrer">Server Application</a></p>
  <pre><code>public partial class Main : Form
  {
      private static Listener _listener;
  
      public Main()
      {
          InitializeComponent();
          _listener = new Listener(8);
          _listener.SocketAccepted += new Listener.SocketAcceptedHandler(_listenerSocketAccepted);
          _listener.Start();
  
      }
  
      private void _listenerSocketAccepted(Socket e)
      {
          Client client = new Client(e);
          client.Received += new Client.ClientReceivedHandler(client_Received);
          client.Disconnected += new Client.ClientDiscconnectedHandler(client_Disconnected);
  
  
          Invoke((MethodInvoker)delegate
          {
              ListViewItem i=new ListViewItem();
              i.Text = client.EndPoint.ToString();
              i.SubItems.Add(client.ID);
              i.SubItems.Add(&quot;XXX&quot;);
              i.SubItems.Add(&quot;XXX&quot;);
              i.Tag = client;
              listViewClients.Items.Add(i);
  
              //textBox1.Text = &quot;New Connection : &quot; + e.RemoteEndPoint + &quot;    Time : &quot; + DateTime.Now;
          });
      }
  
      private void client_Disconnected(Client sender)
      {
          Invoke((MethodInvoker)delegate
          {
              for (int i = 0; i &lt; listViewClients.Items.Count; i++)
              {
                  Client client=listViewClients.Items[i].Tag as Client;
  
                  if (client.ID==sender.ID)
                  {
                      listViewClients.Items.RemoveAt(i);
                      break;
                  }
              }
          });
      }
  
      private void client_Received(Client sender, byte[] data)
      {
          Invoke((MethodInvoker)delegate
          {
              for (int i = 0; i &lt; listViewClients.Items.Count; i++)
              {
                  Client client = listViewClients.Items[i].Tag as Client;
  
                  if (client.ID == sender.ID)
                  {
                      listViewClients.Items[i].SubItems[2].Text = Encoding.Default.GetString(data);
                      listViewClients.Items[i].SubItems[3].Text = DateTime.Now.ToString();
                      break;
                  }
              }
          });
      }
  
      private void Main_Load(object sender, EventArgs e)
      {
  
      }
  }
  </code></pre>
  <p>Client Side Main Form:</p>
  <p><a href="https://i.stack.imgur.com/oQNRK.png" rel="nofollow noreferrer">Client Application</a></p>
  <pre><code>public partial class Main : Form
  {
      Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      public Main()
      {
          InitializeComponent();
      }
  
      private void btnConnect_Click(object sender, EventArgs e)
      {
          sck.Connect(&quot;127.0.0.1&quot;, 8);
          MessageBox.Show(&quot;Connected&quot;);
      }
  
      private void btnSend_Click(object sender, EventArgs e)
      {
          int s = sck.Send(Encoding.Default.GetBytes(txtMsg.Text));
  
          if (s&gt;0)
          {
              MessageBox.Show(&quot;Message Sent..&quot;);
          }
      }
  
      private void btnClose_Click(object sender, EventArgs e)
      {
          sck.Close();
          sck.Dispose();
          Close();
      }
  
      private void Main_Load(object sender, EventArgs e)
      {
          //lis
      }
  }
  </code></pre>
  <p>I have 2 questions.</p>
  <p>1- How can I insert a data to sql came from client?</p>
  <p>2- the listview item flickers and does not go away, when I close my client app with btnClose.</p>