예제 #1
0
파일: NET.cs 프로젝트: optimus-code/Q2Sharp
        public static DatagramSocket Socket(int sock, string ip, int port)
        {
            DatagramSocket newsocket = null;

            try
            {
                if (ip_channels[sock] == null || !ip_channels[sock].IsOpen())
                {
                    ip_channels[sock] = new DatagramSocket(); // DatagramChannel.Open();
                }
                if (ip == null || ip.Length == 0 || ip.Equals("localhost"))
                {
                    if (port == Defines.PORT_ANY)
                    {
                        newsocket = ip_channels[sock].Socket();
                        newsocket.Bind(0);
                    }
                    else
                    {
                        newsocket = ip_channels[sock].Socket();
                        newsocket.Bind(port);
                    }
                }
                else
                {
                    newsocket = ip_channels[sock].Socket();
                    newsocket.Bind(ip, port);
                }

                ip_channels[sock].ConfigureBlocking(false);
                newsocket.EnableBroadcast = true;
            }
            catch (Exception e)
            {
                Com.Println("Error: " + e.ToString());
                newsocket = null;
            }

            return(newsocket);
        }