Hello all,
I am nearing completion on the engine for a game. I have been playing with sockets in C# between to networked computers. for testing I was merely sending a string with a basic message either way. Good news it works under given circumstances.
1. If the host machine running the executable is running in Debug from VS2010 then everything works fine and the message can be received.
2. Can only send to the client if it is running from VS2010.
It is interesting to note that when either the host or client program run from within VS2010 a windows will appear asking to allow network access. It will not do this if I just run the executable from windows explorer.
I've played around days now with no prevail, just hoping someone can shed some light
Read Function
public static string Read()
{
bool done = false;
UdpClient NetworkListener = new UdpClient(PortNumber);
IPEndPoint EndMachine = new IPEndPoint(IPAddress.Any, PortNumber);
string Result = "";
byte[] Received;
int Tries = 0;
try
{
while (!done && Tries < 60)
{
Received = NetworkListener.Receive(ref EndMachine);
Result = Encoding.ASCII.GetString(Received, 0, Received.Length);
if (Result.Length > 0) done = true;
Thread.Sleep(1000);
Tries++;
}
}
catch { }
NetworkListener.Close();
return Result;
}
Send Function
static int PortNumber = 11000;
public static void Send(string Message, string IP)
{
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress send_to_address = IPAddress.Parse(IP);
IPEndPoint sending_end_point = new IPEndPoint(send_to_address, PortNumber);
byte[] send_buffer = Encoding.ASCII.GetBytes(Message);
try
{
sending_socket.SendTo(send_buffer, sending_end_point);
}
catch { }
}