java - why doesn't my client read correctly the outputStream sent by the Server? -


it's simple client-server server using bufferedwriter object send client receiving in object bufferedreader.

when use outputstream , printwriter server , inputstream , scanner client works well.

what happens client in buffered way reads -1 if i'm sending int , null string.

i hope english makes sense. ;p

that's code:

server:

import java.io.*; import java.net.*;  public class server {      public static void main(string[] args) throws ioexception {          serversocket server = new serversocket(8189);         socket incoming;          incoming = server.accept();         try {             // outputstream output = incoming.getoutputstream();             // printwriter outstream = new printwriter(output, true /*autoflush*/);             // outstream.println("enter");             bufferedwriter output = new bufferedwriter(new                                                outputstreamwriter(incoming.getoutputstream()));             output.write(3);             system.out.println("\nsent");          } {             incoming.close();         }     } } 

client:

import java.io.*; import java.net.*; import java.util.scanner;  public class client {      public static void main(string[] args) throws unknownhostexception, ioexception {          //client theclient= new client();         socket rtspsocket;         int serverport = 8189;         //server name address         string serverhost = "localhost";          //get server ip address         inetaddress serveripaddress = inetaddress.getbyname(serverhost);          rtspsocket = new socket(serveripaddress, serverport);          try {             /*             inputstream input = theclient.rtspsocket.getinputstream();             scanner in = new scanner(input);             string line = in.nextline();             system.out.println(line);             */              bufferedreader input = new bufferedreader(new                                      inputstreamreader(rtspsocket.getinputstream()));              //string line = input.readline();             //system.out.println("\nricevuto: " + line);             system.out.println(input.read());         } catch (exception e) {             system.err.println("error: " + e);         }     } } 

you have flush data receive them in client part.

        output.write(3);         output.flush();         system.out.println("\nsent"); 

when have outputstream (or writer) have flush data, way you're 100% sure wanted send has been sent.

most (if not all) outputstream subclasses use "mini buffer" flushed when it's full or manually call flush(). in case, it's more flagrant because you're using bufferedwriter.

another thing, when use streams/writers, should close them after you're finished, 1 of main thing close() (most of time) flushing last data remaining in "mini buffer".


resources :


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -