简体   繁体   中英

Server not receiving input from client

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

Change writer.print("From client: " + msg); to writer.println("From client: " + msg); and remove the while((msg = reader.readLine()) != null) at the client.

EDIT: And you have to add writer.flush(); . That worked for me.

EDIT2: The complete solution. Change BankClient here:

public void writeMsg(String msg) { writer.println("From client: " + msg); writer.flush(); }

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:

在此输入图像描述

Server Side :

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM