0

I using DataOutputStream for send/receive byte stream to a server. 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. Note: Socketinstance initialized before and send to this method as argument;

In additional I have a receive method as following:

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 :

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. I confounded, what send method doesn't throw exception when server is fail but receive method throws exception when connection is lost? Also is there way for checking connection in write method similar readFully method, for sample it throws a exception?

(Sorry if I am using the wrong terminology or grammar, I am learning english language.)

Sam
  • 6,770
  • 7
  • 50
  • 91

3 Answers3

2

the isConnected() method tells you nothing about the liveness of the connection, only that the socket was connected at one point in time.

For more details on handling broken connections, check this answer.

Community
  • 1
  • 1
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
1

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.

Rob Wagner
  • 4,391
  • 15
  • 24
1

In addition to the correct answers by jtahlborn and Recursed, your message has probably not even left the JVM yet. You are writing to a DataOutputStream wrapped around a BufferedOutputStream. Nothing will happen until you call DataOutputStream.flush().

user207421
  • 305,947
  • 44
  • 307
  • 483