how can i make a global chat system where client can message each other client.
ID: 3853898 • Letter: H
Question
how can i make a global chat system where client can message each other client.
i already have client and server coding but it is incomplete
serve.java
import java.net.*;
import java.io.*;
import java.util.*;
public class EasyServer
{
public static void main(String args[])
{
try
{
//Make it a server application
ServerSocket server = new ServerSocket(8888);
//Establish a connection
while(true)
{
Socket soc = server.accept();
ParallelConnection connection =new ParallelConnection(soc);
//listen
connection.start();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
class ParallelConnection extends Thread
{
public static ArrayList<ParallelConnection> listOfConnection = new ArrayList<ParallelConnection>();
BufferedReader receiver;
public PrintStream sender;
String client;
public ParallelConnection(Socket conn)
{
try
{
client = conn.getInetAddress().getHostName();
//Incoming message channel
receiver = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//Outgoing message channel
sender = new PrintStream(conn.getOutputStream());
sender.println("You Are Connected..");
System.out.println(client + " Are Connected..");
//sender.println("Please Enter the Password to enter chat");
listOfConnection.add(this);
} catch (Exception ex)
{
ex.printStackTrace();
}
}
public void run()
{
try
{
String message;
while ((message = receiver.readLine())!=null)
{
if(message.compareTo("Exit") == 0){
sender.close();
receiver.close();
sender.println("Disconnected..");
break;
}
for(int i = 0; i < listOfConnection.size(); i++){
listOfConnection.get(i).sender.println(client + ":" + message);
}
System.out.println(client + ":" + message);
//System.out.println(message);
//sender.println("I Received " + message);
}
} catch(Exception a) {
a.printStackTrace();
}
}
}
and a client.java code
import java.net.*;
import java.io.*;
import java.util.*;
public class Client {
public static void main(String args[]){
try {
//Request for a connection
Socket soc = new Socket("172.18.55.108", 8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//Open a channel for outgoing messages
PrintStream sender = new PrintStream(soc.getOutputStream());
//Receive the first message from server
String line = reader.readLine();
System.out.println(line);
//Reader line from the keyboard
Scanner scan = new Scanner(System.in);
while(true){
line = scan.nextLine();
//Send the line to the server
sender.println(line);
//Receive the message from the server
line = reader.readLine();
System.out.println(line);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Explanation / Answer
Hi The following chat code works fine for me
import java.net.*;
import java.io.*;
import java.util.*;
import java.net.Socket;
public class EasyServer {
static Socket client = null;
static ServerSocket server = null;
public static void main(String[] args) {
System.out.println("Server is starting...");
System.out.println("Server is listening...");
try {
ServerSocket server = new ServerSocket(8888);
} catch (IOException ex) {
System.out.println("Could not listen on port 8888");
System.exit(-1);
}
try {
boolean done = false;
while (!done) {
client = server.accept();
System.out.println("Client Connected...");
BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter streamOut = new PrintWriter(client.getOutputStream(),true);
String line = streamIn.readLine();
if (line.equalsIgnoreCase("bye")) {
streamIn.close();
client.close();
server.close();
done = true;
} else {
System.out.println(line);
streamOut.println(line);
}
}
} catch (IOException e) {
System.out.println("IO Error in streams " + e);
}
}}
1.On the server, you can keep a Set of all the clients that are currently connected to the server.
2.The server should listen for messages
3.Each time the server receives a message, it sends this message back to all clients in the Set, and the clients display the message.
4.if you want to do direct client-to-client, one of them will just have to act as the server.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.