Problem with Email Program in TCP
Hey everyone, i'm trying to develop a email program with client and
server. The client will send the message containing the details:
1. To whom it should be send
2. Subject
3. Message Part
This message will go to server which in turn will redirect it to the
expected destination(the one contained in the "To" part of the
message)
I have written the code for both client and server which are as
follows:
CLIENT code(I have written it in NetBeans):
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class Client extends javax.swing.JFrame implements Runnable {
/** Creates new form Client */
public Client() {
initComponents();
}
public void run(){
ServerSocket ssoc;
Socket ss,sen;
try {
ss = new Socket(InetAddress.getByName("Invictus-PC"),
5100);
output = new ObjectOutputStream(ss.getOutputStream());
output.flush();
ssoc = new ServerSocket(5000);
while(true){
sen = ssoc.accept();
input = new ObjectInputStream(sen.getInputStream());
String s = (String) input.readObject();
int i = s.indexOf(",",0);
int j = s.indexOf(",",i+1);
int k = s.indexOf(",",j+1);
String s1 = s.substring(0,i);
String s2 = s.substring(i+1,j);
String s3 = s.substring(j+1);
jTextArea2.append("New Message Recieved and the Details
are as under:\n");
jTextArea2.append("Message sent by: "+s1+"\n");
jTextArea2.append("Message subject is: "+s2+"\n");
jTextArea2.append("Actual message is: "+s3+"\n \n \n
\n");
jTextArea2.setCaretPosition(jTextArea2.getText().length());
sen.close();
input.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated
Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().add(jTextField1);
jTextField1.setBounds(60, 30, 260, 50);
getContentPane().add(jTextField2);
jTextField2.setBounds(60, 100, 260, 40);
getContentPane().add(jTextField3);
jTextField3.setBounds(60, 160, 260, 140);
jButton1.setText("Send");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(60, 340, 100, 30);
jButton2.setText("Reset");
jButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(220, 340, 100, 30);
jLabel1.setText("To:");
getContentPane().add(jLabel1);
jLabel1.setBounds(20, 20, 40, 50);
jLabel2.setText("Subject:");
getContentPane().add(jLabel2);
jLabel2.setBounds(10, 100, 50, 40);
jLabel3.setText("Message:");
getContentPane().add(jLabel3);
jLabel3.setBounds(10, 150, 46, 40);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(500, 30, 380, 150);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
getContentPane().add(jScrollPane2);
jScrollPane2.setBounds(500, 230, 380, 170);
jLabel4.setText("Sending Message Details:");
getContentPane().add(jLabel4);
jLabel4.setBounds(360, 30, 130, 150);
jLabel5.setText("Recieving Message Details:");
getContentPane().add(jLabel5);
jLabel5.setBounds(360, 230, 130, 170);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent
evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
try {
if( (jTextField1.getText().equalsIgnoreCase("")) ||
(jTextField1.getText().equalsIgnoreCase("")) ||
(jTextField1.getText().equalsIgnoreCase("")) )
{
JOptionPane.showMessageDialog(this,"Please fill out the
details properly");
}
else{
String s = new String();
s = jTextField1.getText()+","+jTextField2.getText()
+","+jTextField3.getText();
output.writeObject(s);
jTextArea1.append("Message is being sent and the details are as
under:\n");
jTextArea1.append("Message sent to server to be send to :
"+jTextField1.getText()+"\n");
jTextArea1.append("Message subject is: "+jTextField2.getText()
+"\n");
jTextArea1.append("Actual Message is: "+jTextField3.getText()
+"\n");
jTextArea1.append("WAITING FOR REPLY........\n \n \n \n ");
jTextArea1.setCaretPosition(jTextArea1.getText().length());
}}
catch(Exception e){}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Client u = new Client();
u.setVisible(true);
Thread t = new Thread(u);
t.start();
}
});
}
ObjectOutputStream output;
ObjectInputStream input;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}
SERVER Code:
import java.net.*;
import java.io.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Server extends javax.swing.JFrame implements Runnable {
/** Creates new form Server */
public Server() {
initComponents();
}
public void run(){
ServerSocket ss;
Socket s;
try {
ss = new ServerSocket(5100,10);
while(true){
s = ss.accept();
input = new ObjectInputStream(s.getInputStream());
String str = (String) input.readObject();
int i = str.indexOf(",",0);
int j = str.indexOf(",",i+1);
int k = str.indexOf(",",j+1);
String s1 = str.substring(0,i);
String s2 = str.substring(i+1,j);
String s3 = str.substring(j+1);
jTextArea1.append("Message Recieved\n");
jTextArea1.append("Details are as under:\n");
jTextArea1.append("Message sent by: "+s.getInetAddress()
+"\n");
jTextArea1.append("Message sent to: "+s1+"\n");
jTextArea1.append("Message subject is: "+s2+"\n");
jTextArea1.append("Actual Message is :"+s3+"\n");
jTextArea1.append("Sending Message from:
"+s.getInetAddress()+" to: "+s1+"\n");
jTextArea1.setCaretPosition(jTextArea1.getText().length());
Socket another = new Socket(InetAddress.getByName(s1),
5000);
ObjectOutputStream o1 = new
ObjectOutputStream(another.getOutputStream());
String s4 = s.getInetAddress()+","+s2+","+s3;
o1.writeObject(s4);
jTextArea1.append("Message Send: "+s4+"\n");
s.close();
another.close();
input.close();
o1.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method
is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated
Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1,
java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Server u = new Server();
u.setVisible(true);
Thread t = new Thread(u);
t.start();
}
});
}
ObjectOutputStream output;
ObjectInputStream input;
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
I tried testing it on local machine,
i run server and then client,
after than i filled out the details in the client form, mentioning the
localhost in the to field,
For the first time,Client sends succesfully, Server also recieves
Successfully and forwards it to the expected destination(in this case
the localmachine) Successfully, and is recieved successfully at the
client(which is reflected in the recieved message details textarea)
But then when i do it for second time,Client sends succesfully, Server
DOESNOT recieve it, and hence do not forward it, and hence message not
received at the destination.
THE PROBLEM is that the loop in the SERVER CODE runs only once(I don't
know why is this so, im really frustated).
Please help me pointing out where am i going wrong.
Just tell me why is the loop in the SERVER code runs only once(for the
first time), I think that is the CORE PROBLEM........
PLEASE HELP!