Working with TCP/IP packets!!!

cmaras

Member
Joined
Jul 1, 2003
Messages
13
Hello,

This is what I want to do, on the network some TCP/IP UDP Broadcast packages are sent like "255.255.255.255:7778 UDP".

What I want to do is for the app (or service if that is better) to listen for packets with a:

Destination IP = 255.255.255.255
Port= 7778
Protocol= UDP

pickup each up and change the Destination Address of "255.255.255.255" of the IP packet to another IP address and then send it on its way.

I dont even have a clue where to begin, I have never programmed on a network/packet level so dont know even what namespace to use. And adding to that Im pretty new to programing as a whole so please have patience with me. But I do know networking and IP communication, my background is with-in Network Engineering.

Thank you for listening,
Claudio
 
Ok, I have taken a hard look at the System.Net.Sockets and in particular the UDPClient Class. It seems like it should be able to do what I like I just cant figure out how to use it.

Ive seen lots of examples and used lots of the code just to try it out but what is troubling me most is that I dont know exactly how it works.

Example:

UDPClient.Recieve
UDPClient.Send
UDPClient.Connect

And the IPEndPoint what does it do how do you use it, if i understood it right its the address of the computer you recieved something from.

In my code the code freezes when I execute the UDPClient.Recieve and I guess its because its waiting for a packet. So question is how can I fire it only when a packet is recieved and how do I read the information in a packet?

Well I guess what I need is somebody who really knows how this Class works to do a little session in explaning how these parts work together and what is required to set up a UDPClient that listens on a specific port and for a specific destination address and pick these packets up.

Again Derek thank you for you help on the way, if you know more please share it if you have the time.

Claudio
 
I have been playing with UDP broadcasts over the last couple of days and what i want to do is very similar,

Im listening for UDP broadcasts and then trying to reply to who ever has broadcast with a msg, and i just cant seem to get it to reply to braodcasts, if i put a fixed ip in it replies fine :/

Heres my code anyways... (the Hash bit is me encoding the msg so you can pretty much ignore that in your case)

private void button1_Click(object sender, System.EventArgs e) {
Byte [] buffer = Encoding.Default.GetBytes(Hash("HELLO|"));

IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9876);
EndPoint ep = (EndPoint)iep;

IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint rEp = (EndPoint) RemoteIpEndPoint;

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UdpClient udp = new UdpClient();

//s.Connect(ep);
//s.Send(buffer);
udp.Send(buffer, buffer.Length, "255.255.255.255", 9876);

try {
Byte[] receiveBytes = new Byte[1024];
//s.Receive(receiveBytes);
udp.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
MessageBox.Show(returnData.ToString());
udp.Close();
} catch (Exception se) { System.Windows.Forms.MessageBox.Show(se.ToString());}
}

private string Hash(string data){
string temp ="";
foreach(char c in data) {
temp += (char)((int)c^128);
}
return temp;
}
 
Oh yeh and its an event for a button :)

(im goign to repost this as another thread cause i need help on this)
 
Cheka,

Thank you, for your help I kinda put this project on hold as work has been a bit busy lately.

But now I will have another go at it... :)

Thanx a lot,
Claudio
 
Ive fixed my app and stuff now, so if you need any help with broadcasting in c# just gimmie a shout,

it took me ages to fix little errors around like 20 hours or summit for one line of code :D

And even thou i asked for help im glad no-one replied! (sounds lame but i means iev done it myself \o/)
 
Cheka,

My ambisions were to work with my app but havent got that far yet, Im just picking up C# myself so that could be my first project. :)

I sent you my email address in a PM, easier to communicate that way....if you dont mind.

Thanks again for the help...

Claudio
 
Back
Top