title: How to send file through socket in c#
body: <p>I have server and client console apps which communicate fine as well as sending some string. Here is the code...</p>
  
  <p>Server</p>
  
  <pre><code>public static void Main()
      {
          try
          {
              IPAddress ipAd = IPAddress.Parse("127.0.0.1"); 
  
              /* Initializes the Listener */
              TcpListener myList = new TcpListener(ipAd, 1234);
  
              /* Start Listeneting at the specified port */
              myList.Start();
  
              Console.WriteLine("The server is running at port 8001...");
              Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
              Console.WriteLine("Waiting for a connection.....");
  
              Socket s = myList.AcceptSocket();
              Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  
              byte[] b = new byte[100];
              int k = s.Receive(b);
              Console.WriteLine("Recieved...");
              for (int i = 0; i &lt; k; i++)
                  Console.Write(Convert.ToChar(b[i]));
  
              ASCIIEncoding asen = new ASCIIEncoding();
              s.Send(asen.GetBytes("The string was recieved by the server."));
              Console.WriteLine("\nSent Acknowledgement");
              /* clean up */
              s.Close();
              myList.Stop();
  
          }
          catch (Exception e)
          {
              Console.WriteLine("Error..... " + e.StackTrace);
          }
      }
  </code></pre>
  
  <p>Client</p>
  
  <pre><code>public static void Main()
      {
          try
          {
              TcpClient tcpclnt = new TcpClient();
              Console.WriteLine("Connecting...");
  
              tcpclnt.Connect("127.0.0.1", 1234);
  
              Console.WriteLine("Connected");
              Console.Write("Enter the string to be transmitted: ");
  
              String str = Console.ReadLine();
              Stream stm = tcpclnt.GetStream();
  
              ASCIIEncoding asen = new ASCIIEncoding();
              byte[] ba = asen.GetBytes(str);
              Console.WriteLine("Transmitting...");
  
              stm.Write(ba, 0, ba.Length);
  
              byte[] bb = new byte[100];
              int k = stm.Read(bb, 0, 100);
  
              for (int i = 0; i &lt; k; i++)
                  Console.Write(Convert.ToChar(bb[i]));
  
              tcpclnt.Close();
          }
  
          catch (Exception e)
          {
              Console.WriteLine("Error... " + e.StackTrace);
          }
      }
  </code></pre>
  
  <p>Now, I need to add the code algorithm which will send a file through the same apps. The key thing to implement is to use Socket.Send in the client. I am not sure about serialization and de-serialization of the the file.</p>
  
  <p>Any hint, advice, suggestion is more that welcome. Thanks.</p>