Click here to Skip to main content
15,070,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
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 { }

        }
Posted
Comments
Ciumac Sergiu 13-Oct-11 17:40pm
   
Have you tried running the application under Administrator privileges?
Thomas.D Williams 16-Oct-11 10:02am
   
I just tried it. It seems to be less temperamental when run as admin on both computers. It only seems to be the host that need to run under admin for it to all work as expected
tokenian 13-Oct-11 22:54pm
   
I encountered this before,it seems not the code problem,just the OS.
Thomas.D Williams 16-Oct-11 10:03am
   
Both computers are running x64 windows 7. Running in admin seems to help

1 solution

CSS
It seems that you have some sort of memory error/corruption that just happens to work ok in the debugger.

You can try using cout to isolate how far/where it dies, or try a tool like Purify, etc.

Root cause is not in code; but in environment. The difference between direct and VS application launch, will be in the initialization of memory. If your program is using an uninitialized variable, the default memory contents can cause different behavior.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900