2006年5月16日

       找一个开源项目来学习Java代码,自己先读一下,然后按自己的思路来重复实现其中一些功能!
一.POP3
协议

1.       目前的电子邮件基本上都是通过 POP3 网络协议接收的。建立双向的传输通道以后, pop3 服务程序会发送一系列基于 ASCII 字符的命令,下面的图大概说明了一下!
通常在学习的过程中,都是去写 email 客户端,其实一个方面是去写一些处理的 function ;另一方面基本上就是写一个实现 pop3 协议的包。

   常用的命令 USER,PASS,STAT,RETR,DELE QUIT. POP3 中只有两种回应码 ”+OK” ”-ERR” 。详细 
       的东西可以找RFC文档来仔细定义.

2.       package org.columba.ristretto.pop3 该包实现了 RFC1939 所指定的 POP3 协议( Post Office Protocol Version3

3.       在分析这个包之前,我们必须意识到一个完整的软件通常都会自己去实现一些辅助的功能,该开源项目里自己实现了一些 I/O 功能。(不过这里有个疑问啊,这样去实现一些东西,对于自己的一些应用是方便了,可是对于其他想复用这些代码的人来说,就需要更多的学习时间)

   接口 Source 继承自 CharSequence

   接口 Steamable 只有一个函数可以返回数据库结构的 InputStream 
 4.  这里有一个可以大概实现pop3client的例子,抄来的,很简单不过可以实现基本的pop3client功能,对协议的理解也小有帮助!
     import java.io.*;
import java.net.*;

class POP3Demo
{
 public static void main (String[] args)
 {
  String POP3Server = "server address";
  int POP3Port = 110;
  Socket clientsocket = null;
  try
  {
   clientsocket = new Socket(POP3Server, POP3Port);
   BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
   
   InputStream is =clientsocket.getInputStream();
   BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
   
   OutputStream os = clientsocket.getOutputStream();
   PrintWriter sockout = new PrintWriter(os,true);
   
   System.out.println("S:"+sockin.readLine());
   while(true)
   {
    System.out.print("C:");
    String cmd = stdin.readLine();
    sockout.println(cmd);
    
    String reply = sockin.readLine();
    System.out.println("S:" + reply);
    
    if(cmd.toLowerCase().startsWith("retr")&&reply.charAt(0)=='+')
    do
    {
     reply = sockin.readLine();
     System.out.println("S:"+reply);
     if(reply!=null && reply.length() > 0)
     if(reply.charAt(0) == '.')
     break;
    }
    while(true);
    
    if(cmd.toLowerCase().startsWith("quit"))
    break;
    
   }
  }
  catch(IOException e)
  {
   System.out.println(e.toString());
  }
  finally
  {
   try
   {
    if(clientsocket!=null)
    clientsocket.close();
   }
   catch(IOException e)
   {
   }
  }  
 }
}

posted @ 2006-05-16 17:39 Learning Java 阅读(141) | 评论 (1)编辑 收藏
 

       找一个开源项目来学习Java代码,自己先读一下,然后按自己的思路来重复实现其中一些功能!
一.POP3
协议

1.       目前的电子邮件基本上都是通过 POP3 网络协议接收的。建立双向的传输通道以后, pop3 服务程序会发送一系列基于 ASCII 字符的命令,下面的图大概说明了一下!
通常在学习的过程中,都是去写 email 客户端,其实一个方面是去写一些处理的 function ;另一方面基本上就是写一个实现 pop3 协议的包。

   常用的命令 USER,PASS,STAT,RETR,DELE QUIT. POP3 中只有两种回应码 ”+OK” ”-ERR” 。详细 
       的东西可以找RFC文档来仔细定义.

2.       package org.columba.ristretto.pop3 该包实现了 RFC1939 所指定的 POP3 协议( Post Office Protocol Version3

3.       在分析这个包之前,我们必须意识到一个完整的软件通常都会自己去实现一些辅助的功能,该开源项目里自己实现了一些 I/O 功能。(不过这里有个疑问啊,这样去实现一些东西,对于自己的一些应用是方便了,可是对于其他想复用这些代码的人来说,就需要更多的学习时间)

   接口 Source 继承自 CharSequence

   接口 Steamable 只有一个函数可以返回数据库结构的 InputStream 
 4.  有一个可以大概实现pop3client的例子(从别处抄来)很简单不过可以实现基本的pop3client功能,对协议的理解也小有帮助!
     import java.io.*;
import java.net.*;

class POP3Demo
{
 public static void main (String[] args)
 {
  String POP3Server = "server address";
  int POP3Port = 110;
  Socket clientsocket = null;
  try
  {
   clientsocket = new Socket(POP3Server, POP3Port);
   BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
   
   InputStream is =clientsocket.getInputStream();
   BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
   
   OutputStream os = clientsocket.getOutputStream();
   PrintWriter sockout = new PrintWriter(os,true);
   
   System.out.println("S:"+sockin.readLine());
   while(true)
   {
    System.out.print("C:");
    String cmd = stdin.readLine();
    sockout.println(cmd);
    
    String reply = sockin.readLine();
    System.out.println("S:" + reply);
    
    if(cmd.toLowerCase().startsWith("retr")&&reply.charAt(0)=='+')
    do
    {
     reply = sockin.readLine();
     System.out.println("S:"+reply);
     if(reply!=null && reply.length() > 0)
     if(reply.charAt(0) == '.')
     break;
    }
    while(true);
    
    if(cmd.toLowerCase().startsWith("quit"))
    break;
    
   }
  }
  catch(IOException e)
  {
   System.out.println(e.toString());
  }
  finally
  {
   try
   {
    if(clientsocket!=null)
    clientsocket.close();
   }
   catch(IOException e)
   {
   }
  }  
 }
}

posted @ 2006-05-16 17:38 Learning Java 阅读(94) | 评论 (0)编辑 收藏
仅列出标题