【问题标题】:Selecting a person to chat with in C#在 C# 中选择要与之聊天的人
【发布时间】:2009-05-05 18:14:04
【问题描述】:

考虑一个有很多人在线的局域网信使的情况。 我需要选择一个特定的人来聊天。 我必须如何在 C# 中这样做? 我想要的是通过点击他的名字来选择一个特定的人。之后我必须发送我输入的任何内容,就像在 IP Lanmessenger 软件的情况下一样(希望你们已经使用它)。 谁能帮帮我。谢谢

【问题讨论】:

  • 这取决于很多参数,例如您使用什么样的控件来显示用户列表,您如何实现客户端之间的通信等等。我认为您需要提供更多详细信息才能获得一些好的答案。

标签: c# chat


【解决方案1】:

如果您想跟踪用户,我建议编写一个服务器应用程序来处理所有连接。这是一个简单的例子(注意这不是一个完整的例子):

using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

private TcpListener tcpListener;
private Thread listenerThread;
volatile bool listening;

// Create a client struct/class to handle connection information and names
private List<Client> clients;

// In constructor
clients = new List<Client>();
tcpListener = new TcpListener(IPAddress.Any, 3000);
listening = true;
listenerThread = new Thread(new ThreadStart(ListenForClients));
listenerThread.Start();

// ListenForClients function
private void ListenForClients()
{
    // Start the TCP listener
    this.tcpListener.Start();

    TcpClient tcpClient;
    while (listening)
    {
        try
        {
            // Suspends while loop till a client connects
            tcpClient = this.tcpListener.AcceptTcpClient();

            // Create a thread to handle communication with client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleMessage));
            clientThread.Start(tcpClient);
        }
        catch { // Handle errors }
    }
}

// Handling messages (Connect? Disconnect? You can customize!)
private void HandleMessage(object client)
{
    // Retrieve our client and initialize the network stream
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();

    // Create our data
    byte[] byteMessage = new byte[4096];
    int bytesRead;
    string message;
    string[] data;

    // Set our encoder
    ASCIIEncoding encoder = new ASCIIEncoding();

    while (true)
    {
        // Retrieve the clients message
        bytesRead = 0;
        try { bytesRead = clientStream.Read(byteMessage, 0, 4096); }
        catch { break; }

        // Client had disconnected
        if (bytesRead == 0)
            break;

        // Decode the clients message
        message = encoder.GetString(byteMessage, 0, bytesRead);
        // Handle the message...
    }
}

现在再次注意这不是一个完整的示例,我知道我已经全力以赴,但我希望这能给你一个想法。 HandleMessage 函数中的消息部分可以是用户的 IP 地址,如果他们正在连接到聊天服务器/断开连接,以及您要指定的其他参数。这是从我为我父亲的公司编写的应用程序中提取的代码,以便员工可以直接从我编写的自定义 CRM 中相互发送消息。如果您还有任何问题,请发表评论。

【讨论】:

    【解决方案2】:

    如果您正在构建用于聊天的 UI,并且您希望看到所有在线的人,典型的 UI 元素将是一个列表框,然后是在框中的某个项目的 On_Click 时触发的代码。该代码可以打开另一个 UI 元素来开始聊天。

    获取登录用户列表更难。您将需要实现某种观察者/订阅者模式来处理来自您正在实现的聊天协议的通知。

    GeekPedia 在creating a chat client and server in C# 上有一个很棒的系列。

    【讨论】:

    • 您能否告诉我单击列表框中的项目时触发的事件的名称。我意识到代码上的事件名称应该是 On_Click 但是属性工具栏中的事件列表中的名称是什么?谢谢
    • 我不认为你想要 On_Click,你可能想要 SelectedIndexChanged。
    猜你喜欢
    • 2023-03-05
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2021-11-15
    • 2011-11-18
    • 2023-01-23
    • 1970-01-01
    相关资源
    最近更新 更多