Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class SmtpServer2 {
- static Vector<String> cmd = new Vector<String>();
- static int whichCommand(String s) {
- if (cmd.size()==0) {cmd.add("HELO"); cmd.add("EHLO"); cmd.add("MAIL"); cmd.add("RCPT"); cmd.add("DATA"); cmd.add("."); cmd.add("QUIT"); }
- return cmd.indexOf(s);
- }
- public static void main(String args[]) throws IOException{
- ServerSocket ss = new ServerSocket(25000);
- do {
- Socket s = ss.accept();
- PrintWriter fos = new PrintWriter(new FileOutputStream("received.txt"),true);
- PrintWriter sout = new PrintWriter(s.getOutputStream(),true);
- Scanner sin = new Scanner(s.getInputStream());
- // first greet the client
- System.out.println("NEW CLIENT");
- sout.println("220 localhost tiny SMTP server\r");
- boolean done=false;
- int state=0;
- while(!done) {
- String command = sin.nextLine();
- String[] tokens = command.split(" ");
- int cmd = whichCommand(tokens[0]);
- switch(state) {
- case 0: if(cmd==0 || cmd==1) {sout.println("250 Hello "+tokens[1]); state=1; } else done=true; break;
- case 1: if(cmd==2) {sout.println("250 Sender ok "); state=2; } else done=true; break;
- case 2: if(cmd==3) {sout.println("250 Receiver ok "); state=3; } else done=true; break;
- case 3: if(cmd==4) {sout.println("354 data reception "); state=4; } else done=true; break;
- case 4: if(cmd==5) {sout.println("250 Message accepted for delivery\r"); state=5;}
- else fos.println(command); break;
- case 5: if(cmd==6) sout.println("221 Closing connection\r"); done=true; break;
- }
- }
- System.out.println("CLIENT GONE");
- fos.close(); sin.close(); s.close();
- }while(true); // it's a server, endless loop !!!
- }
- }
Add Comment
Please, Sign In to add comment