0

I am making app which transfers data using wifi between two devices using hotspot server and client connection.

I am using following code to send data from Client to Server.

OutputStream stream = socket.getOutputStream();

                    stream.write("".getBytes());
                    ContentResolver cr = context.getContentResolver();


                    InputStream is = null;
                    try {
                        is = new ByteArrayInputStream(data.getBytes());
                    } catch (Exception e) {
                        Log.d("TEST", e.toString());
                    }
                    copyFile(is, stream);

The is my copy file method.

  public static boolean copyFile(InputStream inputStream, OutputStream out) {
            byte[] buf = new byte[1024];
            while (true) {
                try {
                    int len = inputStream.read(buf);
                    if (len != -1) {
                        out.write(buf, 0, len);
                    } else {

                        //out.flush();
//out.close();


                        return true;
                    }
                } catch (IOException e) {
                    Log.d("TEST","faaaaaaaaaaaaaaaa::::"+ e.toString());
                    return false;
                }
            }
        }

And, This is my Client side code.

try {

                AppFonts.serverSocket = new ServerSocket(8988);
                Log.d("TEST", "Server: Socket opened0000000000");
                AppFonts.socket = AppFonts.serverSocket.accept();

            Log.d("TEST", "Server: connection done0000000000000000");
            //    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());


            String gotData = convertStreamToString(AppFonts.socket.getInputStream());
            Log.d("TEST", "Server: Socket Files,,,,,,");
            Log.d("TEST", "server: copying files0000000000 " + gotData);

            return gotData;
        } catch (IOException e) {
            Log.e("TEST", "fsat , " + e.getMessage());
            e.printStackTrace();
            return null;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;

        }

In copyFile() method i have tried out.flush() but data is not received on client side.

--

  • I don't want to close outputstream because I want more communication between two devices.
  • when I use out.close(); data sent to client successfully, but without using that data does not reach to client.
  • Server is sending data to client from copyFile() method. Data received from client side exactly what is being sent when I use outputstream.close(); but nothing happens when I remove outputstream.close(); or/and use outputstream.flush();. I tried flush() method after seeing Android: Socket is closed this link.
  • The title of your question has nothing to do with your problem. You say the data is not received on the client side, but the server doesn't send *any* data. Also, you don't say what *is* received by the server. – President James K. Polk Sep 21 '17 at 02:55
  • Server is sending data to client from copyFile() method. Data received from client side exactly what is being sent when I use `outputstream.close();` but nothing happens when I remove `outputstream.close();` or/and use `outputstream.flush();`. I tried flush() method after seeing https://stackoverflow.com/questions/14478450/android-socket-is-closed this link. – Yadav Uttam Sep 21 '17 at 05:07
  • "I am using following code to send data from Client to Server." So that's the client code, right? And the server side code is the side with the server socket, the one you *said* was the client side, right? Maybe you should edit your question to clarify it. – President James K. Polk Sep 21 '17 at 12:19

1 Answers1

1

In order to have a continuously streaming the data, you could do something like this

Timer scheduler = new Timer();
scheduler.scheduleAtFixedRate(task,1000,1000);

final Runnable task = new Runnable() {
   public void run() {
       try {
           is = new ByteArrayInputStream(data.getBytes());
       } catch (Exception e) {
           log.d("TEST", e.toString());
       }
   }
};

in order to continuously get the updated stream of data.

Closing the outputstream doesnt mean the data coming in the stream would no longer be available to you. It means the outputstream which you were using has be deallocated from your memory and inorder to read the new data you need to read the next stream of data.

vivek jha
  • 344
  • 1
  • 11
  • I want two side communication don't want to send only from one side. and closing the output stream means writing data to stream is finished and now it can be sent to the receiver but when I close outputstream socket gets closed so I have to reconnect it to send data again and that is not best coding practice. – Yadav Uttam Sep 21 '17 at 04:26
  • The server will wait for any connections when you call accept(). I suggest you read this link thoroughly. http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html. It will clear a lot of your doubts – vivek jha Sep 21 '17 at 06:31