//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;
}
闽公网安备 35060202000074号