hi! im new to this client/server programing using socket/serversockt i developed a little chat program using BufferedReader n PrtinWriter streams. im using readLine for reading both from keyboard n port. here is the program, its not working. plz tell me wat kinda modification need to be done here. server) import java.net.*; import java.io.*; public class Server { public static void main(String args[])throws Exception{ ServerSocket ss=new ServerSocket(2000); Socket s; BufferedReader r; BufferedReader r1; System.out.println("i am server........ waiting for client......"); s=ss.accept(); r1=new BufferedReader(new InputStreamReader(s.getInputStream())); r=new BufferedReader(new InputStreamReader(System.in)); PrintWriter p = new PrintWriter(new BufferedWriter( new OutputStreamWriter(s.getOutputStream())),true); while(true) { String s1=r.readLine(); String s2=r1.readLine(); if(s1.equals("stop")) break; System.out.println(s2); p.println(s1); }
s.close(); p.close(); } } client) import java.net.*; import java.io.*; public class Client{ public static void main(String args[])throws Exception{ BufferedReader r; BufferedReader r1; Socket s = new Socket("127.0.0.1",2000);
Hello, Your main problem in this code is that you are attempting to read from both ends of the socket at the same time. Nothing is ever written to the socket in your code.. Here's a simplistic chat program I made from your code example. This is a "CB" style chat.. In other words, only one person can chat at a time, and they must alternate turns. You will probably have to use threading to update all clients connected to the server whenever someone chats. Good luck! Cheers, RL Here is server: <pre> import java.net.*; import java.io.*; public class Server { public static void main(String args[])throws Exception{ ServerSocket ss = new ServerSocket(2000); Socket s;
System.out.println("i am server........ waiting for client......");
keyIn = keyread.readLine(); sockwrite.writeObject(keyIn);
if(keyIn.equals("stop")) break; }
s.close(); } } </pre> Here is client: <pre> import java.net.*; import java.io.*; public class Client{ public static void main(String args[])throws Exception{ Socket s = new Socket("127.0.0.1",2000); ObjectOutputStream sockwrite = new ObjectOutputStream(s.getOutputStream()); ObjectInputStream sockread = new ObjectInputStream(s.getInputStream());