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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 37

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

  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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    when I try to transmit the beginning two pieces of data, it attempts to transmit the file itself
    I can't understand how code you've written can do that.
    If you don't read the file, then it won't be able to send it.
    You must have a logic problem.

    I see zero comments in your code so I can't tell you if it is doing what you want it to do or not.

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

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

    Quote Originally Posted by Norm View Post
    I can't understand how code you've written can do that.
    If you don't read the file, then it won't be able to send it.
    You must have a logic problem.

    I see zero comments in your code so I can't tell you if it is doing what you want it to do or not.
    Yeah sorry I really need to start using comments more....

    I realized what was happening. The call for the socket to get an output stream for the file was overriding the previous PrintWriter output stream call so the InputStreamReader was trying to read an incoming file. I guess I was assuming everything would be in perfect synchronization....

    I am rewriting the code to start a new socket on the same (original) server through some callable thread concurrency, transmit the file information, close the socket, restart the socket, and transmit the file.

    I am using a superclass called FileServices that the HostFiles and ClientFiles extend from.... now I have a new question. If I call the superclass method using the super keyword, will it read the subclass instance variables that are currently set? Or will I get a bunch of NPEs....

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    If I call the superclass method using the super keyword, will it read the subclass instance variables that are currently set? Or will I get a bunch of NPEs
    Write a simple short program, compile and execute it to test.
    Let us know what happens.

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

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

    Quote Originally Posted by Norm View Post
    Write a simple short program, compile and execute it to test.
    Let us know what happens.
    Thanks for your help... already did that. Check the OP. I changed it. I really need help on this problem...

    Does the buffered output stream have some kind of hard limit?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Are you trying to send binary data as text/Strings? Doesn't work.

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

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

    Quote Originally Posted by Norm View Post
    Are you trying to send binary data as text/Strings? Doesn't work.
    No.....

    Does file input stream followed by output stream read by an input stream and rewritten by a file output stream sound like that to you?

    Have you read the code yet?

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

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

    If it's of any use the InputStream always stops reading at byte 65,536 if the file is larger than that number. I don't know why. Using a BufferedInputStream yields the same result...

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Does the buffered output stream have some kind of hard limit?
    Why did you ask this?

    I've lost track of your current problem. Can you explain?

    0xFFFF=65535

    That's the limit for 2 unsigned bytes
    Last edited by Norm; July 5th, 2011 at 08:25 PM.

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

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

    Quote Originally Posted by Norm View Post
    Why did you ask this?

    I've lost track of your current problem. Can you explain?

    0xFFFF=65535

    That's the limit for 2 unsigned bytes
    Sorry... I keep on solving problems and running into new ones. Here is where I am at now.... and I don't think I can solve this one.

    I tweeked the file receiving code so that it uses a loop rather than rely on the read(int,int,int) method that doesn't seem to work all that well... (it only read a small portion of the file;65,535 bytes to be exact)

    The whole file is now read... but the problem arises after the file gets written. No program is able to read it... sure enough the files look different if you compare their raw coding in a text editor.

    So, to clarify, I can't seem to WRITE the file data to a new file properly. Here is the method that receives the file data and is supposed to write it to a new file in the specified directory:

         	protected void receiveFile() throws IOException {
    		byte[] bytes = new byte[fileBytes];
    		in = sock.getInputStream();
    		for(int i=0;i<=fileBytes;i++) {
    			in.read(bytes);
    		}
    		fos = new FileOutputStream("/Datawire/"+fileName);
    		buffout = new BufferedOutputStream(fos);
    		buffout.write(bytes,0,fileBytes);
    		buffout.flush();
    		buffout.close();
    	}

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    the files look different
    Completely or just a few bytes?

    Can you make a small executable program that demos the problem that we can test with?

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

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

    Quote Originally Posted by Norm View Post
    Completely or just a few bytes?
    A lot. Do you want me to send you example files?

    Quote Originally Posted by Norm View Post
    Can you make a small executable program that demos the problem that we can test with?
    I will try...

    And if it is worth mentioning, the bad file writing only occurs above a certain size... I haven't determined a specific number yet, however. Possible it could be that magical 65535 byte amount.

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

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

    @Norm Here is the test program I wrote. Try transferring any file larger than 64k and it will corrupt it...

    SimpleServer Class:
    [CODE]package betatesting;
     
    /*------------------------------------------------------------
     * Issue Demonstration: Test Application build01 (Main-Class)
     */
     
    import java.net.*;
    import java.io.*;
    import java.util.concurrent.*;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    /* The SimpleServer class generates a simple GUI so that we can utilize the convenience of the JFileChooser.
     * Once you choose the file, the inner class will be called to start the server.
     * The server will NOT use a loop to keep accepting new clients, so that you can test different files.
     */
     
    public class SimpleServer implements ActionListener {
    	JFrame frame;
    	File selection;
    	private ExecutorService thread = Executors.newFixedThreadPool(5);
     
    	public static void main(String[] args) {
    		new SimpleServer().gui();
    	}
     
    	public void gui() {
    		//Setup a GUI
    		frame = new JFrame("TestApp");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		JButton b1 = new JButton("Choose");
    		b1.addActionListener(this);
    		panel.add(b1);
    		frame.getContentPane().add(BorderLayout.CENTER,panel);
    		frame.setSize(300,300);
    		frame.setVisible(true);
    	}
    	//The action listener for the choose button...
    	public void actionPerformed(ActionEvent e) {
    		JFileChooser chooser = new JFileChooser("Choose A File");
    		int value = chooser.showOpenDialog(frame);
    		if(value == JFileChooser.APPROVE_OPTION) {
    			selection = chooser.getSelectedFile();
    			StartServer ss = new StartServer();
    			ss.setFile(selection);
    			//Call a thread to run the server while we go start the client...
    			thread.execute(ss);
    			//Start the client
    			SimpleClient client = new SimpleClient();
    			client.go();
    		} 
    	}
     
    	//---------------------------------------------------------------------------
     
    	class StartServer implements Runnable {
    		ServerSocket server;
    		Socket socket;
    		File file;
    		/* The server will wait for a client request, print the information and close the socket.
    		 * It will then wait for a second request, on which it will transfer the file.
    		 */
    		public void setFile(File newFile) {
    			file = newFile;
    		}
    		public void run() {
    			try {
    			server = new ServerSocket(7575);
    			socket = server.accept();
    			System.out.println("Beginning file trasnfer...");
    			PrintWriter writer = new PrintWriter(socket.getOutputStream());
    			writer.println(file.getName());
    			writer.println(file.length());
    			writer.flush();
    			socket.close();
    			socket = server.accept();
    			byte[] bytes = new byte[(int)file.length()];
    			FileInputStream fis = new FileInputStream(file);
    			BufferedInputStream buffin = new BufferedInputStream(fis);
    			buffin.read(bytes,0,bytes.length);
    			OutputStream os = socket.getOutputStream();
    			os.write(bytes,0,bytes.length);
    			os.flush();
    			socket.close();
    			} catch(IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    [/CODE]

    SimpleClient Class:
    [CODE]package betatesting;
     
    import java.io.*;
    import java.net.*;
     
    public class SimpleClient {
    	Socket socket;
    	String fileName;
    	int fileBytes;
     
    	public void go() {
    		try {
    			//Start a new socket to get the info
    			socket = new Socket("127.0.0.1",7575);
    			InputStreamReader isr = new InputStreamReader(socket.getInputStream());
    			BufferedReader reader = new BufferedReader(isr);
    			fileName = reader.readLine();
    			fileBytes = Integer.parseInt(reader.readLine());
    			socket.close();
    			//Socket to handle file transfer
    			socket = new Socket("127.0.0.1",7575);
    			byte[] bytes = new byte[fileBytes];
    			InputStream is = socket.getInputStream();
    			for(int i=0;i<fileBytes;i++) {
    				is.read(bytes);
    			}
    			//Stores the file in a new directory in your system's default directory
    			File dir = new File("/TestApp");
    		    if(!dir.exists()) {
    				dir.mkdir();
    			}
    			FileOutputStream fos = new FileOutputStream("/TestApp/"+fileName);
    			BufferedOutputStream bos = new BufferedOutputStream(fos);
    			bos.write(bytes,0,bytes.length);
    			bos.close();
    			socket.close();
    			System.out.println("Transfer Complete");
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    [/CODE]

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Your problem is in the read loop on the client. Change and add this to see what happens:
    	    int nbrRd = is.read(bytes);            // get count of bytes read this call
                System.out.println("C nbrRd=" + nbrRd); // Show nbr bytes read

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

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

    Quote Originally Posted by Norm View Post
    Your problem is in the read loop on the client. Change and add this to see what happens:
    	    int nbrRd = is.read(bytes);            // get count of bytes read this call
                System.out.println("C nbrRd=" + nbrRd); // Show nbr bytes read
    It said 65536... yes. It didn't read all of it. Do you have any idea on how to change that?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Did you make the changes I suggested? What was printed out?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    BTW Nice test program.
    However I prefer one that works automatically without a GUI.
    I added this:
       public static void main(String[] args) {
          File selection = new File("images/AreWeFriends.jpg");
        	ExecutorService thread = Executors.newFixedThreadPool(5);
     
       	StartServer ss = new StartServer();
       	ss.setFile(selection);
       	//Call a thread to run the server while we go start the client...
       	thread.execute(ss);
     
       	//Start the client
       	SimpleClient client = new SimpleClient();
       	client.go();
     
     
       }
    And commented out the SimpleServer up to the StartServer class definition

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

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

    Quote Originally Posted by Norm View Post
    BTW Nice test program.
    However I prefer one that works automatically without a GUI.
    I added this:
       public static void main(String[] args) {
          File selection = new File("http://www.javaprogrammingforums.com/images/AreWeFriends.jpg");
        	ExecutorService thread = Executors.newFixedThreadPool(5);
     
       	StartServer ss = new StartServer();
       	ss.setFile(selection);
       	//Call a thread to run the server while we go start the client...
       	thread.execute(ss);
     
       	//Start the client
       	SimpleClient client = new SimpleClient();
       	client.go();
     
     
       }
    And commented out the SimpleServer up to the StartServer class definition
    Hey whatever you prefer. I only used the GUI for the benefit of JFileChooser since it makes it easier to test different files.

    Yes I did make the changes you suggested. It printed out "65536"

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    I suggested: System.out.println("C nbrRd=" + nbrRd);
    And you say it printed out: 65536
    I don't see how that is possible. What else did it print out? Wasn't "C nbrRd=" there?

    Could you post the full code for the loop where the client reads the data sent from the server.

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

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

    I did realize, however, I should probably be using smaller chunks of read data rather than trying to store all of the file data in one stream. I'm having trouble making that work though...

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

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

    Quote Originally Posted by Norm View Post
    I suggested: System.out.println("C nbrRd=" + nbrRd);
    And you say it printed out: 65536
    I don't see how that is possible. What else did it print out? Wasn't "C nbrRd=" there?

    Could you post the full code for the loop where the client reads the data sent from the server.
    Yes yes yes... I just figured that was unimportant. Exactly, it printed "C nbrRd = 65536".

    Full code for the test app or the actual program?

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    It only printed that one line? The println should be inside of the loop.

    Could you post the code for the loop where the client reads the data sent from the server.
    Last edited by Norm; July 6th, 2011 at 08:07 PM.

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

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

    Wait... did you mean make the change on the original program?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    The purpose of the changes: get the number of bytes read and print them out is to provide debug information about how your program is executing.
    I was talking about the test program which I have a copy of. I was telling you what I did to find the problem. I was asking you to do the same thing so you could see what I saw.

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

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

    Quote Originally Posted by Norm View Post
    The purpose of the changes: get the number of bytes read and print them out is to provide debug information about how your program is executing.
    I was talking about the test program which I have a copy of. I was telling you what I did to find the problem. I was asking you to do the same thing so you could see what I saw.
    There is no loop existent in the client side of the test application. Could you specify which section you are referring to?

Page 1 of 2 12 LastLast

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