Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client not reading data from socket?

I have spent a while reading other posts, and nobody seems to have the same case as me. I have tried all solutions I have read with no results, so I decided to create this question.

I have been working on a server/client application, and the client seems to not read data from the socket, but the server can read data the client sends. The client freezes while trying to read the line. Here's the code I have:

Client:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

public class Main {

    public static Socket s = connect();

    public static void sendMessage(String msg) {
        if (s != null) {
            PrintWriter outWriter = null;
            try {
                outWriter = new PrintWriter(s.getOutputStream(), true);
                outWriter.println(msg);
                outWriter.flush();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                outWriter.close();
            } finally {
            }
        } else {
            System.err.println("Error, socket is null!");
        }
    }

    public static String readMessage() {
        if (s != null) {
            Scanner in = null;
            try {
                in = new Scanner(s.getInputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            String ss = "";
            while (in.hasNext()) {
                ss += in.next();
            }
            System.out.println("REPONSE:" + ss);
            return ss;
        } else {
            System.err.println("Error, socket is null!");
        }
        return "err/readMessage";
    }

    public static Socket connect() {
        try {
            Socket sss = new Socket("localhost", 25586);
            sss.setKeepAlive(true);
            return sss;
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(-1);
        }
        return null;
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sendMessage("HELO");
                        System.out.println("Thread initialized.");

                        while (true) {
                            try {
                                System.out.println("Awaiting message...");
                                Thread.sleep(100);
                                String messages = readMessage();
                                System.out.println("Message recieved! '" + messages + "'");
                                String[] message = messages.split("/");
                                System.out.println(messages);
                                if (message[0].equalsIgnoreCase("DERP")) {                                       // err/reason
                                    System.out.println("IT'S FINALLY WORKING!");
                                } else {
                                    System.out.println("Didn't work :( response:" + messages);
                                }
                            } catch (InterruptedException ex) {
                                ex.printStackTrace();
                            }

                        }
                    }
                });
                t.start();
            }
        });
    }
}

Server:

import java.util.List;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main { /* SERVER */


    public static List<EchoThread> list = new ArrayList<>();

    public static int PORT = 25586;

    public static void main(String[] args) throws IOException {
        new Main(PORT);
    }

    public Main(int port) throws IOException {
        this.PORT = port;
        ServerSocket serverSocket = null;
        Socket socket = null;

        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();

        }
        while (true) {
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                System.out.println("I/O error: " + e);
            }
            // new threa for a client
            EchoThread s = new EchoThread(socket);
            s.start();
            list.add(s);
        }
    }

    public static void sendMessageToAll(String ss) {
        for (EchoThread s : list) {
            s.allMessage(ss);
        }
    }
}

class EchoThread extends Thread {

    protected Socket socket;
    InputStream inp = null;
    BufferedReader brinp = null;

    public EchoThread(Socket clientSocket) {
        this.socket = clientSocket;
        try {
            inp = socket.getInputStream();
            brinp = new BufferedReader(new InputStreamReader(inp));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        String line;
        while (true) {
            try {
                line = brinp.readLine();
                if ((line == null) || line.equalsIgnoreCase("EXIT")) {
                    socket.close();
                    return;
                } else {
                    handle(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }
    }

    public void handle(String s) {
        String[] keys = s.split("/");
        System.out.println("random: Handling request\"" + s + "\"");
        System.out.println("Response: DERP");
        if (s.equalsIgnoreCase("HELO")) {
            System.out.println("Message recieved:" + s);
        }
        respond("DERP");
    }

    public void respond(String s) {
        try {
            System.out.println("Response_WILLBE:" + s);
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os, true);
            pw.println(s);
            pw.flush();
            System.out.println("Message sent!");
        } catch (Exception ex) {
            Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void allMessage(String s) {
        respond(s);
    }
}

I tried the flush() code and \r\n and println fixes, but none worked! -- Thank you for reading!

like image 880
meowy cat Avatar asked Nov 29 '15 00:11

meowy cat


1 Answers

I'm very curious about the differences between the client's and server's methods of communication. For instance, the client uses a Scanner to read input while the server uses a BufferedReader (which is my personal preference). Just a suggestion: be consistent.

Now -- firstly, the client only sends one message, but then begins to read indefinitely in an infinite loop. Seeing as you know exactly how the server should respond after you send "HELO" to it (it should respond with one line, "DERP"), there is no reason to read from the server in any sort of loop.

The same issue exists on the server. As the client is right now, it will always only send one line to the server ("HELO"). Consequently, the server should only expect one line and only read one line. There is absolutely no reason to read input within a loop. However, the actual issue persists only in the client's code:

You currently have while(in.hasNext()) as a conditional as to how long the client will read input from the server. The hasNext method differs in logical function depending on the method of communication. In the case of socket communication, hasNext expects a stream and, as such, will always return true unless the socket is closed.

Avoid using loops in socket communication in general unless you don't know how many lines to read. In such a scenario, you should first have the sender send some sort of number to the receiver, and then the receiver should read the subsequent lines in a for loop reading 'n' number of times (where n is the received number).

Here's a very basic client / server communication program using PrintWriters and BufferedReaders.

Client

public static void main(String[] args){
    try{
        Socket socket = new Socket("localhost", 12345);
        PrintWriter out = new PrintWriter(socket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.write("HELO\n"); //print("HELO\n") and println("HELO") should work too.
        out.flush();
        System.out.println("Server says " + in.readLine());
        in.close();
        out.close();
        socket.close();
    }catch(IOException e){e.printStackTrace();}
}

Server

public static void main(String[] args){
    try{
        ServerSocket serverSocket = new ServerSocket(12345);
        while(true){
            new Thread(new ClientConnectionThread(serverSocket.accept())).start();
        }
    }catch(IOException e){e.printStackTrace();}
}

private class ClientConnectionThread implements Runnable{
    private Socket socket;
    public ClientConnectionThread(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run(){
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream());
            System.out.println("Client says " + in.readLine());
            out.write("HELO Back!\n"); //again, print("HELO Back!\n") and
                //println("HELO Back!") should also work
            out.flush();
            out.close();
            in.close();
            socket.close();
        }catch(IOException e){e.printStackTrace();}
    }
}

Notice that no loops are necessary in the actual communication.

like image 140
Alexander Guyer Avatar answered Sep 30 '22 08:09

Alexander Guyer