Markus Stoeger wrote:
I don't have the source code here right now, but I think you have to create
one socket for each ip address that you get from Dns.Resolve. Then bind the
sockets to these addresses (instead of binding them to IPAddress.Any).
When you call SendTo, use IPAddress.Broad cast as destination.
This way it will be sent out as broadcast on exactly the interface you have
bound the socket to. You'll also have to call ReceiveFrom on each socket.
Yes, of course. I should have figured out that myself.
It should work.. maybe there is another problem? Take a look at the Bind
description on MSDN: http://tinyurl.com/cqoce
It says "If you do not care which local port is used, you can create an
IPEndPoint using 0 for the port number. In this case, the service provider
will assign an available port number between 1024 and 5000."
My mathematical knowledge tells me that 48000 > 5000 :-). Changing to
port 4800 solved the final problem. To those that are interested, this
is the code:
Server:
//Start server
const int Port = 4800;
Socket server = new Socket(AddressF amily.InterNetw ork,
SocketType.Dgra m, ProtocolType.Ud p);
Console.Write(" Running server..." + Environment.New Line);
server.Bind( new IPEndPoint( IPAddress.Any, Port ) );
while(true)
{
IPEndPoint sender = new IPEndPoint(IPAd dress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sende r;
byte[] buffer = new byte[1000];
//Recive message from anyone.
//Server could crash here if there is another server
//on the network listening at the same port.
server.ReceiveF rom(buffer, ref tempRemoteEP);
Console.Write(" Server got '" + buffer[0] +
"' from " + tempRemoteEP.To String() + Environment.New Line);
Console.Write(" Sending '2' to " + tempRemoteEP.To String() +
Environment.New Line);
//Replay to client
server.SendTo( new byte[] {2},
tempRemoteEP );
}
Client:
const int Port = 4800;
string serverIp = "?";
//Get all addresses
string hostname = System.Net.Dns. GetHostName();
IPHostEntry allLocalNetwork Addresses = Dns.Resolve(hos tname);
//Walk thru all network interfaces.
foreach(IPAddre ss ip in allLocalNetwork Addresses.Addre ssList)
{
Socket client = new Socket(AddressF amily.InterNetw ork,
SocketType.Dgra m, ProtocolType.Ud p);
//Bind on port 0. The OS will give some port between 1025 and 5000.
client.Bind( new IPEndPoint( ip, 0 ) );
//Create endpoint, broadcast.
IPEndPoint AllEndPoint = new IPEndPoint( IPAddress.Broad cast, Port );
//Allow sending broadcast messages
client.SetSocke tOption(SocketO ptionLevel.Sock et,
SocketOptionNam e.Broadcast, 1);
//Send message to everyone on this network
client.SendTo( new byte[] {1}, AllEndPoint );
Console.Write(" Client send '1' to " + AllEndPoint.ToS tring() +
Environment.New Line);
try
{
//Create object for the server.
IPEndPoint sender = new IPEndPoint(IPAd dress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sende r;
byte[] buffer = new byte[1000];
//Recieve from server. Don't wait more than 3000 milliseconds.
client.SetSocke tOption(SocketO ptionLevel.Sock et,
SocketOptionNam e.ReceiveTimeou t, 3000);
//Recive message, save wherefrom in tempRemoteIp
client.ReceiveF rom(buffer, ref tempRemoteEP);
Console.Write(" Client got '" + buffer[0] + "' from " +
tempRemoteEP.To String()+ Environment.New Line);
//Get server IP (ugly)
serverIp = tempRemoteEP.To String().Split( ":".ToCharArray (), 2)[0];
//Don't try any more networkss
break;
}
catch
{
//Timout. No server answered. Try next network.
}
}
Console.Write(" ServerIp: " + serverIp + Environment.New Line);
Thanks for your excellent support Max!
PEK