在 C# 中选择要聊天的人

发布于 2024-07-19 03:39:50 字数 157 浏览 3 评论 0原文

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

Consider the case of a lan messenger where a number of people are online.
I need to select a particular person to chat with.
How must I go about doing so in C#?
What I want is to select a particular person by clicking on his name.After that I whatever I type must be sent just as in the case of the IP Lanmessenger software(hoping u people have used it).
Could someone help me out.Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡绿茶香 2024-07-26 03:39:50

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

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 中相互发送消息。 如果还有什么问题请评论。

If you want to keep track of users I advice coding a server application to handle all the connections. Here is a quick example (note this is not a complete example):

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...
    }
}

Now again note this isn't a complete example and I know I sort of went all out on this but I hope this gives you an idea. The message part in the HandleMessage function can be the users IP address, if they are connecting to the Chat server/Disconnecting, and other parameters that you want to specify. This is code taken from an application I wrote for my fathers company so that the employees could message each other right from the custom CRM I wrote. If you have any more questions please comment.

女皇必胜 2024-07-26 03:39:50

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

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

GeekPedia 有一个关于 创建C# 中的聊天客户端和服务器

If you are building the UI for a chat and you want to see all the people online the typical UI element would be a list box and then code that fires on the On_Click of an item in the box. That code could open another UI element to begin the chat.

Getting the list of users logged in is harder. You will need to implement some kind of Observer/Subscriber pattern to handle notifications from the chat protocol you are implementing.

GeekPedia has a great series on creating a chat client and server in C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文