Avik
Avik

Reputation: 2137

Selecting a person to chat with in C#

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

Upvotes: 0

Views: 2638

Answers (2)

Cris McLaughlin
Cris McLaughlin

Reputation: 1211

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.

Upvotes: 3

Gary.Ray
Gary.Ray

Reputation: 6501

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

Upvotes: 0

Related Questions