0

![Server][1]

Code Server : public partial class Form1 : Form { private TcpClient tcpclient;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IPAddress[] localip = Dns.GetHostAddresses(Dns.GetHostName());
        foreach (IPAddress address in localip)
        {
            if (address.AddressFamily == AddressFamily.InterNetwork)
            {
                textBox1.Text = address.ToString();
                textBox2.Text = "8888";
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TcpListener listenner = new TcpListener(IPAddress.Any, int.Parse(textBox2.Text));
        listenner.Start();
        tcpclient = listenner.AcceptTcpClient();

    }

![client][2] Code Client namespace CLIENT { public partial class Form1 : Form { private TcpClient tcpclient; public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        labelmessage.Visible = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        tcpclient = new TcpClient();
        IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
        try
        {
            tcpclient.Connect(ipe);
            if (tcpclient.Connected)
            {
                labelmessage.Visible = true;
                labelmessage.Text = "Conected...";
            }
        }
        catch
        {

        }
    }
}

}

4

1 回答 1

0

使用 AcceptTCPClient() 方法返回的 TCPClient 实例。

     TCPClient _client = _listener.AcceptTCPClient();
    // Add to listview here for connection
    Console.WriteLine(String.Format("{0} connected", _client.Client.RemoteEndPoint.ToString()));
    // Same for others

更新

TCPClient _client = _listener.AcceptTCPClient();
ListViewItem _item = new ListViewItem(_client.Client.RemoteEndPoint.ToString());
listview1.Items.Add(_item);

有关使用 ListView 控件的更多信息

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx

于 2014-04-06T08:56:40.170 回答