
I hve written a server program & a client program. The server is supposed to echo watever is typed in the client. I hve to get 16 values to be echoed. I hve created a string and all the values are added to this string. When I pass the string to the server, it doesn get echoed by the server. In fact server hangs @ tat part after establishing connection with the client. I am at my wits end!Can someone run the code for me & explain why it hangs when its supposed to echo the value in server? I hve changed my code 7 times already still I can't understand why it hangs, tis is really a desperate call, stuck with tis 4 4 days alreay.
SERVER PROGRAM:
[code] import java.io.*; import java.net.*; import java.util.*;
public class AS3{ public static void main(String[] args ){ int i = 1; try{ ServerSocket s = new ServerSocket(9001);
for (;;){
Socket incoming = s.accept( );
System.out.println("Spawning " + i);
new RealEchoHandler(incoming, i).start();
i++;
}
} catch (Exception e){ System.out.println(e); }
}
}
class RealEchoHandler extends Thread{ DataInputStream in; DataOutputStream out; private Socket incoming; private int counter;
public RealEchoHandler(Socket i, int c){
incoming = i;
counter = c;
}
public void run(){
try {
in = new DataInputStream(incoming.getInputStream());
out = new DataOutputStream(incoming.getOutputStream());
boolean done = false;
String str="";
out.writeUTF("Connected!\n");
out.flush();
while (!done){
out.writeUTF(">");
out.flush();
str = in.readUTF();
System.out.println(in+":"+str);
if (str == null)
done = true;
else{
out.writeUTF("Echo (" + counter + "): " + str+"\n");
out.flush();
}
}
incoming.close();
} catch (Exception e){
System.out.println(e);
}
}
}[/code]
CLIENT: [code] import java.io.*; import java.net.*; import java.util.*; class Client3{ public static void main(String[] args) { String store=""; String clientCar=""; String clientBranch=""; String clientDriver=""; String clientPasswd="";
DataOutputStream out;
DataInputStream in;
try {
Socket t = new Socket("127.0.0.1", 9001);
in = new DataInputStream(t.getInputStream());
out = new DataOutputStream(t.getOutputStream());
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
boolean more = true;
System.out.println(in.readUTF());
while (more) {
clientCar = in.readUTF();
clientBranch = in.readUTF();
clientDriver = in.readUTF();
clientPasswd = in.readUTF();
store = store + clientCar+clientBranch+clientDriver+clientPasswd;
out.flush();
store = in.readUTF();
if (store == null)
more = false;
else
out.writeUTF(store + "\n");
}
} catch(IOException e){
System.out.println("Error" + e);
}
System.out.println(store);
}
} [/code]
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.