I need to send a short string in the form of text from the client to the server, and then after that send the binary file.
how can i send both binary and string using the same socket connection?
the server is a Java desktop application, and the client is an Android tablet. I already configured it to send text messages between the client and server in both directions. I have not completed the binary submission part yet.
One idea is to set up two separate servers at the same time. I think this is possible if I use two different port numbers and configure the servers to two different threads in the application. and I will need to configure two simultaneous clients running on two services in the Android application.
another idea is to somehow use the if else statement to determine which of the two file types is sent, or the binary text, and use the appropriate method to get the file for the file type to send.
sample code for sending text
PrintWriter out; BufferedReader in; out = new PrintWriter(new BufferedWriter (new OutputStreamWriter(Socket.getOutputStream())) true,); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out.println("test out"); String message = in.readLine();
sample code to send a binary file
BufferedOutputStream out; BufferedInputStream in; byte[] buffer = new byte[]; int length = 0; out = new BufferedOutputStream(new FileOutputStream("test.pdf)); in = new BufferedInputStream(new FileOutputStream("replacement.pdf")); while((length = in.read(buffer)) > 0 ){ out.write(buffer, 0, length); }
source share