Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- File: UdpClient.cs
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- namespace UdpClient
- {
- class UdpClient
- {
- static void Main(string[] args)
- {
- IPAddress destIp = IPAddress.Parse("192.168.198.130");
- IPEndPoint destEndpoint = new IPEndPoint(destIp, 5001);
- string text =
- "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah" +
- "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah" +
- "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah" +
- "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah";
- Console.WriteLine("Started Client...");
- while (true)
- {
- byte[] sendBuffer = Encoding.ASCII.GetBytes(text);
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- try
- {
- socket.SendTo(sendBuffer, destEndpoint);
- socket.ReceiveTimeout = 3000;
- byte[] receiveBuffer = new byte[sendBuffer.Length];
- socket.Receive(receiveBuffer);
- socket.Close();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
- }
- File: UdpServer.cs
- using System;
- using System.Net;
- using System.Net.Sockets;
- namespace UdpServer
- {
- internal class UdpServer
- {
- private static void Main(string[] args)
- {
- const int listenPort = 5001;
- UdpClient listener = new UdpClient(listenPort);
- IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, listenPort);
- byte[] buffer;
- Console.WriteLine("Started server...");
- try
- {
- while (true)
- {
- buffer = listener.Receive(ref endPoint);
- Console.WriteLine("Got session from: {0}", endPoint);
- listener.Send(buffer, buffer.Length, endPoint);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- listener.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement