import java.io.*;
import java.net.*;
/*
* Client App upon SMTP
* Name: Hussain Al Zerjawi
* Assignment: HW04
* Due date: 02/29/2020
*/
public class Main {
public static void main(String[] args) throws Exception {
Socket tcpSocket = null;
PrintWriter socketOut = null;
BufferedReader socketIn = null;
String repeat = "Yes";
long startTime = 0;
long endTime = 0;
long RTT = 0;
BufferedReader systemInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please type in the DNS name or IP address of the Server machine.");
String serverMachine = systemInput.readLine();
try {
startTime = System.currentTimeMillis();
tcpSocket = new Socket(serverMachine, 5010);
socketOut = new PrintWriter(tcpSocket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
String firstResponse = socketIn.readLine();
System.out.println(firstResponse);
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("RTT of establishing this TCP connection: " + RTT + " ms");
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverMachine);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: "
+ serverMachine);
System.exit(1);
}
while (repeat.contentEquals("Yes") || repeat.contentEquals("yes")) {
String emailContent = "";
String contentLine = "";
socketOut = new PrintWriter(tcpSocket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
System.out.println("Please type in the sender's email address.");
String senderEmail = systemInput.readLine();
System.out.println("Please type in the receiver's email address.");
String receiverEmail = systemInput.readLine();
System.out.println("Please type in the subject.");
String subject = systemInput.readLine();
System.out.println("Please type in the email content (Single'.' on a line indicates end of content).");
while(true){
contentLine = systemInput.readLine();
if(contentLine.contentEquals(".")){
emailContent = emailContent + contentLine + "\r\n";
break;
}
emailContent = emailContent + contentLine + "\r\n";
}
// Step 4) a)
startTime = System.currentTimeMillis();
socketOut.println("HELO " + serverMachine);
String secondResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(secondResponse);
// Step 4) b)
startTime = System.currentTimeMillis();
socketOut.println("MAIL FROM: " + senderEmail);
String thirdResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(thirdResponse);
// Step 4) c)
startTime = System.currentTimeMillis();
socketOut.println("RCPT TO: " + receiverEmail);
String fourthResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(fourthResponse);
// Step 4) d)
startTime = System.currentTimeMillis();
socketOut.println("DATA");
String fifthResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(fifthResponse);
// Step 4) e)
startTime = System.currentTimeMillis();
socketOut.println("To: " + receiverEmail + "\r\n" + "From: " + senderEmail + "\r\n" + "Subject: " + subject + "\r\n\r\n" + emailContent);
String sixthResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(sixthResponse);
System.out.println("Would you like to continue? Please type in either yes or no or quit.");
repeat = systemInput.readLine();
if (repeat.contentEquals("QUIT") || repeat.contentEquals("quit") || repeat.contentEquals("Quit") || repeat.contentEquals("no") || repeat.contentEquals("No")) {
socketOut.println("QUIT");
String seventhResponse = socketIn.readLine();
System.out.println(seventhResponse);
socketOut.close();
socketIn.close();
systemInput.close();
tcpSocket.close();
System.exit(0);
break;
}
else{
socketOut.println("Yes");
}
}
}
}
import java.io.*;
import java.net.*;
/*
* Client App upon SMTP
* Name: Hussain Al Zerjawi
* Assignment: HW04
* Due date: 02/29/2020
*/
public class client {
public static void main(String[] args) throws Exception {
Socket tcpSocket = null;
PrintWriter socketOut = null;
BufferedReader socketIn = null;
String repeat = "Yes";
long startTime = 0;
long endTime = 0;
long RTT = 0;
BufferedReader systemInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please type in the DNS name or IP address of the Server machine.");
String serverMachine = systemInput.readLine();
try {
startTime = System.currentTimeMillis();
tcpSocket = new Socket(serverMachine, 5010);
socketOut = new PrintWriter(tcpSocket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
String firstResponse = socketIn.readLine();
System.out.println(firstResponse);
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("RTT of establishing this TCP connection: " + RTT + " ms");
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverMachine);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: "
+ serverMachine);
System.exit(1);
}
while (repeat.contentEquals("Yes") || repeat.contentEquals("yes")) {
String emailContent = "";
String contentLine = "";
socketOut = new PrintWriter(tcpSocket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
System.out.println("Please type in the sender's email address.");
String senderEmail = systemInput.readLine();
System.out.println("Please type in the receiver's email address.");
String receiverEmail = systemInput.readLine();
System.out.println("Please type in the subject.");
String subject = systemInput.readLine();
System.out.println("Please type in the email content (Single'.' on a line indicates end of content).");
while(true){
contentLine = systemInput.readLine();
if(contentLine.contentEquals(".")){
emailContent = emailContent + contentLine + "\r\n";
break;
}
emailContent = emailContent + contentLine + "\r\n";
}
// Step 4) a)
startTime = System.currentTimeMillis();
socketOut.println("HELO " + serverMachine);
String secondResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(secondResponse);
// Step 4) b)
startTime = System.currentTimeMillis();
socketOut.println("MAIL FROM: " + senderEmail);
String thirdResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(thirdResponse);
// Step 4) c)
startTime = System.currentTimeMillis();
socketOut.println("RCPT TO: " + receiverEmail);
String fourthResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(fourthResponse);
// Step 4) d)
startTime = System.currentTimeMillis();
socketOut.println("DATA");
String fifthResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(fifthResponse);
// Step 4) e)
startTime = System.currentTimeMillis();
socketOut.println("To: " + receiverEmail + "\r\n" + "From: " + senderEmail + "\r\n" + "Subject: " + subject + "\r\n\r\n" + emailContent);
String sixthResponse = socketIn.readLine();
endTime = System.currentTimeMillis();
RTT = endTime - startTime;
System.out.println("The RTT query: " + RTT + " ms");
System.out.println(sixthResponse);
System.out.println("Would you like to continue? Please type in either yes or no or quit.");
repeat = systemInput.readLine();
if (repeat.contentEquals("QUIT") || repeat.contentEquals("quit") || repeat.contentEquals("Quit") || repeat.contentEquals("no") || repeat.contentEquals("No")) {
socketOut.println("QUIT");
String seventhResponse = socketIn.readLine();
System.out.println(seventhResponse);
socketOut.close();
socketIn.close();
systemInput.close();
tcpSocket.close();
System.exit(0);
break;
}
else{
socketOut.println("Yes");
}
}
}
}
/*
* Server App upon SMTP
* A thread is created for each connection request from a client
* So it can handle Multiple Client Connections at the same time
* Name: Hussain Al Zerjawi
* Assignment: HW04
* Due date: 02/29/2020
*/
import java.net.*;
import java.io.*;
public class TCPMultiServer {
public static void main(String[] args) throws IOException {
ServerSocket serverTCPSocket = null;
boolean listening = true;
try {
serverTCPSocket = new ServerSocket(5010);
} catch (IOException e) {
System.err.println("Could not listen on port: 5010.");
System.exit(-1);
}
while (listening){
new TCPMultiServerThread(serverTCPSocket.accept()).start();
}
serverTCPSocket.close();
}
}
/*
* Server App upon SMTP
* A thread is started to handle every client TCP connection to this server
* Name: Hussain Al Zerjawi
* Assignment: HW04
* Due date: 02/29/2020
*/
import java.net.*;
import java.io.*;
public class TCPMultiServerThread extends Thread {
private Socket clientTCPSocket = null;
public TCPMultiServerThread(Socket socket) {
super("TCPMultiServerThread");
clientTCPSocket = socket;
}
public void run() {
try{
PrintWriter cSocketOut = new PrintWriter(clientTCPSocket.getOutputStream(), true);
BufferedReader cSocketIn = new BufferedReader(new InputStreamReader(clientTCPSocket.getInputStream()));
cSocketOut.println("220 cs3700a.msudenver.edu");
}catch (IOException e) {
e.printStackTrace();
}
outer :while(true){
try {
PrintWriter cSocketOut = new PrintWriter(clientTCPSocket.getOutputStream(), true);
BufferedReader cSocketIn = new BufferedReader(new InputStreamReader(clientTCPSocket.getInputStream()));
int line = 0;
String fromClient;
String[] request = new String[1000];
cSocketIn.mark(1000);
if(cSocketIn.readLine() == "QUIT"){
cSocketOut.println("221 <cs3700a.msudenver.edu> closing connection");
cSocketOut.close();
cSocketIn.close();
clientTCPSocket.close();
//System.exit(0);
break;
}
cSocketIn.reset();
// Part 3) a) and b)
String HELO;
while(true){
HELO = cSocketIn.readLine();
if(HELO.contains("HELO")){
System.out.println(HELO);
cSocketOut.println("250 cs3700a.msudenver.edu Hello cs3700b.msudenver.edu");
break;
}
else{
String noHELO = "503 5.5.2 Send hello first";
cSocketOut.println(noHELO);
}
}
// Part 3) c) and d)
String FROM;
while(true){
FROM = cSocketIn.readLine();
if(FROM.contains("MAIL FROM")){
System.out.println(FROM);
cSocketOut.println("250 2.1.0 Sender OK");
break;
}
else{
String noFROM = "503 5.5.2 Need mail command";
cSocketOut.println(noFROM);
}
}
// Part 3) e) and f)
String RCPT;
while(true){
RCPT = cSocketIn.readLine();
if(RCPT.contains("RCPT TO")){
System.out.println(RCPT);
cSocketOut.println("250 2.1.5 Recipient OK");
break;
}
else{
String noRCPT = "503 5.5.2 Need rcpt command";
cSocketOut.println(noRCPT);
}
}
// Part 3) g) and h)
String DATA;
while(true){
DATA = cSocketIn.readLine();
if(DATA.contains("DATA")){
System.out.println(DATA);
cSocketOut.println("354 Start mail input; end with <CRLF>.<CRLF>");
break;
}
else{
String noDATA = "503 5.5.2 Need data command";
cSocketOut.println(noDATA);
}
}
cSocketIn.mark(1000);
while ((fromClient = cSocketIn.readLine()) != null) {
if (fromClient.equals(".")) {
System.out.print("\r\n");
cSocketOut.println("250 Message received and to be delivered");
break;
} else {
System.out.println(fromClient);
request[line] = fromClient;
}
line++;
}
cSocketIn.reset();
// Part 4 and 5)
String QUIT;
while((QUIT = cSocketIn.readLine()) != null){
if(QUIT.contains("QUIT")){
cSocketOut.println("221 <cs3700a.msudenver.edu> closing connection");
cSocketOut.close();
cSocketIn.close();
clientTCPSocket.close();
break outer;
//System.exit(0);
}
else if (QUIT.contains("Yes")){
break;
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}