⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailconnection.java

📁 基于WINCE5.2的ARM嵌入式手机软件的开发
💻 JAVA
字号:
/* * 该类实现了到邮件服务器的连接,提供了send receive等方法 */package MobileEmail;import javax.microedition.io.SocketConnection;import javax.microedition.io.Connector;import java.io.*;/** * * @author Administrator */public class MailConnection {    private static final byte CR=0x0D;//回车    private static final byte LF=0x0A;//换行    private static final byte[] CRLF={CR,LF};    private SocketConnection socket;    private OutputStream os;    private InputStream is;    private byte[] buffer=new byte[128];    public MailConnection(String host,int port) throws IOException{        socket=(SocketConnection)Connector.open("socket://"+host+":"+port);        os=socket.openOutputStream();        is=socket.openInputStream();    }    //关闭连接    public void close() throws IOException{        if(socket!=null){            socket.close();            socket=null;        }    }    public boolean isConnection(){        return socket!=null;    }    //发送字符串s到服务器    public void send(String s) throws IOException{        byte[] bytes=s.getBytes();        int len=bytes.length;        if(len==0){            os.write(CRLF, 0, 2);//结束符号        }        else{            int i=0;            int j=0;            while(i<len){                while(j<len&&bytes[j]!=CR&&bytes[j]!=LF){                    j++;                }                os.write(bytes, i, j-1);                os.write(CRLF, 0, 2);                if(j<len-1&&bytes[j]==CR&&bytes[j+1]==LF){                    j++;                }                i=++j;            }        }    }    //接搜服务器返回的字符串 字符串已CRLF为结束标记    public String receive() throws IOException{        boolean stop=false;        int n=-1;        StringBuffer result=new StringBuffer();        while(!stop){            int count=0;            while(true){                n=is.read(buffer, count, 1);                if(n==-1){                    try{                       close();                    }                    catch(IOException ioe){                        ioe.printStackTrace();                        //throws new IOException("连接已经关闭!");                    }                                    }                else if(n==0){                    Thread.yield();                }                else{                    byte b=buffer[count];                    if(b==CR){                    }                    else if(b==LF){                       stop=true;                       break;                    }                    else{                       count++;                       if(count>=buffer.length){                           break;                       }                    }                }            }            result.append(new String(buffer,0,count));        }        return result.toString();    }   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -