how to make two way communication

I am developing chat program.Here i can't make two way communication. Please see me code then tell me the pblm.

Server Program

-

public static void main(String[] args) throws IOException {

ServerSocket s = new ServerSocket(PORT);

System.out.println("Started: " + s);

try {

Socket socket = s.accept();

try {

System.out.println( "Connection accepted: "+ socket);

BufferedReader in =new BufferedReader new InputStreamReader(socket.getInputStream()));

PrintWriter out =new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);

while (true) {

String str = in.readLine();

if(str!="") {

if (str.equals("END")) break;

System.out.println("Echoing: " + str);

out.println(str);

}

}

} finally {

System.out.println("closing...");

socket.close();

}

} finally {

s.close();

}

}

Client Program

--

public class JabberClient extends Thread {

public Socket socket=null;

public JabberClient(InetAddress strAddr,int intPort){

try {

socket =new Socket(strAddr,intPort);

}catch(Exception ie){

ie.printStackTrace();

}

}

public void run(){

while(true) {

try {

System.out.println("socket = " + socket);

System.out.println(socket.getInetAddress().getHostAddress());

BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

String str = in.readLine();

System.out.println(str);

}

catch(Exception ie){

ie.printStackTrace();

} finally {

try {

System.out.println("closing...");

socket.close();

}catch(Exception ie){

ie.printStackTrace();

}

}

}

}

public static void main(String[] args) throws IOException {

InetAddress addr = InetAddress.getByName(null);

System.out.println("addr = " + addr);

JabberClient obj =new JabberClient(addr,JabberServer.PORT);

obj.start();

}

}

[2234 byte] By [Mani_csa] at [2007-10-3 3:04:42]
# 1
> tell me the pblmTell us the problem.You should format the code.See: http://forum.java.sun.com/help.jspa?sec=formatting
hiwaa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 2
now i enter any data in server terminal.It's shows in client terminal.Similar to enter any data in client terminal it's shows in server terminal.Connection is created.But i can't read message between server and client
Mani_csa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 3

Server Program-

public static void main(String[] args) throws IOException {

ServerSocket s = new ServerSocket(PORT);

System.out.println("Started: " + s);

try {

Socket socket = s.accept();

try {

System.out.println( "Connection accepted: "+ socket);

BufferedReader in =new BufferedReader new InputStreamReader(socket.getInputStream()));

PrintWriter out =new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);

while (true) {

String str = in.readLine();

if(str!="") {

if (str.equals("END")) break;

System.out.println("Echoing: " + str);

out.println(str);

}

}

} finally {

System.out.println("closing...");

socket.close();

}

} finally {

s.close();

}

}

Client Program

--

public class JabberClient extends Thread {

public Socket socket=null;

public JabberClient(InetAddress strAddr,int intPort){

try {

socket =new Socket(strAddr,intPort);

}catch(Exception ie){

ie.printStackTrace();

}

}

public void run(){

while(true) {

try {

System.out.println("socket = " + socket);

System.out.println(socket.getInetAddress().getHostAddress());

BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

String str = in.readLine();

System.out.println(str);

}

catch(Exception ie){

ie.printStackTrace();

} finally {

try {

System.out.println("closing...");

socket.close();

}catch(Exception ie){

ie.printStackTrace();

}

}

}

}

public static void main(String[] args) throws IOException {

InetAddress addr = InetAddress.getByName(null);

System.out.println("addr = " + addr);

JabberClient obj =new JabberClient(addr,JabberServer.PORT);

obj.start();

}

}

Mani_csa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 4
if i am set any value in out object of the server program.It won't show in the client program
Mani_csa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 5

This part should be in a loop:

Socket socket = s.accept();

handleClient(socket);

This part should be put in the handleClient() method, in which you create a new thread for each accepted client and start the thread:

///// put them in the per-client thread /////

BufferedReader in =new BufferedReader new InputStreamReader(socket.getInputStream()));

PrintWriter out =new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);

while (true) {

String str = in.readLine();

if(str!="") {

if (str.equals("END")) break;

System.out.println("Echoing: " + str);

out.println(str);

Your client program also should use loop for basic I/O.

hiwaa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> This part should be in a loop:

> >Socket socket = s.accept();

> handleClient(socket);

>

>

> This part should be put in the handleClient() method,

> in which you create a new thread for each accepted

> client and start the thread:

> [code]

>///// put them in the per-client thread /////

> BufferedReader in =new BufferedReader new

> InputStreamReader(socket.getInputStream()));

> PrintWriter out =new PrintWriter(new

> BufferedWriter( new

> OutputStreamWriter(socket.getOutputStream())),true);

> while (true) {

>String str = in.readLine();

> if(str!="") {

>if (str.equals("END")) break;

> System.out.println("Echoing: " + str);

>out.println(str);

> e]

>

> Your client program also should use loop for basic

> I/O.

Okey!.if am using multiple clients i can put in thread.Here Out is reference of client terminal.So if i write any thing in Out.println method.it will show in the client terminal.But that is not working

Mani_csa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 7

> Okey!.if am using multiple clients i can put in thread.Here Out is reference of client

> terminal.So if i write any thing in Out.println method.it will show in the client terminal.But that

> is not working

Your current program is 100% wrong.

Server should run a thread dispatching loop.

In the thread, you should run an I/O loop using client socket's stream.

Client should run a continuous I/O loop.

hiwaa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 8

Hi mani try this:

public void run() {

String msg;

try{

while((msg = reader.readLine()) != null){

System.out.println("client:"+msg);

incoming.append(msg+"\n");

}//while close

}catch(Exception e){e.printStackTrace();}

}//run close

Here incoming is the text area of the client, where he can see the msgs from other clients

Prathima13a at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...
# 9
Okey i wil try.
Mani_csa at 2007-7-14 20:54:44 > top of Java-index,Java Essentials,Java Programming...