So I am writing a multithreaded server which takes in as an input the server name, port, number of threads, and a file name to request.. My design is to create a socket through main and then extend a Runnable object (which takes in a socket and the file name).. I also have a timertask which fires when 5 seconds are over and then closes and stops all threads.. My current design is to create the socket in main, pass it to runnable (where there is a while loop on condition x, requesting the file over and over), and then when the task fires up it sets the private shared variable x in runnable to false.
I am facing a lot of broken pipe issues which leads me to believe my design is flawed.. Am I missing something? I am not using synchronized/locked variables. Should I create a new socket for every file request?
Stacktrace:
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
at java.io.DataOutputStream.writeBytes(DataOutputStream.java:259)
at FR.filesRecv(Client.java:62)
at FR.run(Client.java:79)
at java.lang.Thread.run(Thread.java:637)
Some general comments:
mainand then just sleep for 5 seconds and then set therunnablevariables to be false. No need for the timer-task.mainif you want the JVM to stop quickly or you can calljoin()on each of your threads to make sure they finish appropriately. You can also setthread.setDaemon(false);before the thread starts and then exitmainand the JVM won’t stop until the threads finish on their own.runnablevariable isvolatileif it is going to be set from another thread.