I'm trying to code an instant messaging system... Initially, I'm doing it this way, and once I get it to work I'll add the GUI.
Once a client sends a message to the server, the server is supposed to display it to all the other clients. How can I do that? I've been trying a few things but it keeps displaying only to the client that sent the message...
Thanks in advance!
SERVER
import java.io.*;
import java.net.*;
class Server {
//one per server
static int port = 3000;
private int backlog = 100;
ServerSocket main;
static DataOutputStream dataOut;
static DataInputStream dataIn;
static String scannerMessage;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static class MailServer extends Thread {
//one per client
static int index;
String name = Client.name;
public MailServer(int index, DataInputStream in, DataOutputStream out) {
Server.dataIn = in;
Server.dataOut = out;
this.index = index; // thread index, one per client
}
public void run() {
while (true) {
try {
String receivedMessage = dataIn.readUTF();
//print receivedMessage to all clients
} catch (Exception e) {
break;
}
}
}
}
public Server(int port) throws Exception {
this.main = new ServerSocket(port);
}
// start a serve
public void serve() throws Exception {
int index = 1;
while (true) {
Socket socket = this.main.accept();
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
DataInputStream dataIn = new DataInputStream(in);
DataOutputStream dataOut = new DataOutputStream(out);
// handle the connection
// keep reading using an infintite loop
System.out.println("Handling connection to Client " + index + "...");
(new MailServer(index, dataIn, dataOut)).start();
index += 1; // add one every time a new client is added
}
}
public static void main(String[] args) throws Exception {
Server s = new Server(port);
System.out.println("Serving....");
s.serve();
}
}
CLIENT
import java.io.*;
import java.net.*;
class Client {
static String hostname = "127.0.0.1";
static int port = Server.port;
static Socket socket;
static String name;
static class Sender extends Thread {
DataOutputStream dataOut;
public Sender(DataOutputStream dataOut) {
this.dataOut = dataOut;
}
public void run() {
while(true) {
//get a message from the user
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String message = br.readLine();
dataOut.writeUTF(message);
dataOut.flush();
} catch(Exception e) {
break;
}
}
}
}
static class Receiver extends Thread {
DataInputStream dataIn;
public Receiver(DataInputStream dataIn) {
this.dataIn = dataIn;
}
public void run() {
while(true) {
try {
//RECEIVE A MESAGE FROM THE SERVER (ending in \n)
String msg = dataIn.readUTF();
while (msg != null) {
System.out.println(msg);
msg = dataIn.readUTF();
}
} catch(Exception e) {
break;
}
}
}
}
//client will require host name and the port
public Client(String hostname, int port) throws Exception {
socket = new Socket(hostname, port);
}
public void connect() throws Exception {
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
DataInputStream dataIn = new DataInputStream(in);
DataOutputStream dataOut = new DataOutputStream(out);
//handle the connection
System.out.println("Handling connection to server...");
Thread sender = new Sender(dataOut);
Thread receiver = new Receiver(dataIn);
sender.start();
receiver.start();
sender.join();
receiver.join();
System.out.println("Client " + Server.MailServer.index);
System.out.println("----------------------");
}
public static void main(String[] args) throws Exception {
Client c = new Client(hostname, port);
c.connect();
}
}
Update: I created a list of all the MailServer objects and then iterated through them to send the message to all the clients, as JP Moresmau suggested... but now the first Client to send something receives all the outputs. Why is this? How can I fix it... ? Thank you, and sorry if my questions seem too obvious or dumb, I'm still a Java noob:(
SERVER - UPDATED
package csci2020_assignment51;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Server {
//one per server
static int port = 3000;
private int backlog = 100;
ServerSocket main;
static DataOutputStream dataOut;
static DataInputStream dataIn;
static String scannerMessage;
static List<MailServer> mailServers = Collections.<MailServer>synchronizedList(new ArrayList<MailServer>());
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static class MailServer extends Thread {
//one per client
static int index;
String name = Client.name;
public MailServer(int index, DataInputStream in, DataOutputStream out) {
Server.dataIn = in;
Server.dataOut = out;
this.index = index; // thread index, one per client
}
public void run() {
while (true) {
try {
String receivedMessage = dataIn.readUTF();
String outputMessage = "Client " + index + " said: " + receivedMessage;
//print receivedMessage to all clients
for (MailServer mailserver : mailServers) {
dataOut.writeUTF(outputMessage);
}
} catch (Exception e) {
break;
}
}
}
}
public Server(int port) throws Exception {
this.main = new ServerSocket(port);
}
// start a serve
public void serve() throws Exception {
int index = 1;
while (true) {
Socket socket = this.main.accept();
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
DataInputStream dataIn = new DataInputStream(in);
DataOutputStream dataOut = new DataOutputStream(out);
// handle the connection
// keep reading using an infintite loop
System.out.println("Handling connection to Client " + index + "...");
MailServer mailServer = new MailServer(index, dataIn, dataOut);
mailServer.start();
mailServers.add(mailServer);
dataOut.writeUTF("Client " + index);
index += 1; // add one every time a new client is added
}
}
public static void main(String[] args) throws Exception {
Server s = new Server(port);
System.out.println("Serving....");
s.serve();
}
}
Have a static list of all the MailServer objects you create
static List<MailServer> servers=Collections.<MailServer>synchronizedList(new LinkedList<MailServer>);
...
MailServer s=new MailServer(index, dataIn, dataOut);
servers.add(s);
s.start();
Then loop through them all when one of them receives a message, and for all expect the receiver, write the message to their output.
The big problem in your code is that dataOut and dataIn are unique! You need to move them to the MailServer class. Each MailServer talks to one specific client and hence needs to have their own streams.
static class MailServer extends Thread {
DataOutputStream dataOut;
DataInputStream dataIn;
And your notification loop becomes:
for(MailServer mailServer:mailServers){
if (mailServer!=this){
mailServer.dataOut.writeUtf(...);
}
}
I also don't understand how you expect to get the Client.name in the server, since Client runs on another machine... Get rid of that for now.
Related
image of a error:image
i am creating a multi threaded client server chat with 2 clients. The program gives no errors, but the message I send does not reach the client, how can I do it? writing
hello # client 1
the message should get to the second client but it doesn't.
how do i change the client name?
Below I leave the files respectively of the server and the client that I wrote:
import java.io.*;
import java.util.*;
import java.net.*;
public class Server{
static Vector<ClientHandler> lc = new Vector<>();
static int i = 0;
public static void main(String[] args) throws UnknownHostException, IOException{
ServerSocket ss = new ServerSocket(2104);
while (true){
Socket s = ss.accept();
System.out.println("Ricevuta nuova richiesta del client " + s);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
ClientHandler ch = new ClientHandler(s,"Client numero" + i, dis, dos);
Thread t = new Thread(ch);
System.out.println("Il client si sta aggiungendo alla lista");
lc.add(ch);
t.start();
i++;
}
}
}
class ClientHandler implements Runnable{
Scanner scn = new Scanner(System.in);
private String name;
final DataInputStream dis;
final DataOutputStream dos;
Socket s;
boolean login;
public ClientHandler(Socket s, String name, DataInputStream dis, DataOutputStream dos){
this.dis = dis;
this.dos = dos;
this.name = name;
this.s = s;
this.login=true;
}
#Override
public void run(){
String ricevuta;
while (true){
try{
ricevuta = dis.readUTF();
System.out.println(ricevuta);
if(ricevuta.equals("logout")){
this.login=false;
this.s.close();
break;
}
StringTokenizer st = new StringTokenizer(ricevuta, "#");
String MsgToSend = st.nextToken();
String recipient = st.nextToken();
for (ClientHandler mc : Server.lc){
if (mc.name.equals(recipient) && mc.login==true){
mc.dos.writeUTF(this.name+ " : " +MsgToSend);
break;
}
}
} catch (IOException e){
e.printStackTrace();
}
}
try{
this.dis.close();
this.dos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
final static int ServerPort = 2104;
public static void main(String args[]) throws UnknownHostException, IOException{
Scanner scn = new Scanner(System.in);
InetAddress ip = InetAddress.getByName("localhost");
Socket s = new Socket(ip, ServerPort);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
Thread sendMessage = new Thread(new Runnable(){
#Override
public void run(){
while (true){
String messaggio = scn.nextLine();
try {
dos.writeUTF(messaggio);
} catch (IOException e){
e.printStackTrace();
}
}
}
});
Thread readMessage = new Thread(new Runnable(){
#Override
public void run(){
while (true){
try {
String messaggio = dis.readUTF();
System.out.println(messaggio);
} catch (IOException e){
e.printStackTrace();
}
}
}
});
sendMessage.start();
readMessage.start();
}
}
my server class
import java.net.*;
import java.io.*;
public class server
{
private Socket socket;
private ServerSocket server;
// constructor with port
public void start(int port){
try {
server = new ServerSocket(port);
while(true){
socket = server.accept();
new ConnectionHandler(socket).run();
}
}catch(IOException i){
}
}
}
class ConnectionHandler implements Runnable{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
public ConnectionHandler(Socket socket){
this.socket=socket;
}
#Override
public void run() {
InputStream inp = null;
BufferedReader brinp = null;
DataOutputStream out = null;
try
{
System.out.println("Waiting for a client ...");
System.out.println(server);
System.out.println("Client accepted");
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String line = "";
// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String[] args) {
server serverr = new server();
serverr.start(4000);
}
}
Here's my client class.
import java.net.*;
import java.io.*;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
String line = "";
// keep reading until "Over" is input
while (!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 4000);
}
}
Trying to develop pretty simple chat application works via terminal, but I think there are plenty of bugs I have in my code.
The server can handle one client, but when another client comes up it doesn't connect to other clients.
What am I have to do now?
I couldn't find out where my problem is, waiting your helps.
Note: I am completely new to socket programming concept.
The ConnectionHandler class is a thread class, and you must wrap its object to a Thread instance and then call start() instead of run().
So in the Server class change
new ConnectionHandler(socket).run();
with
new Thread(ConnectionHandler(socket)).start();
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I wrote a java application for both Server and Client. What I want to do is stop the Client's application(and all of it's threads) when the user enters the word: "logout". I've tried everything I could find so kinda desperate here. Please send help!
Here is my code for Client.java
package client;
//Java implementation for multithreaded chat client
//Save file as Client.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client extends Thread
{
final static int ServerPort = 1234;
private volatile static boolean running = true;
public static void main(String args[]) throws UnknownHostException, IOException
{
Scanner scn = new Scanner(System.in);
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection
Socket s = new Socket(ip, ServerPort);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// sendMessage thread
Thread sendMessage = new Thread(new Runnable()
{
#Override
public void run() {
while (running) {
// read the message to deliver.
try {
String msg = scn.nextLine();
if(msg == "logout") {
running = false;
dis.close();
dos.close();
scn.close();
s.close();
Thread.currentThread().interrupt();
break;
}
dos.writeUTF(msg);
}
catch (IOException e) {
if(!running) {
System.out.println("Closing...");
System.exit(0);
}
}
} }
});
// readMessage thread
Thread readMessage = new Thread(new Runnable()
{
#Override
public void run() {
while (running) {
// read the message sent to this client
try {
String msg = dis.readUTF();
if(sendMessage.isInterrupted()) {
running = false;
dis.close();
dos.close();
scn.close();
s.close();
Thread.currentThread().interrupt();
break;
}
System.out.println(msg);
} catch (IOException e) {
if(!running) {
System.out.println("Closing...");
System.exit(0);
}
}
}
}
});
sendMessage.start();
readMessage.start();
}
}
And this is my Server.java
package server;
//Java implementation of Server side
//It contains two classes : Server and ClientHandler
//Save file as Server.java
import java.io.*;
import java.util.*;
import java.net.*;
//Server class
public class Server
{
// Vector to store active clients
static Vector<ClientHandler> ar = new Vector<>();
// counter for clients
static int i = 0;
public static void main(String[] args) throws IOException
{
// server is listening on port 1234
ServerSocket ss = new ServerSocket(1234);
Socket s;
// running infinite loop for getting
// client request
while (true)
{
// Accept the incoming request
s = ss.accept();
System.out.println("New client request received : " + s);
// obtain input and output streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Creating a new handler for this client...");
// Create a new handler object for handling this request.
ClientHandler mtch = new ClientHandler(s,"client " + i, dis, dos);
// Create a new Thread with this object.
Thread t = new Thread(mtch);
System.out.println("Adding this client to active client list");
// add this client to active clients list
ar.add(mtch);
// start the thread.
t.start();
// increment i for new client.
// i is used for naming only, and can be replaced
// by any naming scheme
i++;
}
}
}
//ClientHandler class
class ClientHandler implements Runnable
{
private String name;
final DataInputStream dis;
final DataOutputStream dos;
Socket s;
boolean isloggedin;
// constructor
public ClientHandler(Socket s, String name,
DataInputStream dis, DataOutputStream dos) {
this.dis = dis;
this.dos = dos;
this.name = name;
this.s = s;
this.isloggedin=true;
}
#Override
public void run() {
String received;
while (true)
{
try
{
// receive the string
received = dis.readUTF();
if(received.equals("logout")){
break;
}
// break the string into message and recipient part
StringTokenizer st = new StringTokenizer(received, "#");
String MsgToSend = st.nextToken();
String recipient = st.nextToken();
// search for the recipient in the connected devices list.
// ar is the vector storing client of active users
for (ClientHandler mc : Server.ar)
{
// if the recipient is found, write on its
// output stream
if (mc.name.equals(recipient) && mc.isloggedin==true)
{
mc.dos.writeUTF(this.name+" : "+MsgToSend);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
try
{
// closing resources
this.dis.close();
this.dos.close();
this.s.close();
this.isloggedin=false;
}catch(IOException e){
e.printStackTrace();
}
}
}
Code reference: Multithread GroupChat 1
Multithread GroupChat 2
Don't compare Strings with == but with equals(). msg == "logout" Should be msg.equals("logout").
Multiple Clients say(A, B, C, D etc) make connection to one server through same socket. They all send messages to server as and when required. Client messages are sent only to server(and not to other clients). But whenever server sends a message it should be delivered to all the clients. Please help me out jam only able to get server message on only 1 client
//MultithreadedServer.java
package server;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Vector;
public class MultithreadedServer
{
Vector<ClientHandler> clients = new Vector<ClientHandler>();
Vector<String> users = new Vector<String>();
private static ServerSocket servSocket;
private static final int PORT = 1247;
public MultithreadedServer() throws IOException{
servSocket = new ServerSocket(PORT);
while(true) {
Socket client = servSocket.accept();
System.out.println("\nNew client accepted.\n");
ClientHandler handler;
handler = new ClientHandler(client);
clients.add(handler);
}
}
public static void main(String[] args) throws IOException
{
MultithreadedServer ms = new MultithreadedServer();
}
class ClientHandler extends Thread
{
private Socket client;
private BufferedReader in;
private PrintWriter out;
String name,message,response;
public ClientHandler(Socket socket)
{
//Set up reference to associated socket...
client = socket;
try
{
in = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
out = new PrintWriter(
client.getOutputStream(),true);
}
catch(IOException e)
{
e.printStackTrace();
}
start();
}
public void sendMessage(String msg) {
System.out.println("is it even coming here?");
out.println("Server:" + msg);
}
public void boradcast(String message) {
// send message to all connected users
for (ClientHandler c : clients) {
c.out.println("Server: hello");
}
}
public String getUserName() {
return name;
}
public void run()
{
try
{
String received;
do
{ System.out.println("Enter Your Message: ");
String msg = in.readLine();
out.println(msg);
boradcast("testing");
received = in.readLine();
out.println("ECHO: " + received);
//Repeat above until 'QUIT' sent by client...
}while (!received.equals("QUIT"));
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (client!=null)
{
System.out.println(
"Closing down connection...");
client.close();
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}}
//ClientProgram
import java.io.*;
import java.net.*;
public class Client
{
private static InetAddress host;
private static final int PORT = 1247;
private static Socket link;
private static BufferedReader in;
private static PrintWriter out;
private static BufferedReader keyboard;
public static void main(String[] args) throws IOException
{
try
{
host = InetAddress.getLocalHost();
link = new Socket(host, PORT);
in = new BufferedReader(
new InputStreamReader(
link.getInputStream()));
out = new PrintWriter(
link.getOutputStream(),true);
keyboard = new BufferedReader(
new InputStreamReader(System.in));
String message, response;
do
{
System.out.print(
"Enter message ('QUIT' to exit): ");
message = keyboard.readLine();
//Send message to server on
//the socket's output stream...
out.println(message);
//Accept response frm server on
//the socket's input stream...
response = in.readLine();
//Display server's response to user...
System.out.println(response);
}while (!message.equals("QUIT"));
}
catch(UnknownHostException uhEx)
{
System.out.println(
"\nHost ID not found!\n");
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
if (link!=null)
{
System.out.println(
"Closing down connection...");
link.close();
}
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
}
}
}
An obvious way to do this would be to cycle through all ClientHandlers in clients and send the message to each of them:
for (ClientHandler ch : clients){
ch.sendMessage(message); //Or something
}
Why does this program not work properly? Client reads SOME_MESSAGE and after that nothing happens. It seems that println method from server in some way have influence on transferring long type numbers.
SERVER
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket socket = new ServerSocket(9999);
while (true) {
Socket sock = socket.accept();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(sock.getOutputStream())), true);
DataOutputStream outByte = new DataOutputStream(sock.getOutputStream());
out.println("SOME_MESSAGE");
outByte.writeLong(948L);
}
}
}
CLIENT
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws Exception {
Socket sock = new Socket("127.0.0.1", 9999);
DataInputStream inByte = new DataInputStream(sock.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(
sock.getInputStream()));
System.out.println(in.readLine());
long number = inByte.readLong();
System.out.println(number);
}
}
Your problem is that the BufferedReader is buffering bytes from the socket's input stream, so the long 948 value isn't in the DataInputStream because the BufferedReader has it read and is buffering it. In general you don't want to be using 2 different wrappers around the same underlying stream, especially if one is buffered. Same with your Server class, but that seems to at least be working.
Your Client needs to use only one wrapper for the socket's input stream. You should just stick with the DataInputStream and along with the Server code, use DataInputStream.readUTF() on the Client while using DataOutputStream.writeUTF() on the Server, getting rid of both the BufferedReader and the PrintWriter.
So on the Server:
while(true) {
Socket sock = socket.accept();
DataOutputStream outByte = new DataOutputStream(sock.getOutputStream());
outByte.writeUTF("SOME_MESSAGE");
outByte.writeLong(948L);
outByte.flush();
}
and on the Client:
public static void main(String[]args)throws Exception
{
Socket sock = new Socket("127.0.0.1",9999);
DataInputStream inByte = new DataInputStream(sock.getInputStream());
System.out.println(inByte.readUTF());
long number = inByte.readLong();
System.out.println(number);
}
It is interesting that java does not allow to do that having said that there is always a better solution. You can use serialization to do your job.
create a interface PayLoad like below
import java.io.Serializable;
public interface PayLoad extends Serializable
{
String getMessage();
//Java does not allow to define byte array of long
int getLength();
byte[] getbytes();
}
Then create an Implementation class like below
public class FilePayLoad implements PayLoad
{
private final String message;
private final int length;
private final byte[] bytes;
public FilePayLoad(String message, int length, byte[] bytes)
{
this.message = message;
this.length = length;
this.bytes = bytes;
}
#Override
public String getMessage()
{
return this.message;
}
#Override
public int getLength()
{
return this.length;
}
#Override
public byte[] getbytes()
{
return this.bytes;
}
}
Now change your server and client like below
Server
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket socket = new ServerSocket(9999);
while (true)
{
Socket sock = socket.accept();
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
byte[] bytes = "SOME_MESSAGE".getBytes();
out.writeObject(new FilePayLoad("SOME_MESSAGE", bytes.length, bytes));
out.flush();
}
}
}
Client
public class Client
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 9999);
ObjectInputStream inByte = new ObjectInputStream(sock.getInputStream());
PayLoad payLoad = (PayLoad) inByte.readObject();
System.out.println(payLoad.getMessage());
System.out.println(payLoad.getLength());
System.out.println(new String(payLoad.getbytes()));
}
}