网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.Java技巧:部署WAR文件和绑定对象.
.JPanel加载图像的过程.
.J2SE应用:JAVA语言关于字符串替.
.JAR 文件揭密.
.基于Nokia S40的猜数字游戏之一.
.编程必备经典:Java常见问题集锦(.
.提高 Java 代码的性能.
.如何用SQL生成SQL批处理文件的方.
.Java平台上的CRM系统.
.Java编程获取硬盘空间.
.Java入门笔记8_JavaTools.
.Apache的完全安装转载.
.完整图解 Tomcat 5.0.28 安装笔记.
.JDBC编程基础第二部分.
.java 技术分析.
.Java的秘密:Swing的实用工具.
.功能完善的Java连接池调用实例.
.java中,this用途总结。。。.
.finalize( ) 方法.
.Sun复兴2大支柱:x86、Sparc服务.

java 实现pop3 server

发表日期:2008-1-5



本人公布此程序希望能给热爱Java的朋友一点帮助!!
如大家有什么不明白的地方可以询问
//write --1998.6

import java.net.*;   
import java.util.*;   

public class pop3server{   
private static final int SERVER_PORT=110;   
private static final int MAX_CLIENT=500;   
private ServerSocket listenSocket=null;   
private boolean keeprunning=true;   
private InetAddress ip;   
public static void main(String[] args){   
pop3server server=new pop3server();   
server.impserver();   
}   
public void impserver(){   
Socket clientSocket=null;   
try{   
while(keeprunning){   
clientSocket=listenSocket.accept();   
int numThreads=Thread.activeCount();   
System.out.println(numThreads);   
manageconnection newhandle=new manageconnection(clientSocket);   
Thread newhandlethread=new Thread(newhandle);   
newhandlethread.start();   
System.out.println("hello");   

}   
}   
catch(IOException excpt){   
System.err.println("Sorry ,Failed I/O:"+excpt);   
}   

}   

public pop3server()   
{   
try{   
listenSocket=new ServerSocket(SERVER_PORT,MAX_CLIENT);   
}catch(IOException excpt){   
System.err.println("SOrry to open port "+SERVER_PORT+":"+excpt);   
System.exit(1);   
}   
}   
}   

/*follow is manage and process command class*/   

class manageconnection implements Runnable{   
private static final boolean AUTOFLUSH=true;   
private Socket mySocket=null;   
private PrintWriter out=null;   
private BufferedReader in=null;   
private boolean checkedpass=false;   
private int msgnum=0;   
private long[] msgsize=new long[100];   
private long totalsize=0;   
private String username=null;   
public manageconnection(Socket newSocket){   
mySocket=newSocket;   
}   
public void run(){   
String nextline=null;   
String passWord;   
int check=0;   
int strlen;   
String content="Subject:hello";   
String command=null;   
String auth_user="user";   
String arg1;   
int arg2=0;   
String arg3;   
int i=0;   
int count=0;   
int time=0;   
try{Thread.currentThread().sleep(10);   
}catch(Exception e){   
System.out.println(e);   
}   
try{   
mySocket.setTcpNoDelay(true);   
time=mySocket.getSoTimeout();   
}catch(SocketException excpt){   
System.out.println(excpt);   
}   
try{   
out=new PrintWriter(mySocket.getOutputStream(),AUTOFLUSH);   
in=new BufferedReader(new InputStreamReader(mySocket.getInputStream())
   
);   
/*System.out.println("hello thread1");*/   
out.println("+OK"+" TianHua's pop3 server");   
try{   
while(true){   
if(check!=3){   
try{   
nextline=in.readLine();}catch(Exception excpt){   
System.out.println("sorry:"+excpt);   
break;   
}   
System.out.println(count+" "+nextline);   

if(nextline.length()<4){
out.println("-ERR");
}else{
command=nextline.substring(0,4);
if(command.equalsIgnoreCase("user")){
i=1;
}
if(command.equalsIgnoreCase("pass")){
i=2;
}
if(command.equalsIgnoreCase("stat")){
i=3;
}
if(command.equalsIgnoreCase("quit")){
i=4;
}
if(command.equalsIgnoreCase("list")){
i=5;
}
if(command.equalsIgnoreCase("retr")){
i=6;
}
if(command.equalsIgnoreCase("dele")){
i=7;
}

switch(i){
case 1:if(check==0){
check=1;
if(nextline.length()<5){
out.println("-ERR");
}else{arg1=nextline.substring(5);
username=arg1;
out.println("+OK");
}
}else{out.println("-ERR");}
i=0;
break;
case 2:if(check==1){
if(nextline.length()<5){
out.println("-ERR");
}else{
arg1=nextline.substring(5);
password=arg1;
if(check(username,password)=='1'){
check=2;
/*msgnum=readmail(username);*/
out.println("+OK");}else{
out.println("-ERR"+" sorry auth failed");
check=0;}
}
}else{out.println("-ERR");}
i=0;
break;
case 3:if(check==2){
msgnum=readmail(username);
out.println("+OK"+" "+msgnum+" "+totalsize);
}else{
out.println("-ERR");
}
i=0;
break;
case 4:out.println("+OK BYE BYE welcome to TianHua's Mail System next

time");
check=3;
i=0;
break;
case 5:if(check==2){
out.println("+OK");
out.println(msgnum+" "+totalsize);
int ii,iii=0;
for(ii=1;ii<=msgnum;ii++){
out.println(ii+" "+msgsize[iii]);
iii++;
}
out.println(".");
}else{
out.println("-ERR");
}
i=0;
break;
case 6:if(check==2){
if(nextline.length()<5){
out.println("-ERR");
}else{
arg1=nextline.substring(5);
out.println("+OK");
System.out.println(arg1);
printmail(Integer.parseInt(arg1),username);
out.print("\r\n");
out.print(".");
out.print("\r\n");
boolean st=out.checkError();
System.out.println(st);
/*if(nextline.length()<6){
out.println("-ERR");
}else{
try{
arg2=Integer.parseInt(nextline.substring(5));
}catch(NumberFormatException except)
{
out.println("-ERR");
break;
}
if(msgnum==0){
out.println("-ERR no msg retrived");
}
if(arg2<=msgnum){
out.println("+OK");
out.println(content);
out.println("");
out.print(".");
out.println();
out.println(".");
System.out.println("msg finished");
}else{
out.println("no sUCh msg");
}
}*/
}
}else{out.println("-ERR");
}
i=0;
System.out.println("retr finished");
break;
case 7:if(check==2){
out.println("+OK");
}else{
out.println("-ERR");
}
i=0;
break;
default:out.println("-ERR");
i=0;
break;


}
}
}else{
out.close();
in.close();
mySocket.close();
break;
}

}
}catch(NullPointerException excpt){
System.out.println("sorry "+excpt);
in.close();
out.close();
mySocket.close();
}

}catch(IOException excpt){
System.err.println("Failed I/O:"+excpt);
}

System.out.println("bye");

}
private void printmail(int msgnumber,String uname){
String msgdir="/webmail/userinfo/";
String msgfile=msgdir+uname+"/"+Integer.toString(msgnumber);
System.out.println(msgfile);
long filepoint=0;
try{
RandomAccessFile file=new RandomAccessFile(msgfile,"r");
long msglength=file.length();
while(filepoint<msglength){
String s=file.readLine();
out.println(s);
try{
Thread.currentThread().sleep(5);
}catch(Exception e){
System.out.println(e);
}
filepoint=file.getFilePointer();
}
}catch(Exception e){
System.out.println(e);
}

}


/*check your username and password*/
private char check(String user,String pass){
String[] arg=new String[2];
int c=0;
char value='0';
arg[0]="uname="+user;
arg[1]="pass="+pass;

try{

Process child=Runtime.getRuntime().exec("/disk2/FTP/pub/login"

,arg);
InputStream inn =child.getInputStream();
while((c=inn.read())!=-1){
value=(char)c;
}
inn.close();

try{child.waitFor();}catch(InterruptedException e){

e.printStackTrace();
}

}catch(IOException e){
System.err.println(e);
}
return value;

}


private int readmail(String uname){
String[] arg=new String[1];
arg[0]="username="+uname;
int[] msg=new int[3];
int i=0,j=0;
int msgnum=0;
int c;
try{
Process child=Runtime.getRuntime().exec("/disk2/ftp/pub/readmail",

arg);
InputStream inn =child.getInputStream();
while((c=inn.read())!=-1){
msg[i]=c-48;
i++;
}
inn.close();
try{child.waitFor();}catch(InterruptedException e){
e.printStackTrace();
}
}catch(IOException e){
System.err.println(e);
}
for(j=0;j<=i;j++){
msgnum=msgnum+msg[j];
}
System.out.println(msgnum);
int k=0;
try{
for(j=1;j<=msgnum;j++){
RandomAccessFile file=new RandomAccessFile("/webmail/userinfo/"+uname+

"/"+Integer.toString(j),"r");
msgsize[k]=file.length();
totalsize=msgsize[k]+totalsize;
System.out.println(msgsize[k]);
k++;
}
}catch(Exception e){
System.out.println(e);
}
System.out.println(totalsize);
return msgnum;
}

}  


上一篇:J2ME中建立Splash启动界面 人气:622
下一篇:java获得鼠标所在位置的图像的rgb值 人气:1114
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐