0

When debugging my code, the code stops here:

while (( msg = reader.readLine())  != null) {
        writer.write("From server: " + msg);
}

I am sending input from a client class, but I can't figure out where I am wrong, since the server never receives the message.

Here is my entire sever class:

    import java.io.*;
import java.net.*;


public class Server {

    private BufferedReader reader;
    private PrintWriter writer;
    private int port;

    public Server(int port)
    {
        this.port = port;
    }

    private String getSeverAddress() {      
        String host = null;
        try {
            InetAddress adr = InetAddress.getLocalHost();
            host = adr.getHostAddress();

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

        return host;
    }

    public void startServer() {

        print("Contact this sever on address: " + getSeverAddress() + " port: " + port);

        ServerSocket ss = null; 
        Socket socket = null;

        try {
            ss = new ServerSocket(port);
            socket = ss.accept();

            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream(), true);

            String msg = null;

            while (( msg = reader.readLine())  != null) {
                writer.write("From server: " + msg);
                print(msg);

                if(msg.toLowerCase().equals("Bye")) {
                    print("Client left");
                    break;
                }
            }

            ss.close();
            socket.close();
            reader.close();
            writer.close();

        } catch(SocketException e) {
            e.printStackTrace();            
        } catch (IOException i ) {
            i.printStackTrace();
            return;
        }
    }       

    private void print(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) {

        Server server = new Server(1111);
        server.startServer();

    }

}

And here is the client class:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;


public class Client {

    private Socket client;
    private BufferedReader reader;
    private PrintWriter writer;

    public Client(Socket socket)
    {
        client = socket;

        try{
            reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            writer = new PrintWriter(client.getOutputStream(), true);

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

    public void writeToServer() {       
        print("Write message to server");

        String msg = null;

        try {
            while((msg = reader.readLine()) != null) {
                writer.write("From client: " + msg);
                print(msg);

                if(msg.toLowerCase().equals("quit")) {
                    break;
                }
            }

            reader.close();
            writer.close();

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

    }

    private void print(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) {
        Socket socket = null;

        try {
            socket = new Socket("localhost", 1111);

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

        Client client = new Client(socket);
        client.writeToServer();
    }

}
5
  • Could you provide errors message? Commented Dec 19, 2014 at 10:54
  • 2
    Both your server and your client are waiting for input from the other - what do you expect to be the first message sent? Also note that you're never sending a line break as far as I can tell, so readLine wouldn't know that it's reached the end of a line... Commented Dec 19, 2014 at 10:54
  • What is after recving message, writer.write("From server: " + msg); Commented Dec 19, 2014 at 10:56
  • Ye Win, there are no error messages. The server just waiting for input. I do write input from the client class, but it just never reaches the server Commented Dec 19, 2014 at 10:58
  • There is no received message. I start the server, which then waits for client messages. I then start the client, and start writing messages to server, but the messages are never received by the server Commented Dec 19, 2014 at 11:01

2 Answers 2

1

Your mistake is server and client both sending/receiving messages background without printing anything. Like when you send message from client, Server received it and again write to the client and it become infinite loop.

Following things are wrong:

Server.java

 try {
         while (( msg = reader.readLine())  != null) {
            print(msg);
            if(msg.toLowerCase().equals("bye")) {
                print("Client left");
                break;
            }
            writer.write("From server: " + msg);
        }

Should be the last statement of loop writer.write("From client: " + msg); and if(msg.toLowerCase().equals("Bye")) should bye

Client.java

try {
           while((msg = reader.readLine()) != null) {
               print(msg);
                if(msg.toLowerCase().equals("quit")) {
                    break;
                }
              writer.write("From client: " + msg);
            }

It should be last in loop writer.write("From client: " + msg);

Sign up to request clarification or add additional context in comments.

2 Comments

If I remover writer.write(...) from the client, how can I send the message then? the writer contains the outputStream of the socket. This is not an attack, I am just wondering
Edited! when you develop it in rich UI then it will get removed and It wrapped in ActionListener of Send button
0

You're reading lines but you aren't sending lines. You need to send a line terminator.

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.