Advertisement
Bunny83

TCPImageServer

Jun 12th, 2020
2,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ImageStreamServer
  13. {
  14.     public class Client
  15.     {
  16.         public TcpClient client;
  17.         public BinaryWriter writer;
  18.         public Client(TcpClient aClient)
  19.         {
  20.             client = aClient;
  21.             writer = new BinaryWriter(client.GetStream());
  22.         }
  23.         public bool SendImageData(byte[] aData)
  24.         {
  25.             if (!client.Connected)
  26.                 return false;
  27.             writer.Write(aData.Length);
  28.             writer.Write(aData);
  29.             return true;
  30.         }
  31.     }
  32.  
  33.  
  34.     class Program
  35.     {
  36.         TcpListener m_Server;
  37.         bool m_ServerRunning;
  38.         Thread m_ListenThread;
  39.  
  40.         bool m_Sending;
  41.         Thread m_SendingThread;
  42.         List<Client> m_Clients = new List<Client>();
  43.  
  44.         static void Main(string[] args)
  45.         {
  46.             var prg = new Program();
  47.             prg.Run();
  48.         }
  49.  
  50.         void ListenThread()
  51.         {
  52.             m_Server = new TcpListener(IPAddress.Any, 1234);
  53.             m_Server.Start();
  54.             m_ServerRunning = true;
  55.             while (m_ServerRunning)
  56.             {
  57.                 try
  58.                 {
  59.                     var newClient = m_Server.AcceptTcpClient();
  60.                     lock (m_Clients)
  61.                     {
  62.                         m_Clients.Add(new Client(newClient));
  63.                     }
  64.                 }
  65.                 catch (Exception e)
  66.                 {
  67.                     Console.Write(e.Message);
  68.                 }
  69.             }
  70.             lock(m_Clients)
  71.             {
  72.                 foreach(var c in m_Clients)
  73.                 {
  74.                     try
  75.                     {
  76.                         c.client.Close();
  77.                     }
  78.                     catch
  79.                     {
  80.                     }
  81.                 }
  82.                 m_Clients.Clear();
  83.             }
  84.         }
  85.  
  86.         class FileItem
  87.         {
  88.             public string name;
  89.             public byte[] data;
  90.         }
  91.  
  92.         void SendThread()
  93.         {
  94.             var folder = new DirectoryInfo("C:\\Data\\UA Images");
  95.             var fileNames = folder.GetFiles("*.png");
  96.             var files = new List<FileItem>();
  97.             foreach(var fn in fileNames)
  98.             {
  99.                 files.Add(new FileItem
  100.                 {
  101.                     data = File.ReadAllBytes(fn.FullName),
  102.                     name = fn.FullName
  103.                 } );
  104.             }
  105.             m_Sending = true;
  106.             Random r = new Random();
  107.             while (m_Sending)
  108.             {
  109.                 Thread.Sleep(500);
  110.  
  111.                 var file = files[r.Next(files.Count)];
  112.                 Console.WriteLine("Sending File: " + file.name);
  113.                 Client[] clients;
  114.                 lock(m_Clients)
  115.                 {
  116.                     clients = m_Clients.ToArray();
  117.                 }
  118.                 foreach (var client in clients)
  119.                 {
  120.                     bool success = false;
  121.                     try
  122.                     {
  123.                         success = client.SendImageData(file.data);
  124.                     }
  125.                     catch
  126.                     {
  127.                         success = false;
  128.                         client.client.Close();
  129.                     }
  130.                     finally
  131.                     {
  132.                         if (!success)
  133.                         {
  134.                             lock (m_Clients)
  135.                             {
  136.                                 m_Clients.Remove(client);
  137.                             }
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.  
  144.         private void Run()
  145.         {
  146.             m_ListenThread = new Thread(ListenThread);
  147.             m_ListenThread.Start();
  148.  
  149.             m_SendingThread = new Thread(SendThread);
  150.             m_SendingThread.Start();
  151.  
  152.             string line;
  153.             while ((line = Console.ReadLine()) != "stop")
  154.             {
  155.                 line = line.ToLower().Trim();
  156.                 if (line == "status")
  157.                 {
  158.                     lock (m_Clients)
  159.                     {
  160.                         Console.WriteLine("Connected clients: " + m_Clients.Count);
  161.                     }
  162.                 }
  163.                 else
  164.                 {
  165.                     Console.WriteLine(line);
  166.                 }
  167.             }
  168.             m_Sending = false;
  169.             m_ServerRunning = false;
  170.             m_Server.Stop();
  171.             m_ListenThread.Join();
  172.             m_SendingThread.Join();
  173.             Console.WriteLine("");
  174.             Console.WriteLine("Press <Enter> to quit");
  175.             Console.ReadLine();
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement