Java implementation POP3 server

zhaozj2021-02-08  306

Sender: WLmqth (Tiantian), the letter area: Java I published this program to help the friends who love Java !! If you have anything you don't understand, you can ask // Write --1998.6import 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); SY stem.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 string content = "subject: hello"; string command = NULL;

String auth_user = "User"; string arg1; int Arg2 = 0; string arg3; 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 except) {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 ("User") {i = 1;} if (Command.EqualsignoreCase ("pass") {i = 2;} if (Command.equalsignoreCase ")) {i = 3;} if (command.equalsignorecase (" quit ") {i = 4;} IF (command.equalsignorecase (" list ")) {i = 5;} if (command.equalsignorecase RETR ")) {i = 6 } IF ("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 (use, password == '1') {CHECK = 2; / * msgnum = readmail (username); * / out.println ("

OK ");} else {outputln (" - Err " " sorry automation "; check = 0;}}} else} else {out.println (" - err ");} i = 0; Break; Case 3: if (Check == 2) {msgnum = readmail (username); out.println (" OK" " MSGNUM " TOTALSIZE);} else {outputln ("- Err"); } i = 0; Break; Case 4: Out.println (" OK Bye Welcome to Tianhua's mail system nexttime); 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.prin TLN ("- Err No Msg Retrived");}} (arg2 <= msgnum) {OUT.PRINTLN (" OK"); out.println (content); out.println (""); Out.print (" Out.println (); out.println ("."); System.out.Println ("MSG Finished");} else {outputln ("no sudh 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 ("sORRY" EXCPT); IN. Close (); out.close (); mysocket.close ();}} catch (ooException 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

转载请注明原文地址:https://www.9cbs.com/read-880.html

New Post(0)