I am currently trying to design a client-server application, something like this: the user connects to the server and when the authentication is OK the server send to the user some files. The problem is that those files are written in a single file (with my method).
Here is some code:
The function which transfers the file
public void processFile(int id, DataOutputStream oStream, Socket socket, int tip){
String fileName;
if(tip==0){
fileName="File"+Integer.toString(Intrebari[id])+".txt";
}else{
fileName="Image"+Integer.toString(Intrebari[id])+".jpg";
}
byte[] buffer=null;
int bytesRead = 0;
FileInputStream file=null;
try {
file = new FileInputStream(fileName);
buffer = new byte[socket.getSendBufferSize()];
while((bytesRead = file.read(buffer))>0)
{
oStream.write(buffer,0,bytesRead);
}
file.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
And the function that choose which file have to be send
private void send(int id) throws IOException {
os.writeBytes("sendFile" + id+"\n");
System.out.println("sendFile" + id);
generatedFiles.processFile(id, os, comunicare, 0);
if (generatedFiles.Imagini[id] == 1) {
os.writeBytes("sendImage" + id+"\n");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("sendImage" + id);
generatedFiles.processFile(id, os, comunicare, 1);
}
}
I have to mention that os
is DataOutputStream
and comunicare
is Socket
type.
I think that the problem is that I combine writeBytes
with write
. Can anyone help me with this problem? How can I make the server and the client to receive both files and messages?