chinmay choudhary
chinmay choudhary

Reputation: 63

Java Client program is successfully connecting to server but it not passing any data

Server Code:

public class StackCalculator implements Runnable {

static Stack<Integer> numbers=new Stack<Integer>(); 
Socket currentSocket;
Scanner s;
PrintWriter pw;
public static void main(String[] args){
 try{
     ServerSocket guard=new ServerSocket(6600);
     StackCalculator SC=new StackCalculator();
    while(true){
         SC.currentSocket=guard.accept();
         System.out.println("Client connected");
         Thread t=new Thread(SC);
         t.start();
     }


 }catch(Exception e){
    e.printStackTrace();
 }
 }
public void run(){

try{
try{
 s=new Scanner(currentSocket.getInputStream());
 pw=new PrintWriter(currentSocket.getOutputStream());
doService();

}finally{
    currentSocket.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void doService(){

 if(s.hasNext()==false){
     return;
 }
 String request=s.next();
    System.out.println("Request received:"+request);
 PerformCalculation(request);
 }
 public void PerformCalculation(String request){
 if(request.equals("PUSH")){
   try{
    String num=s.next();
     int n=Integer.parseInt(num);
     StackCalculator.numbers.push(n);
     pw.println("OK");
  }catch(NumberFormatException nf){
     pw.println("ERROR");
  }
 }else if(request.equals("ADD")){
     if(StackCalculator.numbers.size()<2){
         pw.println("TOO_FEW_ELEMENTS");
     }else{
         int n1=StackCalculator.numbers.pop();
         int n2=StackCalculator.numbers.pop();
         int ns=n1+n2;
         pw.println("RESULT "+ns);
     }
 }else if(request.equals("MULT")){
    if(StackCalculator.numbers.size()<2){
        pw.println("TOO_FEW_ELEMENTS");
    }else{
        int n1=StackCalculator.numbers.pop();
         int n2=StackCalculator.numbers.pop();
         int ns=n1*n2;
        pw.println("RESULT "+ns);
     }
  }else if(request.equals("QUIT")){
    pw.close();
    s.close();

}else{
    System.out.println("Unknown Command");
}
}

}

Client COde:

      public static void main(String[] args) {

     try{
    Socket sc=new Socket("localhost",6600);
    Scanner s=new Scanner(sc.getInputStream());
    PrintWriter pw=new PrintWriter(sc.getOutputStream());
    pw.print("PUSH 11");
    System.out.println("response created");
    String response=s.nextLine();

    System.out.println(response);
    pw.close();
    sc.close();
  }catch(Exception e){
    e.printStackTrace();
  }
 }

It it is not passing the data, neither is it is showing any error nor the program is terminating on itself. I left the codes running for more than an hour but no progress is shown. Could it be in a situation of deadlock or could it be the some problem with input/output streams.

Upvotes: 0

Views: 48

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35106

PrintWriter.print does not autoflush, so you're never sending anything. You should either use println, or call flush() after print(...)

Upvotes: 2

Related Questions