我正在尝试在我的应用程序中实现SMTP协议,但是使用codenameone的SocketConnection进行多个InputStream/OutputStream
的读写时遇到了问题。
这是我目前的代码:
Socket.connect("<google ip>",25,new SocketConnection(){
@Override
public void connectionError [...]
@Override
public void connectionEstablished(InputStream is, OutputStream os){
try{
BufferedInputStream in = new BufferedInputStream(is);
BufferedOutputStream out = new BufferedOutputStream(os);
byte[] response = new byte[1024];
String z;
int size;
out.write( ("HELO mydomain.me").getBytes() );
out.flush();
size = in.read(response);
//in.mark(size);
//in.reset();
z = new String(response, 0 ,size);
System.out.println(z);
response = new byte[1024];
out.write( ("MAIL FROM: <myaddress@bubu.me>").getBytes() );
out.flush();
System.out.println("I get here!");
size = in.read(response);
System.out.println("I NEVER get here!");
z = new String(response, 0 ,size);
System.out.println(z);
[... rest of SMTP that I never reach ...]
}catch(IOException ex){
System.out.println("I don't even get connection errors");
}
}
}
这是我的输出结果:
220 mx.google.com ESMTP [...] -gsmtp
我在这里停下来了。这意味着 "HELO ..." 和它的响应正常工作,但是接下来它在尝试第二次从 BufferedInputStream 读取时卡住了。
我尝试过从 BufferedInputStream 切换到 InputStream,使用 all combination of in.mark(size); , in.reset() ,只读取固定数量的字节(in.read(response, 0, 200)),但我无法让它工作起来。
我该怎么做才能解决这个问题?
InpuStream
,OutputStream
和固定的MAIL FROM:
时,同样的问题仍然存在。 我认为我不能在创建应用程序时使用com.codename1
之外的外部库,这可能会导致冲突。 - Davide