mpavlovic89
mpavlovic89

Reputation: 769

String from Client prints out on Socket's console in Client/Socket app in java

I am trying to create a simple client/server socket app. The problem I encountered is how to make and where should I put it, a part that will ask on the client side for the input and then print out on the server side that same input. I tried but no success.

GOAL: In console of Client class, when user inputs for example "My name is Mike", what I want is that at that time on Server's console the string "My name is Mike" prints out in new line.

MAIN

public class Main {
public static void main(String[] args) {
        System.out.println("The server has been summoned.\n");
        System.out.println("The server is waiting for client to come...");
        try {
            ServerSocket servertest = new ServerSocket(2014);
            while (true) {
                try {
                    Socket ser = servertest.accept();
                    new Server.ThreadSer(ser).start();
                } catch (IOException e) {}
            }
        } catch (IOException e) {System.err.println(e);}       
}}}

SERVER

public class Server {

    public static class ThreadSer extends Thread {
        private Socket s;
        public ThreadSer(Socket s) {
            this.s = s;
}
        @Override
        public void run() {
            try {
              String response = "This is the IP: " + s.getLocalAddress() + " that has come via port: "
                        + s.getLocalPort() + "\r\n";
                OutputStream out = s.getOutputStream();
                out.write(response.getBytes());

            } catch (IOException e) { System.err.println(e); }
}}}

CLIENT

public class Client {

    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket socket = new Socket("localhost", 2014);
        new OutputThread(socket.getInputStream()).start();
}

    public static class OutputThread extends Thread {
        private InputStream inputstream;
        public OutputThread(InputStream inputstream) {
            this.inputstream = inputstream;
}
        @Override
        public void run() {
            BufferedReader input = new BufferedReader(new InputStreamReader(inputstream));
            while (true) {
                try {
                    String line = input.readLine();
                    System.out.println(line);
                } catch (IOException exception) {
                    exception.printStackTrace();
                    break;
}}}}}

I missed something in my code.

Upvotes: 0

Views: 809

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

In your client you dont need to readLine from the server you need to outputstream to send a string to the server:

example:

in Client

     public static class OutputThread extends Thread {
        private InputStream inputstream;
        Scanner scanner;
        String message;
        public OutputThread(OutputStream outputstream) {
            this.inputstream = inputstream;
}
        @Override
        public void run() {
            ObjectOutputStream output = new ObjectOutputStream(outputstream);
            scanner = new Scanner(System.in);
            while (true) {
                try {
                    System.out.print("InputMessage: ");
                    message = scanner.nextLine();
                    System.out.println(message);
                    output.writeObject(message); //send the string to the server
                output.flush();
                } catch (IOException exception) {
                    exception.printStackTrace();
                    break;
}}}}}
  • What is it doing is that it ask for an input message and then pass the messag to the server using the output stream.

in server:

    public class Server {

    public static class ThreadSer extends Thread {
        private Socket s;
        public ThreadSer(Socket s) {
            this.s = s;
}
        @Override
        public void run() {
            try {
              String response = "This is the IP: " + s.getLocalAddress() + " that has come via port: "
                        + s.getLocalPort() + "\r\n";
                ObjectInputStream input = new ObjectInputStream(s.getInputStream()); 
               while(true){
                 Object object = input.readObject();
                 String command = ((String) object).trim(); //trim the string
                 System.out.println(command);
              }
            } catch (IOException e) { System.err.println(e); }
}}}
  • what it is doing is that it is looping and reads the line the client send to it using the inputstream. after it then prints it to the console of the server.

Upvotes: 2

Related Questions