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!
> Just tell me why is the loop in the SERVER code runs only once
It may be related to Server repeatedly invoking accept(), which "blocks
until a connection is made."
while(true) {
s = ss.accept();
...
}
Your example is extremely hard to read. It's wrapped carelessly and
filled with extraneous code. The null layout, followed by a spurious
pack(), is particularly awkward. The components cannot adapt to changes
in the window's size. I urge you to revisit this limitation of your
design.
For reference, here's a simpler, working example:
<http://groups.google.com/group/comp.lang.java.help/msg/9f7f86db103f35c9>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
>Hey everyone, i'm trying to develop a email program with client and
You do know that you can whip together an Email program using the
JavaMail classes.
see http://mindprod.com/jgloss/javamail.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
There is no harm in being sometimes wrong especially if one is promptly found out.
~ John Maynard Keynes (born: 1883-06-05 died: 1946-04-21 at age: 62)
Why do you wrap the GUI in an extra 'Thread'?
--
Lew