将IP+Port转换成标准格式***.***.***.***:*****

来源:互联网 发布:d3.js 社交图谱 编辑:程序博客网 时间:2024/05/16 21:27

        private string IPandPort(string temp)
        {
             string ipandport = temp;
            string[] strDetail = ipandport.Split(":".ToCharArray());
            string iptemp = strDetail[0];
            string porttemp = strDetail[1];
            string[] ipDetail = iptemp.Split(".".ToCharArray());
            string ip = "";
            string port="";
            for (int i = 0; i < ipDetail.Length; i++)
            {
                if (ipDetail[i].Length < 3)
                {
                    if (ipDetail[i].Length == 0)
                    {
                        ipDetail[i] = "000";
                    }
                    else if (ipDetail[i].Length == 1)
                    {
                        ipDetail[i] = "00" + ipDetail[i];
                    }
                    else if (ipDetail[i].Length == 2)
                    {
                        ipDetail[i] = "0" + ipDetail[i];
                    }
                }
               
            }
            ip = ipDetail[0] + "." + ipDetail[1] + "." + ipDetail[2] + "." + ipDetail[3];
            if (porttemp.Length < 5)
            {
                if (porttemp.Length == 1)
                {
                    port = "0000" + porttemp;
                }
                else if (porttemp.Length == 2)
                {
                    port = "000" + porttemp;
                }
                else if (porttemp.Length == 3)
                {
                    port = "00" + porttemp;
                }
                else if (porttemp.Length == 4)
                {
                    port = "0" + porttemp;
                }
            }
             else
            {
                port = porttemp;
            }
            string ipport=ip+port ;
            return ipport ;
        }