0

So there this problem that has been giving me headaches for days now.I am making a multi-user chat application.My design is as follows: 1.There is a login window. 2.As soon as the details are entered, the client-side chat window opens. 3.Now the user starts typing. 4.As soon as he hits enter or clicks on the send button,the message is sent to the server. 5.The server sends it to all clients, including the one that send it the original message.

The problem:I am unable to receive any messages from the server to the client.

Here is my server class:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server implements Runnable {
static InetAddress address;
static ArrayList<Integer> clients=new ArrayList<Integer>();
static ArrayList<Socket> socs=new ArrayList<>();
static String message="";
static DataOutputStream toClient;
static  ServerSocket socket;
static Socket socketNew;
static boolean running=false;
public static void main(String[] args) throws IOException
{
    socket=new ServerSocket(8000);
    System.out.println("Server started on port 8000");
    running=true;

    while(true)
    {   
        socketNew=socket.accept();
        socs.add(socketNew);
        address=socketNew.getInetAddress();

        System.out.println("connected to client at address: "+address);

        Server server=new Server();
        new Thread(server).start();


    }
}


public void run() {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(socketNew.getInputStream()));
        String message;
        PrintWriter out;

        while ((message = br.readLine()) != null) {
            System.out.println(message);
            for (Socket s : socs) // sending the above msg. to all clients
            {
                out = new PrintWriter(s.getOutputStream(), true);
                out.write(message);
                out.flush();

            }


        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

And here is the receive_message function in the client class.Note that this method,I've run on a separate thread that starts as soon as the user logs-in.

public void receive_data()
{while(true)
{
try {
    BufferedReader in;
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    while(in.readLine()!=null)
    {
        System.out.println(in.readLine());
       console(in.readLine());
       }
   }
 catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }

  }
 }



}

Any suggestions?Thanks for your time. :-)

1 Answer 1

1

You are writing messages without a line ending, while your client is waiting for a line ending character in the readLine loop. By placing out.write('\n') in your server send loop, it will also send a newline character.

Example:

for (Socket s : socs)  {
     out = new PrintWriter(s.getOutputStream(), true);
     out.write(message);
     out.write('\n'); // added this line
     out.flush();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.