Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:38:45+00:00 2026-05-30T22:38:45+00:00

I wrote this simple Java program which connects to internic server and returns the

  • 0

I wrote this simple Java program which connects to internic server and returns the domain details. However, I am facing a strange problem. I may sound dumb but here is the program!

import java.io.*;
import java.net.*;
public class SocketTest {
    public static void main(String[] args) {
        String hostName;
        int i = 0;

        try {                  
            Socket socketClient = new Socket("whois.internic.net", 43);
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            InputStream in = socketClient.getInputStream();
            OutputStream out = socketClient.getOutputStream();
            System.out.println("Please Enter the Host Name!!");
            hostName = bf.readLine();      
            hostName = hostName + "\n";
            byte[] buf = hostName.getBytes();
            out.write(buf);

            while((i = in.read()) != -1) {
                System.out.print((char)i);
            }

            socketClient.close();
        } catch(UnknownHostException uht) {
            System.out.println("Host Error");
        } catch(IOException ioe) {
            System.out.println("IO Error " + ioe);
        } catch(Exception e) {
            System.out.println("Exception " + e);
        }
    }
}

The program runs fine, without any runtime errors, but it shows no output when I try to print the result from internic server in the last piece of try block. I tried rearranging the code and found that if I place the bf.readLine() after creating socket streams, there is no output. However, if I place it before the socket creation (at the start of main method), the program displays intended output.

Is there any stream conflict or so? I am a newbie to networking in Java. The solution may be obvious but I am not able to understand! Please help me!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:38:46+00:00Added an answer on May 30, 2026 at 10:38 pm

    Move your input stream initialization after you send the domain to the output stream… This works for me locally:

    import java.io.*;
    import java.net.*;
    
    public class SocketTest {
        public static void main(String[] args) {
            String hostName;
            int i = 0;
            try {
                Socket socketClient = new Socket("whois.internic.net", 43);
                BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    
                OutputStream out = socketClient.getOutputStream();
                System.out.println("Please Enter the Host Name!!");
                hostName = bf.readLine();
                hostName = hostName + "\n";
                byte[] buf = hostName.getBytes();
                out.write(buf);
    
                InputStream in = socketClient.getInputStream();
                while ((i = in.read()) != -1) {
                    System.out.print((char) i);
                }
                in.close();
                out.close();
                socketClient.close();
    
            } catch (UnknownHostException uht) {
                System.out.println("Host Error");
            } catch (IOException ioe) {
                System.out.println("IO Error " + ioe);
            } catch (Exception e) {
                System.out.println("Exception " + e);
            }
        }
    }
    

    Output:

    Please Enter the Host Name!!
    yahoo.com
    
    Whois Server Version 2.0
    
    Domain names in the .com and .net domains can now be registered
    with many different competing registrars. Go to http://www.internic.net
    for detailed information.
    
    YAHOO.COM.ZZZZZZZ.GET.ONE.MILLION.DOLLARS.AT.WWW.UNIMUNDI.COM
    YAHOO.COM.ZZZZZZ.MORE.INFO.AT.WWW.BEYONDWHOIS.COM
    ....Whole bunch more
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple program with java swing which suppose to start another thread
A couple of month ago I wrote a simple program in Java. I have
I wrote this simple C code and compiled it using Visual Studio 2010, with
So I wrote this simple console app to aid in my question asking. What
I've wrote this simple piece of code. And I have a slight problem with
I'm trying to retrieve folders name from a zip file. I wrote this simple
This is a very simple bash script I wrote: #!/bin/bash ITEM_LIST=items.txt LOG_FILE=log.log TOTAL_ITEMS=$(wc -l
I wrote a simple hello world program to test the JDK installation. I can
I once wrote a simple 'crawler' to download http pages for me in JAVA.
I'd like to port a Linux C program to Java. This program controls a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.