簡體   English   中英

使用緩沖的讀取器和套接字

[英]Using Buffered reader and Socket

我編寫了這個簡單的Java程序,該程序連接到Internic服務器並返回域詳細信息。 但是,我面臨一個奇怪的問題。 我聽起來可能很蠢,但這是程序!

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);
        }
    }
}

該程序運行良好,沒有任何運行時錯誤,但是當我嘗試在最后一塊try塊中嘗試從Internic服務器打印結果時,它沒有顯示任何輸出。 我嘗試重新排列代碼,發現如果在創建套接字流后放置bf.readLine() ,則沒有輸出。 但是,如果將其放置在套接字創建之前(在main方法的開始處),程序將顯示預期的輸出。

是否存在流沖突? 我是Java網絡方面的新手。 解決方案可能很明顯,但我聽不懂! 請幫我!!!

將域發送到輸出流后,將輸入流的初始化移動。...這對我本地有效:

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);
        }
    }
}

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM