如何在.NET套接字程序手动设置客户端的IP地址 [英] How to set IP address of client manually in .net socket program

查看:223
本文介绍了如何在.NET套接字程序手动设置客户端的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个多线程的客户端 - 服务器聊天应用程序,并希望与多个客户端测试我的应用程序。我打算创建客户端模拟器它创建一个随机端口和IP。我的意思是我的客户端系统应具有多个端口上运行(不运行多次)。

我试图找出的code这给客户端的IP和端口号的客户端类的部分,但不能看着办吧。我只找到了一部分给服务器的IP和端口。

这是我建立的连接部分

 私人无效cmdConnect_Click(对象发件人,发送System.EventArgs)
    {
        尝试
        {
            //创建一个新的客户端套接字...
            m_socWorker =新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.IP);
            字符串szIPSelected = txtIPAddress.Text;
            字符串szPort = txtPort.Text;
            INT奥尔波特= System.Convert.ToInt16(szPort,10);


            System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
            System.Net.IPEndPoint remoteEndPoint =新System.Net.IPEndPoint(remoteIPAddress,奥尔波特);
            m_socWorker.Connect(remoteEndPoint);
        }
        赶上(System.Net.Sockets.SocketException SE)
        {
            的MessageBox.show(se.Message);
        }
    }
 

和我的数据发送部分

 私人无效cmdSendData_Click(对象发件人,发送System.EventArgs)
    {
        尝试
        {
            对象objData = txtData.Text;
            byte []的byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
            m_socWorker.Send(byData);
        }
        赶上(System.Net.Sockets.SocketException SE)
        {
            的MessageBox.show(se.Message);
        }
    }
 

解决方案

如果你真的需要做的服务器的连接之前设置客户端套接字地址,它往往是,当一个人想强制内核的有效方案绑定到多主系统中一个特定的本地地址,你可以叫 Socket.Bind() 之前您拨打 Socket.Connect ()

  ip地址localIPAddress = IPAddress.Parse(szLocalIP);
IPEndPoint localEndPoint =新IPEndPoint(localIPAddress,0);
m_socWorker.Bind(localEndPoint);

m_socWorker.Connect(remoteEndPoint);
 

您也可以显式地指定了 IPEndPoint 构造本地端口号,但​​你必须确保,该端口没有被使用。如果你离开等于0的端口号,然后系统会选择一个(几乎)任意空闲的端口号。

请注意,你不能只挑一个任意客户端IP地址 - 它必须是一个地址,对系统的功能的网络接口配置。参见 IPCONFIG / ALL 什么配置的输出。您可以分配多个IP地址的接口,如果你只是想测试一下。

I created a multithreaded client-server chat application and want to test my application with multiple clients. I'm planning to create a simulator in client side which create a random port and IP. By that I mean my client system should run with multiple ports (without running multiple times).

I tried to find out the part of the code which gives client IP and port number in the client class, but couldn't figure it out. I only found the part which gives server IP and port.

This is my connection establishing part

private void cmdConnect_Click(object sender, System.EventArgs e)
    {
        try
        {
            //create a new client socket ...
            m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            String szIPSelected  = txtIPAddress.Text;
            String szPort = txtPort.Text;
            int  alPort = System.Convert.ToInt16 (szPort,10);


            System.Net.IPAddress    remoteIPAddress  = System.Net.IPAddress.Parse(szIPSelected);
            System.Net.IPEndPoint   remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
            m_socWorker.Connect(remoteEndPoint);
        }
        catch (System.Net.Sockets.SocketException se)
        {
            MessageBox.Show ( se.Message );
        }
    }

and my data sending part

private void cmdSendData_Click(object sender, System.EventArgs e)
    {
        try
        {
            Object objData = txtData.Text;
            byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
            m_socWorker.Send (byData);
        }
        catch(System.Net.Sockets.SocketException se)
        {
            MessageBox.Show (se.Message );
        }
    }

解决方案

If you really need to set the client socket address before making a connection to the server, and it is often a valid scenario when one wants to force the kernel to bind to a specific local address on multihomed systems, you may call Socket.Bind() before you call Socket.Connect():

IPAddress  localIPAddress = IPAddress.Parse(szLocalIP);
IPEndPoint localEndPoint  = new IPEndPoint(localIPAddress, 0);
m_socWorker.Bind(localEndPoint);

m_socWorker.Connect(remoteEndPoint);

You may also explicitly specify the local port number in the IPEndPoint constructor, but you have to make sure, that the port is not in use. If you leave the port number equal to 0, then the system would pick an (almost) arbitrary free port number.

Note that you cannot just pick an arbitrary client IP address - it must be one of the addresses, configured on your system's enabled network interfaces. See the output of ipconfig /all for what is configured. You can assign more than one IP address to an interface if you simply want to test.

这篇关于如何在.NET套接字程序手动设置客户端的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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