UPDATED ISSUE:
After redesigning the program to use separate sockets on the same central server, I am now experiencing a strange issue...
I can send Word documents and text files over the stream successfully, but any other type of file (music,video,etc.) throws a "Connection reset by peer: socket write error" exception...
java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at networking.FileServices.sendFile(FileServices.java:43) at networking.HostFiles$OutboundFile.run(HostFiles.java:54) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
And the new file written by the program has the same amount of data as the original but is corrupted and can't be read by any program (besides Word documents and text files.... they turn out fine for some reason).
HostFiles and ClientFiles just handle the sockets by calling for HostService to accept new socket requests. Depending on which side initiated the transfer, either the host or client then calls the superclass's methods to actually handle the streaming.
Here is the source code for the superclass (FileServices). You will notice there are no socket.close() calls because I was experimenting with why the connection is getting reset by the peer....
[CODE]package networking; import java.io.*; import java.net.*; abstract class FileServices { static File file; FileOutputStream fos; InputStream in; OutputStream out; BufferedInputStream buffin; BufferedOutputStream buffout; Socket sock; static String fileName; static int fileBytes; byte[] incomingBytes; Preferences p = new Preferences(); ThreadManager tm = new ThreadManager(); //sendInformation() is used with the first socket initiation to transmit essential data about the file to the receiving end... //It can be called by either client or host in their respective subclasses of this class. protected void sendInformation() throws IOException { PrintWriter writer = new PrintWriter(sock.getOutputStream()); writer.println(file.getName()); writer.println(file.length()); writer.flush(); sock.close(); } //Receives the essential information needed to write the file.... protected void receiveInformation() throws IOException { InputStreamReader reader = new InputStreamReader(sock.getInputStream()); BufferedReader buffer = new BufferedReader(reader); fileName = buffer.readLine(); fileBytes = Integer.parseInt(buffer.readLine()); System.out.println(fileBytes); buffer.close(); sock.close(); //The socket is now closed and another one is requested and opened in order to handle the actual file transfer. } //Reads the file data and writes it to the socket's output stream protected void sendFile() throws IOException { byte[] bytes = new byte[(int)file.length()]; buffin = new BufferedInputStream(new FileInputStream(file)); buffin.read(bytes,0,bytes.length); out = sock.getOutputStream(); out.write(bytes,0,bytes.length); //This is the line where the exception above is thrown (line 43) out.flush(); } //For the receiving end.... this method reads the socket's input stream, and calls the method to write the file. protected void receiveFile() throws IOException { incomingBytes = new byte[fileBytes]; in = sock.getInputStream(); buffin = new BufferedInputStream(in); buffin.read(incomingBytes,0,incomingBytes.length); //Call the writeFile() method to write the data to a file... This was an experiment. Before, writeFile was inside this method. writeFile(); } //Writes the file to the application's storage directory... private void writeFile() { try { fos = new FileOutputStream("/Datawire/"+fileName); buffout = new BufferedOutputStream(fos); buffout.write(incomingBytes,0,fileBytes); buffout.flush(); } catch(IOException e) { writeFile(); } } }[/CODE]
Any help is appreciated!