java-服务器未收到套接字客户端发送的消息

我有套接字服务器(java桌面应用程序)正在等待来自java webapp(套接字客户端)的连接.通讯正常,我在客户端看到了来自服务器的消息,但是当我发送消息时,我在服务器端什么都没有收到.
可能是什么问题?当我用telnet检查服务器时,一切正常.下面是我的代码:

服务器:

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");
        }

客户:

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();
    }
}

感谢帮助!

最佳答案
您正在阅读的行,但您不是在写行.在消息中添加行终止符,或使用println().

转载注明原文:java-服务器未收到套接字客户端发送的消息 - 代码日志