Accepting a socket connection (simple file-server) : Socket Server : Network C# Examples


C# Examples » Network » Socket Server »

 

Accepting a socket connection (simple file-server)









    
using  System;
using  System.Collections.Generic;
using  System.Collections.Specialized;
using  System.IO;
using  System.IO.Compression;
using  System.Net;
using  System.Net.Mail;
using  System.Net.Sockets;
using  System.Runtime.InteropServices;
using  System.Text;

public  class  MainClass
{
        private  static  void  HandleRequest(object  state)
        {
                using  (Socket  client  =  (Socket)state)
                using  (NetworkStream  stream  =  new  NetworkStream(client))
                using  (StreamReader  reader  =  new  StreamReader(stream))
                using  (StreamWriter  writer  =  new  StreamWriter(stream))
                {
                        string  fileName  =  reader.ReadLine();
                        writer.Write(File.ReadAllText(fileName));
                }
        }
        public  static  void  Main()
        {
                using  (Socket  s  =  new  Socket(AddressFamily.InterNetwork,SocketType.Stream,  ProtocolType.Tcp))
                {
                        s.Bind(new  IPEndPoint(IPAddress.Loopback,  9999));
                        s.Listen(3);

                        Socket  client  =  s.Accept();
                        ThreadPool.QueueUserWorkItem(HandleRequest,  client);
                }
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Network
» Socket Server