FileServer work

git-svn-id: svn://anubis/gvsu@33 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-03-08 20:29:51 +00:00
parent a9d87cb211
commit b86758e2a9
2 changed files with 55 additions and 5 deletions

View File

@ -8,11 +8,14 @@ public class FileServer implements Runnable
private int m_port; private int m_port;
private String m_rootPath; private String m_rootPath;
private ServerSocket m_serverSocket; private ServerSocket m_serverSocket;
private char m_pathSeparator = '/';
public FileServer(int port, String rootPath) public FileServer(int port, String rootPath)
{ {
m_port = port; m_port = port;
m_rootPath = rootPath; m_rootPath = rootPath;
if (rootPath.indexOf('\\') >= 0)
m_pathSeparator = '\\';
} }
public void run() public void run()
@ -46,6 +49,48 @@ public class FileServer implements Runnable
public void run() public void run()
{ {
try {
processRequest();
} catch (Exception e) { }
}
private void processRequest() throws Exception
{
DataOutputStream os = new DataOutputStream(
m_socket.getOutputStream());
BufferedReader br = new BufferedReader(
new InputStreamReader(
m_socket.getInputStream()));
/* read the file name the client is requesting */
String fileName = br.readLine();
String absFileName = m_rootPath + m_pathSeparator + fileName;
FileInputStream fis = null;
try {
fis = new FileInputStream(absFileName);
} catch (Exception e) { }
if (fis != null)
{
sendBytes(fis, os);
}
os.close();
br.close();
m_socket.close();
}
private void sendBytes(FileInputStream fis, OutputStream os)
throws Exception
{
byte[] buff = new byte[1024];
int bytes = 0;
while ((bytes = fis.read(buff)) != -1)
{
os.write(buff, 0, bytes);
}
} }
} }
} }

View File

@ -9,18 +9,23 @@ public class KaZaClient
private String m_sharedFolder; private String m_sharedFolder;
private boolean m_connected = false; private boolean m_connected = false;
private Socket m_socket;
public KaZaClient(String userName, int kbps, public KaZaClient(String userName, int kbps,
String sharedFolder, String server) String sharedFolder, String server)
{ {
m_sharedFolder = sharedFolder; m_sharedFolder = sharedFolder;
// TODO: connect
// IF (CONNECTED SUCCESSFULLY) try {
{ m_socket = new Socket(server, KaZaServer.LISTEN_PORT);
} catch (Exception e) {
return;
}
// TODO: publish our name and file list
Thread fsThread = new Thread(new FileServer(LISTEN_PORT, m_sharedFolder)); Thread fsThread = new Thread(new FileServer(LISTEN_PORT, m_sharedFolder));
m_connected = true; m_connected = true;
} }
}
public boolean connected() { return m_connected; } public boolean connected() { return m_connected; }
} }