Advertisement
Guest User

Archivos por Sockets

a guest
Mar 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace SocketExample
  9. {
  10.     class MainClass
  11.     {
  12.         public static void Main(string[] args)
  13.         {
  14.             new Thread(MainClass.Servidor)
  15.                 .Start();
  16.  
  17.             Thread.Sleep(1000);
  18.  
  19.             new Thread(MainClass.Cliente)
  20.                 .Start();
  21.         }
  22.  
  23.         public static void Servidor()
  24.         {
  25.             IPHostEntry datosAnfitrion = Dns.GetHostEntry(Dns.GetHostName());  
  26.             IPAddress direccionIP = datosAnfitrion.AddressList[0];
  27.             IPEndPoint puntoFinal = new IPEndPoint(direccionIP, 11000);
  28.  
  29.             Socket escuchador = new Socket(
  30.                 direccionIP.AddressFamily,
  31.                 SocketType.Stream,
  32.                 ProtocolType.Tcp
  33.             );
  34.  
  35.             escuchador.Bind(puntoFinal);
  36.             escuchador.Listen(10);
  37.            
  38.             while (true)
  39.             {
  40.                 Console.WriteLine("Esperando conexión...");
  41.  
  42.                 Socket socket = escuchador.Accept();
  43.  
  44.                 Console.WriteLine("Conexión establecida! Enviando...");
  45.  
  46.                 FileStream archivo = null;
  47.  
  48.                 switch (".jpg")
  49.                 {
  50.                     case ".jpg":
  51.                         archivo = File.OpenWrite("recibido.jpg");
  52.                         break;
  53.                     case ".txt":
  54.                         archivo = File.OpenWrite("recibido.txt");
  55.                         break;
  56.                 }
  57.  
  58.                 byte[] buffer = new byte[1024];
  59.                 int leidos = 0;
  60.  
  61.                 while ((leidos = socket.Receive(buffer)) > 0) {
  62.                     archivo.Write(buffer, 0, leidos);
  63.                 }
  64.  
  65.                 archivo.Close();
  66.  
  67.                 Console.WriteLine("Listo!");
  68.  
  69.                 socket.Shutdown(SocketShutdown.Both);
  70.                 socket.Close();
  71.             }
  72.            
  73.         }
  74.  
  75.         public static void Cliente()
  76.         {
  77.             IPHostEntry datosAnfitrion = Dns.GetHostEntry(Dns.GetHostName());
  78.             IPAddress direccionIP = datosAnfitrion.AddressList[0];
  79.             IPEndPoint puntoFinal = new IPEndPoint(direccionIP, 11000);
  80.  
  81.             while (true)
  82.             {
  83.                 Socket socket = new Socket(
  84.                     direccionIP.AddressFamily,
  85.                     SocketType.Stream,
  86.                     ProtocolType.Tcp
  87.                 );
  88.                 socket.Connect(puntoFinal);
  89.                 socket.SendFile("archivo.jpg");
  90.                 socket.Close();
  91.  
  92.                 Thread.Sleep(10000);
  93.             }
  94.  
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement