Choosing a person to communicate in C #
Consider a case where one of the Internet users is online. I need to choose a specific person for communication. How am I supposed to do this in C #? I want to select a specific person by clicking on his name. After that, everything I type should be sent in the same way as with the IP Lanmessenger software (hoping people used it). Can someone help me. thanks
source to share
If you want to track users, I advise you to code your server application to handle all connections. Here's a quick example (note that 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, please note that this is not a complete example and I know I kind of tried my best, but I hope this gives you an idea. The part of the message in the HandleMessage function can be the IP address of the users if they connect to the chat server / disconnect and other parameters you want to specify. This is code taken from an app I wrote for my dad's company so employees can communicate with each other right from the custom CRM I wrote. If you have any more questions, please comment.
source to share
If you are building a chat user interface and want to see all the people on the web, a typical UI element would be a list box and then the code that runs on the On_Click of the element in the box. This code can open another UI element to start a chat.
Getting a list of registered users is more difficult. 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 building a chat client and server in C # .
source to share