-1

Hello I have started learning socket programming and i have made a chat app but it is not working i don't know why, when server sends data the client print and receives it but when client sends data it did't print in the Server also the Output is also weird please help Thank Youas you can see that at server console the message is printed 2 times Why??

as you can see that at server console the message is printed 2 times Why??

code for server>>

public class Server {
public static Socket s;
public static void main(String[] args) throws IOException {
    
    System.out.println("Server has Started");
    ServerSocket ss = new ServerSocket(9999);
    
    System.out.println("Server is Waiting For Client Connection");
    s = ss.accept();
    
    System.out.println("Client connected");
    
    Runnable send = new sender();
    Runnable recieve = new sender();
    Thread sends = new Thread(send);
    Thread recieves = new Thread(recieve);
    sends.start();
    recieves.start();
    
    
    

    }
}

public class reciever implements Runnable{

@Override
public void run() {
    
    while(true) {
        try {
            
            BufferedReader br = new BufferedReader(new InputStreamReader(Server.s.getInputStream()));
            String message = br.readLine();
            
            System.out.println("Client:" + message);
        
        
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}

}

public class sender implements Runnable{

@Override
public void run() {
    
    
    while(true) {
    
    try {
        Scanner scan = new Scanner(System.in);
        System.out.print("Message: ");
        String message = scan.nextLine();   
        
        OutputStreamWriter os = new OutputStreamWriter(Server.s.getOutputStream());
        PrintWriter out = new PrintWriter(os);
        out.println(message);
        os.flush();
    
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    
    }
}

}

Client Code>>

public class Client {

public static Socket s ;

public static void main(String[] args) throws Exception {
    String ip = "localhost";
    int port = 9999;
    s = new Socket(ip, port);
    
    Runnable send = new sender();
    Runnable recieve = new reciever();
    Thread sends = new Thread(send);
    Thread recieves = new Thread(recieve);
    sends.start();
    recieves.start();
}

}

public class reciever implements Runnable{

@Override
public void run() {
    while(true) {
    try {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(Client.s.getInputStream()));
        String message = br.readLine();
        
        System.out.println("Server:" + message);
    
    
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}

}

public class sender implements Runnable {

@Override
public void run() {
    
    while(true) {
        
        try {
            Scanner scan = new Scanner(System.in);
            
            System.out.print("Message: ");
            String message = scan.nextLine();
            
            OutputStreamWriter os = new OutputStreamWriter(Client.s.getOutputStream());
            
            PrintWriter out = new PrintWriter(os);
            out.println(message);
            os.flush();
        
        
        
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
            
        
    }
    
}

}

1 Answer 1

1

In Server code change

Runnable recieve = new sender();

to

Runnable recieve = new reciever();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.