简体   繁体   English

Java,SSLSocket,未收到答案

[英]Java, SSLSocket, receiving no answer

I'm trying to use SSL with IMAP in java. 我正在尝试在Java中将SSL与IMAP一起使用。 I do not want to use the IMAP class. 我不想使用IMAP类。 For some reason, when I send the n th message, I receive the answer to message n-2, and not to message n-1. 出于某种原因,当我发送第n条消息时,我收到的消息是n-2,而不是消息n-1。 Which means that I don't receive any answer to the first message sent until I send the second message. 这意味着在发送第二条消息之前,我不会收到对第一条消息的任何答复。 Can anyone spot what's wrong in the following minimal code ? 在下面的最小代码中,谁能发现什么地方出了问题? (It is indeed minimal, apart from the println, which, I guess, help debugging) (除了println,这确实很小,我想这有助于调试)

import java.io.*;
import javax.net.ssl.*;
public class Mail{
    static String server = "imap.gmail.com";
    static String user   = "straightouttascript@gmail.com";
    static String pass   = "azerty75";
    public static void print (PrintWriter to, String text){
        System.out.println("sent : "+text);
        to.println(text+ "\r");
        to.flush();
    }

    public static void read  (BufferedReader from) throws InterruptedException, IOException {
        do {
            String line = from.readLine();
            System.out.println("received: "+line);
        } while (from.ready());
    }

    public static void main(String[] args){
        try{
            SSLSocket sslsocket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(server, 993);
            System.out.println("Start connexion");

            BufferedReader from = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
            //             read(from);

            PrintWriter to = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sslsocket.getOutputStream())), true);
            print(to,"a1 login "+user+" "+pass);
            read(from);/*exepcted:
                         OK gimap ready 
                         a1 OK login@host authenticated (Success)*/

            sslsocket.close();
            System.out.println("End connexion");
        }

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

IMAP is not a pingpong protocol. IMAP不是乒乓协议。 The server doesn't send one line in response to one of yours. 服务器不会发送一行响应您的其中一条。

Rather, you send commands and the server sends information. 相反,您发送命令,服务器发送信息。 The server is permitted to send you more information than you asked for, so you can get seven responses to one command, and you can even get a response without sending a command at all, which is then called an unsolicited response. 允许服务器向您发送比您要求的更多的信息,因此您可以对一个命令获得七个响应,甚至可以在根本不发送命令的情况下获得响应,这就是所谓的未经请求的响应。 Strange phrase. 奇怪的短语。 Unsolicited responses are used by some servers to notify you about new mail, by more to notify you about flag changes on messages, and by (almost?) all to notify you that they're about to close your connection. 一些服务器使用未经请求的响应来通知您有关新邮件的信息,其他服务器则用于通知您有关邮件标志更改的信息,所有(几乎?)通知所有人它们将要关闭您的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM