如何在c#中动态更改客户端的IP地址?

时间:2014-08-22 19:28:08

标签: c# ip client

在我的客户端 - 服务器winform应用程序中,每次运行时都要更改客户端的IP地址。 文本框应该使用我的客户端的IP地址,然后使用位于同一台计算机上的本地IP连接到服务器。

客户代码:

 public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void ConnectToServer()
        {
            string server_localip = GetLocalIP();
            clientSocket.Connect(server_localip, 8888);

        }

        public void SendData(string dataTosend)
        {
            if (string.IsNullOrEmpty(dataTosend))
                return;
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = new byte[33];
            outStream = System.Text.Encoding.ASCII.GetBytes(dataTosend);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();


        }

        public void CloseConnection()
        {
            clientSocket.Close();
        }
        public string ReceiveData()
        {
            StringBuilder message = new StringBuilder();
            serverStream = clientSocket.GetStream();
            serverStream.ReadTimeout = 100;
            //the loop should continue until no dataavailable to read and message string is filled.
            //if data is not available and message is empty then the loop should continue, until
            //data is available and message is filled.
            while (true)
            {
                if (serverStream.DataAvailable)
                {
                    int read = serverStream.ReadByte();
                    if (read > 0)
                        message.Append((char)read);
                    else
                        break;
                }
                else if (message.ToString().Length > 0)
                    break;
            }
            return message.ToString();
        }
        public string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            return "127.0.0.1";
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            ConnectToServer();
            btnConnect.Text = "Connected";
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            //if (!Regex.IsMatch(txtPrivateId.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
            //{
            //    lblError.Text ="Please type a valid IMPI.";
            //}
            //if (!Regex.IsMatch(txtPublicId.Text, "[^0-9]"))
            //{
            //    lblError.Text = lblError.Text + "\nPlease type a valid IMPU.";
            //}
            //else
            //{
            lblError.Text = "";


            string data = txtPrivateId.Text + ";" + txtPublicId.Text;
            for (int i = 0; i < 1; i++)
            {
                SendData(data);
            }

            string rec = null;
            rec = ReceiveData();
            txtReceive.Text = rec;
        }
    }

任何指导都将受到赞赏..

1 个答案:

答案 0 :(得分:0)

您可以使用netsh更改您的IP地址:

netsh interface ip set address [adapter name] static [ip address] [subnet mask] [gateway] [interface metric]

尝试下面的代码:

Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"Local Area Connection\" static 192.168.0.10 255.255.255.0 192.168.0.1 1");
p.StartInfo = psi;
p.Start();

并记住您的应用程序应该以提升的权限运行 “本地连接”应与您的适配器名称相匹配 有关netsh命令行开关的更多信息,请访问下面的Microsoft知识库文章:
http://support.microsoft.com/kb/242468