0

With the code below, I'm not getting "daSHI" printed onto the console. Instead im getting "?????" printed.

To test, instead of using the socket's input/output streams, I created a FileInputStream and FileOutputStream writing/reading from the same file and "daSHI" was printed just fine.

However, when I use sock.getOutputStream and sock.getInputStream, it doesn't work for some reason. All I'm getting are question marks. Any ideas? Encoding Problems?

public void sendMessage(Socket sock) throws KVException {

    OutputStream outStream;
    InputStream inStream;

    try{

        byte[] b = {'d', 'a', 'S', 'H', 'I'};
        outStream = sock.getOutputStream();

        outStream.write(b);
        sock.shutdownOutput();



        inStream = sock.getInputStream();

        for (int i = 0; i < b.length; i++) {
            System.out.print("" + (char) inStream.read());
         }

    }
    catch(IOException e){
        KVMessage errorMessage = new KVMessage("resp", "Network Error: Could not send data");
        throw new KVException(errorMessage);
    }
}
Jonathan
  • 1,469
  • 5
  • 13
  • 22
  • Is this a loopback socket connection? You haven't shown how the socket is connected to the other end. What's at the other end echoing the data? – Jim Garrison Nov 22 '13 at 23:56
  • Random guess: The input stream is at EOF, and that is just 5 times -1. Omit (char) to verify. Why? Depends on your server... Did you try adding outStream.flush() and closing the socket only after you have read data from input? – Stefan Haustein Nov 22 '13 at 23:57
  • Your `socket` InputStream is not the same as `OutputStream`. So, why d you expect the data to echo back? – Germann Arlington Nov 22 '13 at 23:58
  • I created a socket through 'new Socket("localhost", 'portnumber') How would I be able to send things through the inputstream and for some receiver to get that input then? Apparently, im thinking about this the wrong way. – Jonathan Nov 23 '13 at 00:08
  • @StefanHaustein Like add .flush() after the .write() and .shutdownOutput() after the reading? When I do this, it seems like it just hangs. I read on [here](http://stackoverflow.com/questions/12189338/java-socket-inputstream-hangs-blocks-on-both-client-and-server) that the socket keeps reading until EOF, which I won't get until I close the socket. Therefore, I have to close it before reading? – Jonathan Nov 23 '13 at 00:16
  • @GermannArlington Misunderstood Sockets/Input&Output Streams in general. – Jonathan Nov 23 '13 at 00:34
  • Look up echo port number - http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml - and connect to that port (echo service is active by default). Generally, always remember that for TCP connection to work it needs `ServerSocket` on one end and `Socket` on the other end. – Germann Arlington Nov 23 '13 at 11:34

1 Answers1

0

Unless you're connected to an echo server or another copy of the same application, sockets don't work that way: you can't expect to read what you wrote because what you're reading is what the far end of the socket is sending.

Most likely, the cause here is that the input stream has hit EOF and read() is returning -1. -1 becomes a zigamorph when cast to char, and printing this invalid character produces a ?.

Jeffrey Hantin
  • 35,734
  • 7
  • 75
  • 94