I'm trying to both send and recieve Json files on client and server side, but it either gets stuck whent the server tries to recieve the file or it says the socket is closed.
My Server:
private void handleClientRequest(Socket clientSocket)
{
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
try
{
// Receive json
inputStream = clientSocket.getInputStream();
fileOutputStream = new FileOutputStream(Strings.PLANNING_SOFTWARE_DRONE_JSON);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) > 0)
{
fileOutputStream.write(buffer, 0, bytesRead);
}
System.out.println("Received JSON file from client");
// Send json
File responseFile = new File("response.json");
outputStream = clientSocket.getOutputStream();
fileInputStream = new FileInputStream(responseFile);
while ((bytesRead = fileInputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
fileOutputStream.close();
fileInputStream.close();
outputStream.close();
System.out.println("Sent JSON file to client");
clientSocket.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
Client:
private void sendGuiJson (Socket guiServer)
{
JSONController.createDroneJSON(drone, Strings.DRONE_CONTROLLER_DRONE_JSON);
FileInputStream fileInputStream = null;
OutputStream outputStream = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try
{
File jsonFile = new File(Strings.DRONE_CONTROLLER_DRONE_JSON);
byte[] buffer = new byte[1024];
fileInputStream = new FileInputStream(jsonFile);
outputStream = guiServer.getOutputStream();
int bytesRead;
while((bytesRead = fileInputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
System.out.println("Sent JSON file to server");
inputStream = guiServer.getInputStream();;
fileOutputStream = new FileOutputStream("new");
while ((bytesRead = inputStream.read(buffer)) > 0)
{
fileOutputStream.write(buffer, 0, bytesRead);
}
System.out.println("Received JSON file from server");
fileInputStream.close();
outputStream.close();
inputStream.close();
fileInputStream.close();
guiServer.close();
}
catch(FileNotFoundException e)
{
System.out.println(Strings.SENDGUIJSON_FILENOTFOUND);
throw new RuntimeException(e);
}
catch(SecurityException e)
{
System.out.println(Strings.SENDGUIJSON_SECURITY);
throw new RuntimeException(e);
}
catch(IOException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
The current Output looks like this
Server started on port 10808
Client connected: 127.0.0.1
Sent JSON file to server
When I add outputStream.close() after flushing it on the client side the I get a little further:
Server started on port 10808
Client connected: 127.0.0.1
Received JSON file from client
Sent JSON file to server
Sent JSON file to client
java.net.SocketException: Socket is closed
at java.base/java.net.Socket.getInputStream(Socket.java:986)
at drone.control.DroneController.sendGuiJson(DroneController.java:70)
at drone.control.DroneController.run(DroneController.java:38)
at java.base/java.lang.Thread.run(Thread.java:1623)
Exception in thread "Thread-0" java.lang.RuntimeException: java.net.SocketException: Socket is closed
at drone.control.DroneController.sendGuiJson(DroneController.java:99)
at drone.control.DroneController.run(DroneController.java:38)
at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: java.net.SocketException: Socket is closed
at java.base/java.net.Socket.getInputStream(Socket.java:986)
at drone.control.DroneController.sendGuiJson(DroneController.java:70)
... 2 more
The DroneController is the client with the method sendGuiJson().
I am stuck at this point.
I want the client so successfully recieve the file sent by the server.