Socket Exception
I have code for transferring files (thanks for help so far). The trouble is, after the file has been received, the client sends "exit" to the server, which is supposed to read that, and close the socket. However, for some reason, the server is blocking and not receiving the command, until the socket is closed, giving me an exception. I know I could bypass this by simply putting a Try Catch in there, but that's just avoiding the error rather than figuring out why it is happening? I have (in the client):
public WebClient()throws IOException{
String fileName ="test.txt";//this is the hard-coded file that will be requested.
Socket socket =null;
PrintWriter out =null;
BufferedReader in =null;
boolean outputToTerminal =true;//so that bytes from a file transfer aren't displayed to the terminal window.
File file =new File(fileName);//a little too much hard-coding, but it's only an example.
try{
socket =new Socket("127.0.0.1", 8008);
out =new PrintWriter(socket.getOutputStream(),true);
in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (UnknownHostException e){
System.out.println("Couldn't resolve address: 127.0.0.1");
System.exit(1);
}
catch (IOException e){
System.out.println("Couldn't get I/O for the connection to: 127.0.0.1");
System.exit(1);
}
BufferedReader stdIn =new BufferedReader(new InputStreamReader(System.in));
System.out.println(in.readLine());//This is the greeting message from the server.
String sent ="GET " + fileName;//hardcoded.
out.println(sent);
System.out.println(">" + sent);//The > is for display purposes, to show "user" commands.
while (outputToTerminal){
String received = in.readLine();
if (received.startsWith("File size:")){//This comes just before the bytes are sent.
outputToTerminal =false;//no more commands are to be displayed until the transfer is complete.
}
System.out.println(received);
}
file.createNewFile();//this file will be filled with the sent bytes.
FileOutputStream testFile =new FileOutputStream(file.toString());
//in.close();
DataInputStream dataInput =new DataInputStream(socket.getInputStream());
byte received[] =newbyte[socket.getReceiveBufferSize()];
int test = dataInput.read(received);
System.out.println(test);
while (test != -1){
testFile.write(received);
test = dataInput.read(received);
}
testFile.close();//the file channel is closed.
//dataInput.close();
//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(received);
System.out.println(in.readLine());//the last two strings sent by the server, again, hardcoded.
sent ="EXIT";
System.out.println(">" + sent);
out.println(sent);//tells the server we are finished.
//out.close(); //cleaning up.
//in.close();
//stdIn.close();
//socket.close();
}
Where all the close()s were commented out to see if they were causing the problem.
In the server:
publicclass WebServer
{
private File file;
private Socket client =null;
private PrintWriter terminal;
publicstaticvoid main(String[] args)throws IOException{
WebServer webServer =new WebServer();
}
public WebServer()throws IOException{//static void main(String[] args) throws IOException {
ServerSocket server =null;
try{
server =new ServerSocket(8008);
}catch (IOException e){
System.out.println("Unable to listen to Port 8008.");
System.exit(1);
}
try{
client = server.accept();
}catch (IOException e){
System.err.println("Connection failed.");
System.exit(1);
}
terminal =new PrintWriter(client.getOutputStream(),true);
BufferedReader command =new BufferedReader(new InputStreamReader(client.getInputStream()));
terminal.println("Welcome to Jimmy's webserver.");
String input;
try{
while ((input = command.readLine()) !=null){
if (input.toLowerCase().equals("exit")){
break;
}
elseif (input.toLowerCase().equals("help")){
terminal.println("Commands:");
terminal.println("GET <filename> - JWS will send <filename> to you.");
terminal.println("EXIT - exits Jimmy's Webserver.");
}
elseif (input.toLowerCase().startsWith("get ")){
sendFile(new File(input.substring(4)));
}
}
terminal.close();
command.close();
client.close();
server.close();
}
privatevoid sendFile(File file)throws FileNotFoundException, IOException{
if (file ==null){
terminal.println("Please use the command: GET filename");
}
elseif (!file.exists()){
terminal.println(file.toString() +" could not be found in the current directory.");
}
else{
terminal.println("Sending file: " + file.toString());
terminal.println("File size: " + file.length() +"bytes.");
Long time = System.currentTimeMillis();
FileInputStream fileReader =new FileInputStream(file);
byte buffer[] =newbyte[client.getReceiveBufferSize()];
//terminal.close();
DataOutputStream dataTerminal =new DataOutputStream(client.getOutputStream());
//terminal.close();
while (fileReader.read(buffer) != -1){
dataTerminal.write(buffer);
}
fileReader.close();
//dataTerminal.close();
terminal =new PrintWriter(client.getOutputStream());
dataTerminal.close();
terminal.println("Transfer of " + file.toString() +" is complete.");
time = System.currentTimeMillis() - time;
if (time == 0){ time++;}
terminal.print("Transfer took: " + time +" milliseconds. ");
terminal.close();
}
}
}
I do appreciate there is a lot of code there, but I thought rather than extracting some and have you wondering some aspects, I might as well paste the whole thing.
The line that is blocking (and eventually crashing) is:
while ((input = command.readLine()) !=null){
Any help you can give me would be greatly appreciated. Thank-you.

