Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 手动(手动)在客户端/服务器中指定客户端的Ip_C#_Sockets_Client Server - Fatal编程技术网

C# 手动(手动)在客户端/服务器中指定客户端的Ip

C# 手动(手动)在客户端/服务器中指定客户端的Ip,c#,sockets,client-server,C#,Sockets,Client Server,我正在C中使用TCP套接字构建一个客户机/服务器# 以下是服务器的代码: namespace Server { public partial class Form1 : Form { private List<Socket> _clientSockets; private Socket server = null; private byte[] _buffer; public Form1()

我正在C中使用TCP套接字构建一个客户机/服务器#

以下是服务器的代码:

namespace Server
{
    public partial class Form1 : Form
    {
        private List<Socket> _clientSockets;
        private Socket server = null;
        private byte[] _buffer;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(new IPEndPoint(IPAddress.Any, 13000));
            server.Listen(10);
            server.BeginAccept(new AsyncCallback(OnConnect), null);
        }
        private void OnConnect(IAsyncResult AR)
        {
            Socket client = server.EndAccept(AR);
            _clientSockets.Add(client);
            MessageBox.Show("Connected");
            client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(OnRecieve), client);
        }
        private void OnRecieve(IAsyncResult AR)
        {
            Socket client = (Socket) AR.AsyncState;
            Int32 numberOfBytes = client.EndReceive(AR);
            byte[] rec = new byte[numberOfBytes];
            Array.Copy(_buffer, rec, numberOfBytes);
            MessageBox.Show(Encoding.ASCII.GetString(rec));
            client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(OnRecieve), client);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            _clientSockets = new List<Socket>();
            _buffer = new byte[4096];
        }
    }
}
但是当点击客户端上的按钮时,它并没有连接到服务器。。 只有我在客户端更改了这些语句,它才会连接到服务器

 IPEndPoint endPoint = new IPEndPoint(my_Ip_Address,  port);

然后它被连接,服务器中的消息框显示为“已连接”,为什么??
因为我想手动指定客户端的IP地址,而不是使用
IPAddress.Loopback

您给出的
textBox1
值是多少?@RoryMcCrossan值,如192.168.2.33是否与该地址类似?还是真的是那个地址?您确定作为
my\u Ip\u address
传递的地址实际上是计算机的Ip地址吗?
 IPEndPoint endPoint = new IPEndPoint(my_Ip_Address,  port);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, port);