Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to make client-server chat the code which i make it only once sending message to server but not from server to client

Client code
Java
public class Chat_Client {

    public static void client ()
        {
            try
            {
            Socket cs = new Socket("localhost", 9);
            PrintStream cp = new PrintStream(cs.getOutputStream());
            System.out.print("Client: ");
            InputStreamReader cir = new InputStreamReader(System.in);
            BufferedReader cbr = new BufferedReader(cir);
            String temp1 = cbr.readLine();
            cp.println(temp1);
            BufferedReader cbr1 = new BufferedReader(new InputStreamReader(cs.getInputStream()));
            String temp2 = cbr1.readLine();
            System.out.print(temp2);
                        
            }
            catch(Exception e)
            {
            
            }
       }
    public static void main(String[] args) {
        
       client();
      }



----------------------------------------------------------------------------------------------
server code
Java
public class Chat_server1 {
    public static void server()
    {
        try
        {
            ServerSocket ss = new ServerSocket(9);
            Socket cs = ss.accept();
            BufferedReader cbr = new BufferedReader(new InputStreamReader(cs.getInputStream()));
            String temp = cbr.readLine();
            System.out.print("Client :" +temp);
            //JOptionPane.showMessageDialog(null,"Salam");
            PrintStream spr = new PrintStream(cs.getOutputStream());
            String temp1 = "Server: I got your message ";
            spr.println(temp1);
            //server();
        }
        catch(Exception e)
        {
            
        }
    }
    public static void main(String[] args) {
        // TODO code application logic here
         server();
    }
Posted
Updated 10-Jun-14 21:09pm
v2
Comments
TorstenH. 11-Jun-14 3:10am    
ok - so what is your question now?
kami124 11-Jun-14 5:28am    
i want two sided communication
Xiao Ling 13-Jun-14 5:50am    
No matter on server or client side, you have to create two threads for sending and receiving data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900