分享
 
 
 

java 实现pop3 server

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

本人公布此程序希望能给热爱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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有