I have a server and client and I need the server to send a jpeg file through a tcp socket, a file called base.jpg that has a size of 3092 bytes, the client is supposed to make and save the jpeg image inside base.jpg into a file called newbase.jpg

My problem is that the new image file created by the client can't be viewed, it gives an error (says the file format is not supported) when you try to look at it. Also in the newbase.jpg properties it's showing the size of it as 3093 bytes, even though the original image base.jpg is 3092, not sure if that has anything to do with it.

Here is the current code for the part that does this:

server
Java Code:
ServerSocket inSocket = new ServerSocket(40075);

while (true)
{
DataOutputStream toClient = new DataOutputStream(sSocket.getOutputStream());


File jfile = new File("base.jpg");
		if (!jfile.exists() || !jfile.isFile())
		{
			System.out.println("Not a file");
		}
		else System.out.println("Is a file");
		
		int filesize = (int)jfile.length();
		
		System.out.println(filesize);//prints 3092
		
		BufferedInputStream fis = new BufferedInputStream(new FileInputStream(jfile));
		byte[] buffer = new byte[filesize];
		
		int length;
		try
		{
		while ((length = fis.read(buffer)) != -1)
		{
			
			toClient.write(buffer, 0, length);
			toClient.flush();
		}
		}
		catch (Exception e){}
}
and the client

Java Code:
InetAddress addr = InetAddress.getLocalHost();


			       Socket clSocket = new Socket(addr,40075);

			       DataOutputStream toPserver = new DataOutputStream(clSocket.getOutputStream());
			       DataInputStream fromPserver = new DataInputStream(clSocket.getInputStream());

			       
			       File dlfile = new File("newbase.jpg");
			       dlfile.createNewFile();
			       
			       DataOutputStream dos = new DataOutputStream(new FileOutputStream(dlfile));
			       try {
			       while (fromPserver.available() > 0)
			       {
			    	   dos.writeByte(fromPserver.readByte());
			       }
			       }
			       catch (Exception e){}
			       System.out.println("image transfer done");

clSocket.close();
I'm not sure if I have to convert the file in client to a buffered image or how to do this.