I am using the code in Java TCP socket: data transfer is slow, works nice but i have one problem on the receiving part it reads the data but doesn't exit the while loop, don't really know why, code is exactly the same.
ok so the code is like this client side:
uploadFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
try{
JFileChooser chooser = new JFileChooser();
File file=null;
int returnVal = chooser.showOpenDialog(UserWindow.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
file = chooser.getSelectedFile();
FileInputStream fis = new FileInputStream(file);
OutputStream os= Client.socket.getOutputStream();
int packetSize=65536;
byte[] buffer = new byte[packetSize];
Client.out.writeObject("upload file");
int read=0;
do{
os.write(buffer, 0, read);
System.out.println(read);
}while((read = fis.read(buffer))!=-1);
os.flush();
System.out.println("sent");
fis.close();
}
catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"File send error: "+ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);}
}
});
server side:
else if(message.equals("upload file")){
try{
FileOutputStream fos=new FileOutputStream("doc.pdf");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int packetsize=65536;
byte[] buffer = new byte[packetsize];
InputStream is =socket.getInputStream();
int read=0;
do{
bos.write(buffer,0,read);
System.out.println(read);
}while((read = is.read(buffer))!=-1);
System.out.println("received");
bos.close();
fos.close();
}catch(Exception ex){ex.printStackTrace();}
}