Advertisement
NAK

UDP Server

NAK
Apr 22nd, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Server
  10. {
  11.     class Program
  12.     {
  13.         private static byte[] data;
  14.         static void Main(string[] args)
  15.         {
  16.             data = System.Text.ASCIIEncoding.ASCII.GetBytes("<The packet of data>");
  17.             IPEndPoint ServerEndPoint= new IPEndPoint(IPAddress.Any,9050);
  18.             Socket WinSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  19.             WinSocket.Bind(ServerEndPoint);
  20.  
  21.             Console.Write("Waiting for client");
  22.             IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
  23.             EndPoint Remote = (EndPoint)(sender);
  24.             int recv = WinSocket.ReceiveFrom(data, ref Remote);
  25.             Console.WriteLine("\nMessage received from {0}:", Remote.ToString());
  26.             Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
  27.             Console.ReadLine();
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement