1,934 questions
There are plenty of samples from Google, like :
How to C# Chat server programming
Chat Server with Client Implemented with C#
etc...
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I want to create a chat similar to Messenger with c #, I tried some tutorials but did not understand them, anyone have a tutorial idea how to do this?
There are plenty of samples from Google, like :
How to C# Chat server programming
Chat Server with Client Implemented with C#
etc...
Here is some code that will show you how we will be using threads to allow multiple connections:
?
1 Socket socket = sSocket.accept();
2 Thread socketThread = new ThreadClass(socket);
3 socketThread.start();
We will run this code every time someone connects and this will essentially place their connection in it’s own thread. This thread will handle everything for that one client. Unfortunately we can’t just say “RUN THE SOCKET” and it will suddenly magically receive connections from the client, print them out, and send them to all the other clients. So, we essentially create our own custom thread. Here is some code that will outline how this will work:
//We need to use "implements Runnable to tell Java that this is a thread
class ClientThread implements Runnable {
//This run method is what is executed when the thread starts
public void run()
{
//Set up the PrintWriter and BufferedReader here
while(true) {
//Get info sent from client
String clientInput = input.nextLine();
//Here would would have a for loop that would send the
//client's message to every other client connected.
}
}
}
Okay, now that you know a little about threads and what we’re going to do with them lets redo the server code.
The New Server Code
import java.net.*;
import java.util.*;
import java.io.*;
public class Server
{
public static void main(String[] args)
{
new Server();
}
public Server()
{
//We need a try-catch because lots of errors can be thrown
try {
ServerSocket sSocket = new ServerSocket(5000);
System.out.println("Server started at: " + new Date());
//Loop that runs server functions
while(true) {
//Wait for a client to connect
Socket socket = sSocket.accept();
//Create a new custom thread to handle the connection
ClientThread cT = new ClientThread(socket);
//Start the thread!
new Thread(cT).start();
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
//Here we create the ClientThread inner class and have it implement Runnable
//This means that it can be used as a thread
class ClientThread implements Runnable
{
Socket threadSocket;
//This constructor will be passed the socket
public ClientThread(Socket socket)
{
//Here we set the socket to a local variable so we can use it later
threadSocket = socket;
}
public void run()
{
//All this should look familiar
try {
//Create the streams
PrintWriter output = new PrintWriter(threadSocket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(threadSocket.getInputStream()));
//Tell the client that he/she has connected
output.println("You have connected at: " + new Date());
while (true) {
//This will wait until a line of text has been sent
String chatInput = input.readLine();
System.out.println(chatInput);
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
}
}
Regards
Ram Dixit
(Full Stack Developers)
Email: ******@digiprima.com
Visit us at -