Skip to content

Instantly share code, notes, and snippets.

@salihkaragoz
Last active April 21, 2017 14:54
Show Gist options
  • Save salihkaragoz/196b0900600479a127c0e6c11fbc0631 to your computer and use it in GitHub Desktop.
Save salihkaragoz/196b0900600479a127c0e6c11fbc0631 to your computer and use it in GitHub Desktop.
You can catch UDP packet as specified port and IP address
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 600);
UdpClient newsock = new UdpClient(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
data = newsock.Receive(ref sender);
Console.WriteLine("Message received from {0}:", sender.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
newsock.Send(data, data.Length, sender);
int serino = 1;
int serino2 = 1;
while (true)
{
data = newsock.Receive(ref sender);
string[] words = Encoding.ASCII.GetString(data, 0, data.Length).Split(',');
if (serino.ToString()== words[0] && words[1]=="192.168.85.1")
{ Console.WriteLine("Errorrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
serino++;
}
if(serino2.ToString()==words[0] && words[1] == "192.168.1.61")
{
serino2++;
Console.WriteLine("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
}
Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
// Console.WriteLine(words[0]);
newsock.Send(data, data.Length, sender);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment