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

时间:2015-05-28 11:48:44

标签: java sockets java-websocket

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

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

客户端:

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

你正在读行,但你不是在写行。在邮件中添加行终止符,或使用println().