手动指定客户端/服务器中客户端的IP(手动)

时间:2014-11-11 09:27:24

标签: 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()
        {
            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];
        }
    }
}

这是客户代码:

namespace Client
{
    public partial class Form1 : Form
    {
        private Socket _client;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Int32 port = 13000;
            IPAddress my_Ip_Address = IPAddress.Parse(textBox1.Text);
            IPEndPoint endPoint = new IPEndPoint(my_Ip_Address, port);
            _client.BeginConnect(endPoint, new AsyncCallback(OnConnect), null);
        }
        private void OnConnect(IAsyncResult AR)
        {
            _client.EndConnect(AR);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte [] toSend = Encoding.ASCII.GetBytes(textBox2.Text);
            _client.BeginSend(toSend, 0, toSend.Length ,SocketFlags.None, new AsyncCallback(OnSend), _client);
        }
        private void OnSend(IAsyncResult AR)
        {
            Socket client = (Socket) AR.AsyncState;
            client.EndSend(AR);
        }
    }
}

但是当客户端上的点击按钮时,它没有连接到服务器.. 如果我在客户端更改了这些语句,它只能连接到服务器

 IPEndPoint endPoint = new IPEndPoint(my_Ip_Address,  port);

TO

IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, port);

然后它已连接,服务器中的消息框显示为白色文本&#34;已连接&#34; ,为什么? 因为我想手动指定客户端的IP地址,而不是使用IPAddress.Loopback

0 个答案:

没有答案