rodrwan

ClienteUDP

Apr 15th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7.  
  8. namespace ClientUDP
  9. {
  10.     class Cliente
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Client();
  15.         }
  16.         static void Client()
  17.         {
  18.             byte[] data = new byte[1024]; // Tamaño de la data
  19.             IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999); // seteamos la ip y el puerto de escucha
  20.  
  21.             Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Creamos un objeto del tipo socket UDP
  22.  
  23.             string welcome = "Hello Server"; // string a enviar.
  24.             data = Encoding.ASCII.GetBytes(welcome); // codificamos el string
  25.             server.SendTo(data, data.Length, SocketFlags.None, ip); // enviamos la data, el tamaño, un flags none y la ip
  26.  
  27.             IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
  28.             EndPoint Remote = (EndPoint)sender;
  29.  
  30.             data = new byte[1024];
  31.             int receivedDataLength = server.ReceiveFrom(data, ref Remote);
  32.  
  33.             Console.WriteLine("Message received from {0}:", Remote.ToString());
  34.             Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
  35.             Console.Read();
  36.             server.Close();
  37.         }
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment