Server not receiving message sent by socket client

淺唱寂寞╮ 提交于 2019-12-20 03:18:30

问题


I have socket server (java desktop app) that is waiting for connection from java webapp (socket client). The communication looks ok, I see message from the server on the client side, but when I send the message, I don't receive anything on my server side. What can be the problem? When I checked server with telnet, everything is working ok. Below is my code:

SERVER:

public class ThumbnailGenerator {
    static Logger log = Logger.getLogger("ThumbnailGenerator");

    public static List<ThumbnailTask> taskqueue = new ArrayList<ThumbnailTask>();

    public static void main(String[] args) throws IOException {
        ThumbnailTask urlobject = new ThumbnailTask();
        urlobject.setUrl("http://www.google.com");
        urlobject.setOutputfile("newFile");
        urlobject.setStatus(0);
        taskqueue.add(urlobject);
        MyThread mt = new MyThread();
        mt.start();
        startServer();
    }

    public static void startServer() {
        final int portNumber = 2000;
        log.info("Creating server socket on port " + portNumber);

        ServerSocket serverSocket;

        try {
            serverSocket = new ServerSocket(portNumber);
            log.info("Server waiting for clients!");
            while (true) {
                Socket socket = serverSocket.accept();
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os, true);
                pw.println("Give URL address!");
                pw.flush();
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String str = br.readLine();

                log.info("Message received from client is " + str);

                String[] ar = str.split(",");
                ThumbnailTask urlobject = new ThumbnailTask();
                urlobject.setUrl(ar[0]);
                urlobject.setOutputfile(ar[1]);
                urlobject.setStatus(0);

                synchronized (taskqueue) {
                    taskqueue.add(urlobject);
                }

                pw.println("Starting making thumbnail of " + ar[0]);

                pw.close();
                socket.close();
                log.info("Thumbnail of: " + ar[0] + " will be created");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    class MyThread extends Thread {
        static Logger log = Logger.getLogger("MyThread");
        boolean running = true;

        public void run() {
            log.debug("ENTRY THREAD");
            while (running) {
                if (!ThumbnailGenerator.taskqueue.isEmpty()) {
                    Iterator<ThumbnailTask> thumbnailiterator = ThumbnailGenerator.taskqueue.iterator();
                    int count = 0;
                    while (thumbnailiterator.hasNext()) {
                        ThumbnailTask task = thumbnailiterator.next();
                        boolean thumbnailresult = false;
                        if (task.getStatus() == 0) {
                            thumbnailresult = ThumbnailGenerator.makeThumbnail(task.getUrl(), task.getOutputfile());
                            if (thumbnailresult = true) {
                                task.setStatus(1);
                                //ThumbnailGenerator.taskqueue.set(count, task);
                                log.info("RESULT = true");
                            }
                        }
                        count++;
                    }
                } else {
                    running = false;
                }
                try {
                    MyThread.sleep(10000);
                } catch (InterruptedException e) {
                    running = false;
                    e.printStackTrace();
                }
            }
            log.debug("EXIT");
        }

CLIENT:

public static void startSender(String url) {
    final String host = "localhost";
    final int portNumber = 2000;
    log.info("Creating socket to '" + host + "' on port " + portNumber);

    try {
        while (true) {
            Socket socket = new Socket(host, portNumber);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            log.info("Server says:" + br.readLine());

            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);


            String sendMessage = url;
            bw.write(sendMessage);
            bw.flush();
            log.info("Message sent to the server : " + sendMessage);

            log.info("Server says:" + br.readLine());

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

Thanks for help!


回答1:


You're reading lines but you're not writing lines. Add a line terminator to the message, or use println().



来源:https://stackoverflow.com/questions/30505760/server-not-receiving-message-sent-by-socket-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!