public class twowaychat {
ServerSocket server;
Socket socket;
BufferedReader read;
BufferedWriter write;
String temp, temp1;
twowaychat() {
Thread thread1 = new Thread("Read-Client") {
public void run() {
try {
do {
temp = read.readLine();
System.out.print(temp);
} while (!(temp.equals("Exit")));
close();
} catch (Exception e) {
System.out.print("Line 30 error :" + e.toString());
}
}
};
Thread thread2 = new Thread("Write-Client") {
public void run() {
try {
Scanner scan = new Scanner(System.in);
do {
temp1 = scan.next();
write.write(temp1);
} while (!(temp.equals("Exit")));
close();
} catch (Exception e) {
System.out.print("Line 53 error :" + e.toString());
}
}
};
try {
server = new ServerSocket(4368);
socket = server.accept();
System.out.println("Connected ");
read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
thread1.start();
thread2.start();
} catch (Exception e) {
System.out.println("Line 72 error :" + e.toString());
}
}
protected void close() {
try {
System.out.print("Closing Connection");
read.close();
write.close();
socket.close();
} catch (Exception e) {
System.out.print(e.toString());
}
}
public static void main(String args[]) {
twowaychat obj = new twowaychat();
}
}
I want to build a chatting program. So client & server can chat. I have used two threads in both client & server to read & write from & to streams respectively. But this program is not working at all just prints "Connected". My client program also follows the same structure as the above server program.
I want a program where client & server can keep sending & receiving messages till Exit is entered.