Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 26 to 37 of 37

Thread: Can't Get Network I/O To Write And Read Properly...

Threaded View

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Can't Get Network I/O To Write And Read Properly...

    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!
    Last edited by bgroenks96; July 5th, 2011 at 03:46 PM. Reason: Changing issue


Similar Threads

  1. Images (read/write, drawing)
    By helloworld922 in forum File Input/Output Tutorials
    Replies: 1
    Last Post: September 5th, 2011, 09:11 AM
  2. Program to read & write the Employee Records
    By arvindk.vij in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2011, 01:19 AM
  3. Read (using Odbc)and write using (POI api)
    By amruta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2010, 10:12 AM
  4. How to Write and Read Binary Data
    By neo_2010 in forum File Input/Output Tutorials
    Replies: 3
    Last Post: January 4th, 2010, 02:38 PM