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

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

本文介绍了服务器未接收套接字客户端发送的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

服务器:

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()。

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

这篇关于服务器未接收套接字客户端发送的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:30