Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- namespace ClientUDP
- {
- class Cliente
- {
- static void Main(string[] args)
- {
- Client();
- }
- static void Client()
- {
- byte[] data = new byte[1024]; // Tamaño de la data
- IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999); // seteamos la ip y el puerto de escucha
- Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Creamos un objeto del tipo socket UDP
- string welcome = "Hello Server"; // string a enviar.
- data = Encoding.ASCII.GetBytes(welcome); // codificamos el string
- server.SendTo(data, data.Length, SocketFlags.None, ip); // enviamos la data, el tamaño, un flags none y la ip
- IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
- EndPoint Remote = (EndPoint)sender;
- data = new byte[1024];
- int receivedDataLength = server.ReceiveFrom(data, ref Remote);
- Console.WriteLine("Message received from {0}:", Remote.ToString());
- Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
- Console.Read();
- server.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment