本人公布此程序希望能给热爱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;
}
}
闽公网安备 35060202000074号