Home > Enterprise >  Socket Program to get year of birth
Socket Program to get year of birth

Time:10-25

I have this program which I was working on. I am not a very intermediate when it comes to Java socket programs and networking, but I have a small mistake here, take a look:

Server Class :

import java.net.*;
import java.io.*;
import java.util.*;

public class MyEchoServer {
    public static void main(String[] args) throws Exception{
        ServerSocket ss = new ServerSocket(6807);
        
        System.out.println("MyEchoServer is Running");
        System.out.println("Waiting for the client to connect...");
        
        Socket s = ss.accept();
        
        Scanner sc = new Scanner(s.getInputStream());
        PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
        String str="";
        Integer age=1;
        while(age!=0) {
            str=sc.nextLine();
            age = Integer.parseInt(str);
        
            pw.println(2022-age);
        }
        s.close();
    }
}

Client Class:

import java.net.*;
import java.io.*;
import java.util.*;

public class MyEchoClient {
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("localhost",6246);
        
        Scanner sc = new Scanner(s.getInputStream());
        PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
        
        Scanner in = new Scanner(System.in);
        
        String str="";
        Integer age=1;
        
        while(age!=0) {
            System.out.println("Enter Age");
            str=in.nextLine();
            age = Integer.parseInt(str);
            pw.println(age);
            str=sc.nextLine();
            System.out.println(str);
        }
        System.out.println("Exiting...");
        s.close();
    }
}

The output :

Enter Age
10
2012
Enter Age
0
2022
Exiting...

The problem is, after saying 0 to exit the program, it also prints the equation to get the age.

It is probably a simple mistake that's going to be funny to decent programmers, but I have been trying to edit this for hours and couldn't come up with an idea.

The problem is probably in the while loop, but I can't get my head around it.

CodePudding user response:

The problem is, after saying 0 to exit the program, it also prints the equation to get the age.

That is because you are performing the calculation and printing the result regardless of what the input value actually is. You need to look at the input value first and then act accordingly, eg:

Server Class :

import java.net.*;
import java.io.*;
import java.util.*;

public class MyEchoServer {
    public static void main(String[] args) throws Exception{
        ServerSocket ss = new ServerSocket(6807);
        
        System.out.println("MyEchoServer is Running");
        System.out.println("Waiting for the client to connect...");
        
        Socket s = ss.accept();
        
        Scanner sc = new Scanner(s.getInputStream());
        PrintWriter pw = new PrintWriter(s.getOutputStream(), true);

        do {
            String str = sc.nextLine();
            int age = Integer.parseInt(str);

            if (age == 0) break; // <-- add this!

            pw.println(2022-age);
        }
        while (true);
        s.close();
    }
}

Client Class:

import java.net.*;
import java.io.*;
import java.util.*;

public class MyEchoClient {
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("localhost",6246);
        
        Scanner sc = new Scanner(s.getInputStream());
        PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
        
        Scanner in = new Scanner(System.in);
                
        do {
            System.out.println("Enter Age");
            String str = in.nextLine();
            int age = Integer.parseInt(str);
            pw.println(age);

            if (age == 0) break; // <-- add this!

            str = sc.nextLine();
            System.out.println(str);
        }
        while (true);
        System.out.println("Exiting...");
        s.close();
    }
}
  • Related