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?
I do something like this:
Server -send Command Server -send File Client -confirms file sucessfull
Server -send Command Server -send File Client -confirms file sucessfull ...
like this...supose you have a socket from a client, than you...
socket.getOutputStream.write("FILE: SomeFile.bin, SIZE: 872973493\r\n".getBytes("choose an encoding")) socket.getOutputStream.flush(); (you will only need to flush if you are expecting a server string response like: OK SEND ME THE FILE, IM READY, otherwise no need too) client reads and see that is a file and it has this bytes size, so it starts reading from socket.getInputStream untill it gets the length of file as expected. after this clients confirm he recived the file
then server can send another file, you could do instead of FILE, use IMAGE: or anything you want. You just have to read the message from client side to see if it is a file or image
Here are some functions that might help you:
public static void readInputStreamToFile(InputStream is, FileOutputStream fout,
long size, int bufferSize) throws Exception
{
byte[] buffer = new byte[bufferSize];
long curRead = 0;
long totalRead = 0;
long sizeToRead = size;
while(totalRead < sizeToRead)
{
if(totalRead + buffer.length <= sizeToRead)
{
curRead = is.read(buffer);
}
else
{
curRead = is.read(buffer, 0, (int)(sizeToRead - totalRead));
}
totalRead = totalRead + curRead;
fout.write(buffer, 0, (int) curRead);
}
}
public static void writeFileInputStreamToOutputStream(FileInputStream in, OutputStream out, int bufferSize) throws Exception
{
byte[] buffer = new byte[bufferSize];
int count = 0;
while((count = in.read(buffer)) != -1)
{
out.write(buffer, 0, count);
}
}