简体   繁体   English

DataOutputStream.write(byte b [],int off,int len)不会引发异常

[英]DataOutputStream.write(byte b[], int off, int len) don't throws exception

I using DataOutputStream for send/receive byte stream to a server. 我使用DataOutputStream向服务器发送/接收字节流。 First I discuss my important code parts then ask my question. 首先,我讨论重要的代码部分,然后提出问题。

I have a send method as following: 我有一个发送方法如下:

protected static void sendMessage (Socket socket, byte[] b)  throws IOException
{
   BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream(), 2048);
   DataOutputStream serverOut = new DataOutputStream(bos);

   serverOut.write(b, 0, b.length);
   serverOut.flush();
}

I using above method for send message to server by DataOutputStream instance. 我使用上述方法通过DataOutputStream实例将消息发送到服务器。 Note: Socket instance initialized before and send to this method as argument; 注意: Socket实例之前已初始化,并作为参数发送给此方法;

In additional I have a receive method as following: 另外,我有如下的receive方法:

protected static void receive (Socket socket, byte[] b) throws IOException
{ 
   BufferedInputStream bis = new BufferedInputStream (socket.getInputStream ());
   DataInputStream serverIn = new DataInputStream (bis);

   serverIn.readFully(b, 0, b.length);
}

In last step I show main method : 在最后一步中,我展示了main方法:

public static void main(String[] args)
{
    ServerSocketFactory socket_factory = ServerSocketFactory.getDefault();
    InetAddress host =  InetAddress.getByName("192.168.20.33");
    ServerSocket server_socket = socket_factory.createServerSocket(1111,3,host);
    Socket socket = server_socket.accept();
    System.out.println(socket.isConnected());

    byte[] request = new byte[]{48,48,48};
    send(socket, request);

    byte[] response = new byte[10];
    receive(socket,response);
}

In normal situation all thing do fine but my question is: When server down after test connection by socket.isConnected() statement send method work fine and any exception don't throw but when invoking receive method , it throws a exception for unconnected host. 在正常情况下,一切正常,但我的问题是:通过socket.isConnected()语句测试连接后服务器停机时, send方法可以正常工作,并且不会抛出任何异常,但是在调用receive方法时,它将为未连接的主机抛出异常。 I confounded, what send method doesn't throw exception when server is fail but receive method throws exception when connection is lost? 我很困惑,当服务器发生故障时,哪种send方法不会引发异常,而当连接丢失时,哪种send方法receive引发异常? Also is there way for checking connection in write method similar readFully method, for sample it throws a exception? 还有没有办法在类似于readFully方法的write方法中检查连接,例如抛出异常?

(Sorry if I am using the wrong terminology or grammar, I am learning english language.) (很抱歉,如果我使用了错误的术语或语法,那么我正在学习英语。)

the isConnected() method tells you nothing about the liveness of the connection, only that the socket was connected at one point in time. isConnected()方法没有告诉您连接的活跃性,仅告诉您套接字在某个时间点连接的。

For more details on handling broken connections, check this answer . 有关处理断开的连接的更多详细信息,请查看此答案

When you send a message out, your software has no knowledge of whether or not the destination received your message. 当您发送消息时,您的软件不知道目的地是否收到了您的消息。 You could send your message to any location and you would never get an error or exception. 您可以将消息发送到任何位置,并且永远不会出现错误或异常。 However, when you receive a message your software is fully aware that it is expecting a message, and will alert you when it does not receive a message as expected. 但是,当您收到一条消息时,您的软件就会完全意识到它正在收到一条消息,并且在未收到预期的消息时会提醒您。

If you would like to make sure that there is something on the other end before you send a message, you could modify your software by sending out a ping to the destination and make sure something is connected on the other end before sending. 如果要在发送消息之前确保另一端有东西,则可以通过向目标发送ping并在发送前确保另一端已连接东西来修改软件。

In addition to the correct answers by jtahlborn and Recursed, your message has probably not even left the JVM yet. 除了jtahlborn和Recursed的正确答案之外,您的消息可能甚至还没有离开JVM。 You are writing to a DataOutputStream wrapped around a BufferedOutputStream. 您正在写包裹在BufferedOutputStream.周围的DataOutputStream BufferedOutputStream. Nothing will happen until you call DataOutputStream.flush(). 除非您调用DataOutputStream.flush().否则什么都不会发生DataOutputStream.flush().

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataOutputStream.write(int b)不写 - DataOutputStream.write(int b) doesn't write readFully(byte [] b,int off,int len)和EOFException - readFully(byte[] b, int off, int len) and EOFException BufferedInputStream.read(byte [] b,int off,int len)中的off参数 - The off parameter in BufferedInputStream.read(byte[] b, int off, int len) JAVA:InputStream.read(byte [] b,int off,int len)中的字节数组分配 - JAVA: Byte array allocation in InputStream.read(byte[] b, int off, int len) InputStream read(byte [] b,int off,int len)-删除文件的其余部分? - InputStream read(byte[] b, int off, int len) - Drop the rest of the file? read(byte [] b,int off,int len):读取文件到最后 - read(byte[] b, int off, int len) : Reading A File To Its End Can BufferedInputStream.read(byte [] b,int off,int len)是否会返回0? 是否有重要的,破坏的InputStream可能会导致这种情况? - Can BufferedInputStream.read(byte[] b, int off, int len) ever return 0? Are there significant, broken InputStreams that might cause this? 使用DataOutputStream将int写入文件 - Write int to file using DataOutputStream Java-DataOutputStream writeLong \\ Byte \\ Double \\ Int Speed - Java - DataOutputStream writeLong\Byte\Double\Int Speed RandomAccessFile writeInt(int i)vs write(byte [] b)-性能 - RandomAccessFile writeInt(int i) vs write(byte[] b) - performance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM