TheInsider
Programmer
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
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();
}
}