Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket I/O data not being sent

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
CA
Hi,

The code below is really quite simple. I'm trying to create a socket-based client and server that will allow the client to send a filename to the server and the server will return that file to the client. Right now I've just hard-coded a file to be returned. The server is receiving the input "Requested Filename" from the client, but when the server tries to send back the data from the 2MB file "c:\temp\music.mp3", nothing happens. No ProgressMonitorInputStream window appears and no exceptions appear to be thrown. I've written socket-based applications before, and individually, all of these pieces of code have worked, but when combined, it seems to do nothing! It doesn't seem to like the mixing and matching of I/O procedures. Any help would be appreciated!

Thanks,

TheInsider

Code:
import java.io.*;
import java.net.*;

public class Server {
	private static final int SERVER_PORT = 1979;

	public static void main(String[] args) {
		ServerSocket socket;

// Open the GUI client here for now, so I don't need to run two applications.
new GUIClient();

		try {
			socket = new ServerSocket(SERVER_PORT);

			while (true)
				new Thread(new Connection(socket.accept())).start();
		} catch (IOException e) {
			System.err.println("Server.main()");
			System.err.println(e);
			System.exit(-1);
		}
	}
}

-------------------------------------------------------------------------------------------------

import java.io.*;
import java.net.*;

public class Connection implements Runnable {
	private Socket socket;

	Connection(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			System.out.println(reader.readLine());

			File file = new File("C:\\temp\\music.mp3");
			byte[] buffer = new byte[(int) file.length()];

			try {
				FileInputStream input = new FileInputStream(file);
				input.read(buffer);
				input.close();
				socket.getOutputStream().write(buffer);

			// Alternative way of sending data. Same result, though.
				//OutputStream output = new BufferedOutputStream(socket.getOutputStream());
				//output.write(buffer);
				//output.flush();
				//output.close();
			} catch (IOException e) {
				System.out.println("Connection.run()");
				System.err.println(e);
			}

			socket.close();
		} catch (IOException e) {
			System.err.println("Connection.run()");
			System.err.println(e);
		}
	}
}

-------------------------------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class GUIClient extends JFrame {
	private static final int	WINDOW_WIDTH = 320,
								WINDOW_HEIGHT = 200;
	private static final int	SERVER_PORT = 1979;
	private static final String	SERVER_ADDRESS = "localhost";

	GUIClient() {
		this.setLayout(null);
		this.setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);

		JButton button = new JButton("Download");
		button.setBounds(10, 10, 100, 25);
		add(button);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Thread() {
					public void run() {
						try {
							Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
							InputStream input = new BufferedInputStream(socket.getInputStream());

							// Eventually this will be used to send the filename to receive back
							PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
							writer.println("Requested Filename");

							ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, "Downloading...", input);
							ProgressMonitor pm = pmis.getProgressMonitor();
							pm.setMaximum(input.available());
							pm.setMillisToPopup(1);
							pm.setMillisToDecideToPopup(1);

							int i;

							while ((i = pmis.read()) != -1) {
								// Write data to disk...
							}

							pmis.close();
							writer.close();
							input.close();
							socket.close();
						} catch (IOException e) {
							System.out.println("GUIClient.button.actionPerformed()");
							System.err.println(e);
						}
					}
				}.start();
			}
		});
	}

	public static void main(String[] args) {
		new GUIClient();
	}
}
 
Don't know if passing a null as parent component to the ProgressMonitorInputStream constructor will upset it any...

Maybe you should also flush the server socket's OutputStream after you write to it.

Try it with a 'regular' InputStream implementation on the client rather than the ProgressMonitorInputStream.

Might be an idea to run the client through a debugger and step through it.

Tim
 
Thank you for replying. I've successfully passed in "null" as the parent to a ProgressMonitorInputStream before -- albeit, in local file I/O, non-socket, applications. So, I don't think that's causing any problems. I also wrote a simple threaded Java Web server once and "socket.getOutputStream().write(buffer);" worked without calling "flush();". I'll try removing the ProgressMonitor, and I'll keep debugging. Any further suggestions are certainly welcome.
 
I commented out the ProgressMonitor code and attempted to save the InputStream to a file. When "writer.close();" is placed after the code to write the file to disk, an exception is thrown:
GUIClient.button.actionPerformed()
java.net.SocketException: Connection reset

If I move the "writer.close();" statement to just after "writer.println("Requested Filename");", I get the following exception:

GUIClient.button.actionPerformed()
Connection.run()
java.net.SocketException: socket closed
java.net.SocketException: Software caused connection abort: socket write error

I figured that the socket connection was somehow being closed or "reset". This is proof of my hypothesis [sadeyes].

At this point I don't care what I have to change as long as I can get a progress indicator to display the download progress and be able to save the stream to a file!

Thanks
 
Well, I was able to get the data to transfer to the client and save it to a file. I plugged "java.net.SocketException: Connection reset" into Google and the description I found was that the client was probably closing the connection. So, I get rid of socket.close(); in GUIClient and made sure that writer.close(); and input.close(); weren't called until after I write the file to the disk. I suppose that writer.close(); closes more than just the output stream... apparently it's the entire connection. The ProgressMonitor still doesn't appear, but this may be due to the fact that the 2MB file is being transfered too quickly on my local machine. I'm still trying to figure this all out.[ponder]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top