I'm trying to implement a SMTP protocol in my app, but I have a problem doing more than 1 write/read on a InputStream/OutputStream using codenameone SocketConnection.
This is what I have so far:
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");
}
}
}
This is the output I get:
220 mx.google.com ESMTP [...] -gsmtp
i get here!
And the program get stuck here.
Which means the "HELO ..." and its response works fine, but then it get stuck trying to read a second time from the BufferedInputStream.
I have tried to switch from BufferedInputStream to InputStream, all combination of in.mark(size); , in.reset() , reading only a fixed amount of bytes (in.read(response, 0 , 200)), but I did not manage to make it work.
What could I do to fix this?