套接字,UDP和网络访问性 [英] Sockets, UDP and Network Access tempermental

查看:79
本文介绍了套接字,UDP和网络访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我的游戏引擎即将完成.我一直在玩C#与网络计算机之间的套接字.对于测试,我只是以任何一种方式发送带有基本消息的字符串.好消息,它在给定的情况下有效.

1.如果运行可执行文件的主机在VS2010的Debug中运行,则一切正常,可以接收消息.

2.仅当从VS2010运行时才能发送到客户端.

有趣的是,当主机或客户端程序从VS2010内部运行时,将出现一个窗口,要求允许网络访问.如果我只是从Windows资源管理器中运行可执行文件,它将不会执行此操作.
我已经玩了好几天,但都没有胜利,只是希望有人能给我一些启示


读取功能

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 { }

        }

推荐答案

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.


这篇关于套接字,UDP和网络访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆