Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server not receiving input from client

Tags:

java

java-io

I have a server and client. My problem is that when the client writes messages, the server is not receiving the messages. Server code

public class BankServer {

    private int port;

    private BufferedReader reader = null; 
    private PrintWriter writer = null;

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

    public void startServer() {
        print("Contact server at: " + getServerAddress() + ": port: " + port);

        while(true) {
            try {
                ServerSocket ss = new ServerSocket(port);
                Socket client = ss.accept();

                if(client != null) {
                    print("Client connected");
                }

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

                String msg = null;

                print("Server waiting for input");

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

                client.close();
                ss.close();
                closeStreams();

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

    private void closeStreams() {
        if(reader != null) { 
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            } }

        if(writer != null) {
            writer.close();
        }       
    }

    private String getServerAddress() {

        InetAddress inetAdr = null;

        try {
            inetAdr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        String host = inetAdr.getHostAddress();

        return host;
    }

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

    public static void main(String[] args) {        
        new BankServer(2222).startServer();
    }

}

I have this code, where the Server waits for input:

ServerSocket ss = new ServerSocket(port);
        Socket client = ss.accept();

        if(client != null) {
            print("Client connected");
}

From this code, I can see that a connection is made. But when client sends messages, they are not printed by the server

Here is the client code:

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 BankClient {

    private PrintWriter writer = null;
    private BufferedReader reader = null;
    private Socket socket;

    private int port;
    private String adr;

    public BankClient(String address, int port) 
    {
        adr = address;
        this.port = port;

        try {
            socket = new Socket(adr, this.port);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream());


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

    public void writeMsg(String msg) {

        try {
            while((msg = reader.readLine()) != null) {
                writer.print("From client: " + msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        BankClient client = new BankClient("127.0.0.1", 2222);
        Scanner scanner = new Scanner(System.in);

        System.out.println("Waiting for input");

        while(true) {
            String msg = scanner.nextLine();
            client.writeMsg(msg);

            if(msg.equals("quit")) { 
                System.out.println("Bye");
                break; 
            }                       
        }

        scanner.close();
    }
}

This is the first time I work with streams, and I am also pretty new to programming. I have found a lot of help on stackoverflow, and google, but I just can't figure this out. Thanks for any help you can provide

like image 485
MOR_SNOW Avatar asked Sep 01 '26 20:09

MOR_SNOW


1 Answers

Replace :

public void writeMsg(String msg) {

        try {
            while((msg = reader.readLine()) != null) {
                writer.print("From client: " + msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

By :

public void writeMsg(String msg) {
        try {
            DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
            outToServer.writeBytes("From client: " + msg + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Client Side:

enter image description here

Server Side :

enter image description here

like image 94
Jad Chahine Avatar answered Sep 03 '26 09:09

Jad Chahine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!