Advertisement
Guest User

Untitled

a guest
Jan 29th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. File: UdpClient.cs
  2.  
  3. using System;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7.  
  8. namespace UdpClient
  9. {
  10.     class UdpClient
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.  
  15.             IPAddress destIp = IPAddress.Parse("192.168.198.130");
  16.  
  17.             IPEndPoint destEndpoint = new IPEndPoint(destIp, 5001);
  18.             string text =
  19.                 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah" +
  20.                 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah" +
  21.                 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah" +
  22.                 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah";
  23.        
  24.             Console.WriteLine("Started Client...");
  25.             while (true)
  26.             {
  27.  
  28.                 byte[] sendBuffer = Encoding.ASCII.GetBytes(text);
  29.                 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  30.            
  31.                 try
  32.                 {
  33.                     socket.SendTo(sendBuffer, destEndpoint);
  34.  
  35.                     socket.ReceiveTimeout = 3000;
  36.                     byte[] receiveBuffer = new byte[sendBuffer.Length];
  37.                     socket.Receive(receiveBuffer);
  38.                    
  39.                     socket.Close();
  40.                    
  41.                    
  42.                 }
  43.                 catch (Exception ex)
  44.                 {
  45.                     Console.WriteLine(ex.Message);
  46.                 }
  47.             }
  48.         }
  49.     }
  50. }
  51.  
  52.  
  53. File: UdpServer.cs
  54.  
  55. using System;
  56. using System.Net;
  57. using System.Net.Sockets;
  58.  
  59.  
  60. namespace UdpServer
  61. {
  62.     internal class UdpServer
  63.     {
  64.         private static void Main(string[] args)
  65.         {
  66.             const int listenPort = 5001;
  67.             UdpClient listener = new UdpClient(listenPort);
  68.             IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, listenPort);
  69.             byte[] buffer;
  70.  
  71.             Console.WriteLine("Started server...");
  72.             try
  73.             {
  74.                 while (true)
  75.                 {
  76.                     buffer = listener.Receive(ref endPoint);
  77.                     Console.WriteLine("Got session from: {0}", endPoint);
  78.                    
  79.                     listener.Send(buffer, buffer.Length, endPoint);
  80.  
  81.                 }
  82.             }
  83.             catch (Exception e)
  84.             {
  85.                 Console.WriteLine(e.ToString());
  86.             }
  87.             listener.Close();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement