EOFException - socket programming
Hi guys,
I'm new here and i want your help..
I'm trying to read bytes form a stream and store them in a file..
I give you the client code..Check the code and tell me if there's anything wrong:
transferedFile.createNewFile();
// read the length of bytes from Server
int fileLength = inFromServer.read();
byte[] buffer = new byte[fileLength];
DataInputStream receivedFile = new
DataInputStream(sock.getInputStream());
for (int i=0;i<buffer.length;i++){
buffer = receivedFile.readByte();
}
ThefileLength is the File's length that exists in the Server
Theerror that i take when i run the code is:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readByte
at TestClient main
Could anyone help me?I'll be very grateful...
Thanks,
Pavanas
P.S.:If you want the hole code just ask me and I'll post it..>
[991 byte] By [
pavanasa] at [2007-11-26 18:46:28]

# 1
Here is my client code.
import java.io.*;
import java.net.*;
import java.lang.*;
public class TestClient {
/** Creates a new instance of TestClient */
public TestClient() {
}
public static void main(String[] args) throws IOException{
// Server IP and Server port
final InetAddress server_addr =InetAddress.getByName("localhost");
final int server_port = 5000;
final String directory="C:/";
// creates the socket
Socket sock = new Socket(server_addr,server_port);
BufferedReader inFromUser = null,inFromServer = null,inFromSrvr = null;
PrintWriter outToServer = null;
try{
// create the input and output streams through socket
outToServer = new PrintWriter(sock.getOutputStream(),true);
inFromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
inFromUser = new BufferedReader(new InputStreamReader(System.in));
//inFromSrvr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
}
catch (IOException e){
sock.close();
System.exit(1);
}
try{
// Request a File name from the user
System.out.print("Give the Fileanme[ending is requiered] that you want:");
String wantedFile = inFromUser.readLine();
if (wantedFile.equals("") != true){
System.out.println("You have given a Filename...");
System.out.println("The File you have asked is:" + wantedFile);
// send Filename to Server
outToServer.println(wantedFile);
// accept answer from Server
String answerFromServer = inFromServer.readLine();
if (answerFromServer.equalsIgnoreCase("exist") == true){
System.out.println("File " + wantedFile + " is tranfering from Server..");
File transferedFile = new File(directory + wantedFile);
// if file already exists on client then crash
if (transferedFile.exists() == true){
System.out.println("File already exists...Can't copy..");
System.exit(1);
}
else{
transferedFile.createNewFile();
// read the length of bytes from Server
int fileLength = inFromServer.read();
byte[] buffer = new byte[fileLength];
DataInputStream receivedFile = new DataInputStream(sock.getInputStream());
//InputStream receivedFile = sock.getInputStream();
for (int i=0;i<buffer.length;i++){
buffer[i] = receivedFile.readByte();
}
/*int i = 0;
while (receivedFile.read(buffer) != -1){
buffer[i] = receivedFile.readByte();
i++;
}*/
//write buffer to file
try{
FileOutputStream writeToFile = new FileOutputStream(transferedFile);
writeToFile.write(buffer,0,fileLength);
writeToFile.close();
}
catch(EOFException eofe){
System.err.println(" "+eofe);
}
catch(Exception e2){
System.out.println("Can't write to file...");
}
}
}
else{
System.out.println("Wrong String From Server...");
System.exit(1);
}
}
else{
System.out.println("Empty String...You must give a Filename...");
System.exit(1);
}
}
finally{
outToServer.close();
inFromUser.close();
inFromServer.close();
sock.close();
}
}
}
Anyone plz....
Thanks in advance,
pavanas
# 2
Your premature EOFException is caused by data being lost in the BufferedReader. So you are also reading the wrong data as the file length. Get rid of the BufferedReader and use DataInputStream.readLine() instead. And yes, I know that DataInputStream.readLine() is deprecated, but it also isn't buffered.
> int fileLength = inFromServer.read();
This only reads a char, i.e. an unsigned int, so the maxuimum file size you can transmit is 2^32 bytes. Is that enough? I would change this to DataInputStream.readInt() or readLong(), and change the other end accordingly.
> byte[] buffer = new byte[fileLength];
And conversely, this is going to run out of memory if the file is too large. Use a buffer of 8192 or 16384 etc. The rest of your code should look like this:
byte[] buffer = new byte[8192];
InputStream receivedFile = new BufferedInputStream(sock.getInputStream());
FileOutputStream writeToFile = new FileOutputStream(transferedFile);
int count;
while ((count = receivedFile.read(buffer)) > 0)
writeToFile.write(buffer, 0, count);
// close files etc ...
ejpa at 2007-7-9 6:20:22 >
