how to send both binary file and text using the same socket
i have to send a short string as text from client to server and then after
that send a binary file.
how would I send both binary file and the string using the same socket
connection?
the server is a java desktop application and the client is an Android
tablet. i have already set it up to send text messages between the client
and server in both directions. i have not yet done the binary file sending
part.
one idea is to set up two separate servers running at the same time. I
think this is possible if i use two different port numbers and set up the
servers on two different threads in the application. and i would have to
set up two concurrent clients running on two services in the Android app.
the other idea is to somehow use an if else statement to determine which
of the two types of files is being sent, either text of binary, and use
the appropriate method to receive the file for the file type being sent.
example code for sending text through socket
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();
example code for sending binary file through socket
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);
}
No comments:
Post a Comment