Sending File to fast and receving too slow

i can type simple text and transmit through tcp/ip client/server model. However, once i make a file transfer, it can be done but after the file transfer the program got exception and keep on looping and can't read or write on both clientserver.
[252 byte] By [Ckgala] at [2007-11-27 2:24:34]
# 1

> i can type simple text and transmit through tcp/ip

> client/server model. However, once i make a file

> transfer, it can be done but after the file transfer

> the program got exception

What exception?

> and keep on looping

in what code?

> and

> can't read or write on both clientserver.

Can't read or write what?

ejpa at 2007-7-12 2:31:46 > top of Java-index,Core,Core APIs...
# 2

What exception?

It has a socket closed exception if i close the socket or it will enter while loop and realise it can't read any thing and jump back and forth entering while loop and exception.

in what code?

Java

> can't read or write on both clientserver.

Can't read or write what?

I mean after file transferred is done. it can't resume the usual read and write of simple text.

Basically i have a server inclusive of Server (JFrame), Listener Thread(to listen for incoming client request) and Server Thread to proceed with i/o of message.

I have a client inclusive of a Client(Jframe) and a ClientThread for i/o of message.

When the client decide to send file, it will select from filechooser and open up filestream and send and close the stream. Then the server will open filechooser to save the file.

The problem lies when in the client i select file and send, immediately after sending it will jump to read input fail exception and while loop etc then the server side will go into this kind of state as well.

Ckgala at 2007-7-12 2:31:46 > top of Java-index,Core,Core APIs...
# 3
I think you are missing the point. WE CAN'T SEE YOUR CODE SO HOW CAN WE TELL FROM YOUR TERSE DESCRIPTION WHAT IS LIKELY TO BE WRONG!
sabre150a at 2007-7-12 2:31:46 > top of Java-index,Core,Core APIs...
# 4

Client (JFrame Code)

private void SendFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendFileActionPerformed

// TODO add your handling code here:

JFileChooser fileChooser = new JFileChooser();

fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

int result = fileChooser.showDialog(this,"Send");

if (result == JFileChooser.CANCEL_OPTION)

return;

File fileName = fileChooser.getSelectedFile();

if (fileName == null || fileName.getName().equals("")){

JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid",

JOptionPane.ERROR_MESSAGE);

}else{

try{

// sendfile

output.writeUTF("FILE TRANSFER\n");

output.flush();

byte [] mybytearray = new byte [(int)fileName.length()];

System.out.println(mybytearray.length);

FileInputStream fis = new FileInputStream(fileName);

BufferedInputStream bis = new BufferedInputStream(fis);

bis.read(mybytearray,0,mybytearray.length);

int i;

for(i=0; i<mybytearray.length; i++){

System.out.println(mybytearray + " ");

}

setOutputFile(getSocket().getOutputStream());

getOutputFile().write(mybytearray,0,mybytearray.length);

displayMessage("Sending ... \n" + fileName.toString() + "\n");

output.writeUTF(fileName.toString());

System.out.println("Sending...");

getOutputFile().write(4444);

getOutputFile().flush();

getOutputFile().close();

//socket.close();

}catch (IOException io){

displayMessage("Error in File Transfer\n");

//System.exit(-1);

}

}

ClientThread Code (Exception Read Failed and Enter While Loop etc)

public void run() {

System.out.println("come to ClientThread");

try{

client.displayMessage("Inside the ClientThread\n");

client.input = new DataInputStream(client.getSocket().getInputStream());

client.output = new DataOutputStream(client.getSocket().getOutputStream());

client.displayMessage("Creating the input and output streams\n");

client.output.writeUTF("Connecion Established !\n");

} catch (IOException e) {

client.displayMessage("Failed to create input or output Stream\n");

//System.exit(-1);

}

while(true){

try{

System.out.println("Enter the while loop waiting");

//server.displayMessage("\nEnter the loop waiting\n");

String message = client.input.readUTF();

client.displayMessage(message);

System.out.println("Client read success at the try-catch loop @ thread");

} catch (IOException e) {

System.out.println("Read failed");

//System.exit(-1);

}

}

ServerThread(Receiving File and Read Failed and Enter While Loop)

public void run(){

System.out.println("come to serverthread");

String line;

input = null;

//output = null;

//server.displayMessage("Connection Established !\n");

try{

server.displayMessage("Inside the ServerThread\n");

input = new DataInputStream(welcomeSocket.getInputStream());

server.output = new DataOutputStream(welcomeSocket.getOutputStream());

server.displayMessage("Creating the input and output streams\n");

server.output.writeUTF("Connecion Established !\n");

//inputFile = client.getInputStream();

//server.displayMessage("\nStarted the inputFile Stream\n");

} catch (IOException e) {

server.displayMessage("Failed to create input or output Stream\n");

//System.exit(-1);

}

while(true){

try{

System.out.println("Enter the while loop waiting");

//server.displayMessage("\nEnter the loop waiting\n");

String message = input.readUTF();

System.out.println("Fine and smooth");

server.displayMessage(message);

System.out.println("Fine and smooth");

System.out.println("okay");

setInputFile(welcomeSocket.getInputStream());

if(message.equalsIgnoreCase("FILE TRANSFER\n")){

System.out.println("working");

while(inputFile.read() != 4444){

System.out.println("getting in");

int filesize=1024;

int bytesRead;

int current = 0;

byte [] mybytearray = new byte [filesize];

//server.saveFile();

FileOutputStream fos = new FileOutputStream("aaa.txt");

System.out.println("getting the data from File1");

BufferedOutputStream bos = new BufferedOutputStream(fos);

System.out.println("getting the data from File2");

inputFile.read(mybytearray,0,mybytearray.length);

int j;

for(j=0; j><mybytearray.length; j++){

System.out.println(mybytearray[j] + " ");

}

System.out.println("getting the data from File3");

bos.write(mybytearray,0,mybytearray.length);

BufferedInputStream bis = new BufferedInputStream(fis);

bis.read(mybytearray,0,mybytearray.length);

int i;

for(i=0; i><mybytearray.length; i++){

System.out.println(mybytearray + " ");

}

setOutputFile(getSocket().getOutputStream());

getOutputFile().write(mybytearray,0,mybytearray.length);

displayMessage("Sending ... \n" + fileName.toString() + "\n");

output.writeUTF(fileName.toString());

System.out.println("Sending...");

getOutputFile().flush();

output.writeUTF("ACCOMPLISHED\n");

getOutputFile().close();*/

System.out.println("File Transfer accomplished");

server.displayMessage("File Transfer accomplished\n");

server.output.writeUTF("File Transfer accomplished\n");

bos.close();

}

System.out.println("Server read success at the try-catch loop @ thread");

}else if (message.equalsIgnoreCase("DISCONNECT\n") == true){

server.displayMessage("Get into disconnecting stage\n");

input.close();

server.output.close();

server.displayMessage("Client Disconnected from Server\n");

server.ListenSocket();

}

} catch (IOException e) {

System.out.println("Server Read failed");

//System.exit(-1);

}

}>

Ckgala at 2007-7-12 2:31:46 > top of Java-index,Core,Core APIs...
# 5

I can't make head or tail of that without formatting, and we would need a description of your application protocol to figure out which pieces of the client read and/or write what to the socket, and whether this is properly sequenced, and whether you've implemented your application protocol correctly, but I can see a few basic problems:

- readUTF can throw an EOFException which you should be handling separately

- in most places place you are treating all IOExceptions the same, e.g. as 'read failed' and not printing the actual exception, so you don't really know what's going on

- where you test in.read() != 4444 you are throwing away the result of that read, so you are losing a byte every time around this loop

- you're ignoring the result of reading into your buffer so you don't know how much data is in it, if any: you're just asssuming it wasn't an EOF and also that the buffer was completely filled and writing out the whole buffer.

The usual read loop looks like this:

int count;

byte[] buffer = new byte[4096]; // or more

while ((count = in.read(buffer)) > 0)

out.write(buffer,0,count);

out.close();

You might get some hints from that.

ejpa at 2007-7-12 2:31:46 > top of Java-index,Core,Core APIs...
# 6
send the acknowledgement from the server to the client that a perticular line has reached at server when this acknowlegement recieved at client side send another line from the client.
Neem-Baz-Aankhaina at 2007-7-12 2:31:46 > top of Java-index,Core,Core APIs...