java - 使用缓冲读取器和套接字

标签 java sockets

我编写了这个简单的 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);
        }
    }
}

程序运行良好,没有任何运行时错误,但当我尝试在最后一 block try block 中打印来自 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

关于java - 使用缓冲读取器和套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9455495/

相关文章:

java - JspWriter 和 PrintWriter 有什么区别?

java - findviewbyid 无法解决 android studio 中的方法错误

java - 聊天应用程序: Get messages between 2 users in Parse query

java - Android studio appcombat 操作栏设置

sockets - Nodejs中有一种 "Socket connection pool"吗?

java - JAXB - 编码器写入系统输出但不写入 XML 文件

web-services - 物联网设备如何连接到服务器

c++ - 在 C++ 中通过套接字发送 protobuf 的序列化是什么?

java - TableView内容不显示

java - 通过不同计算机上的套接字发送大文件