rodrwan

ServidorUDP

Apr 15th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 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. using System.Threading;
  8.  
  9. namespace ServerUDP
  10. {
  11.     class Server
  12.     {
  13.         static void Main()
  14.         {
  15.             Servers();
  16.         }
  17.  
  18.         static void Servers()
  19.         {
  20.             int receivedDataLength;
  21.             byte[] data = new byte[1024];
  22.             byte[] resp = new byte[1024];
  23.             IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
  24.  
  25.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  26.  
  27.             socket.Bind(ip);
  28.  
  29.             IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
  30.             EndPoint Remote = (EndPoint)(sender);
  31.  
  32.             while (true)
  33.             {
  34.                 data = new byte[1024];
  35.                 receivedDataLength = socket.ReceiveFrom(data, ref Remote);
  36.                 Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
  37.                 string hello = "Hello Client";
  38.                 resp = Encoding.ASCII.GetBytes(hello);
  39.                 socket.SendTo(resp, resp.Length, SocketFlags.None, Remote);
  40.             }
  41.  
  42.         }
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment