Reputation:
Ok, so I have written my very simple JAVA ftp server. I now want to connect to it, on the same machine. I am using ubuntu 11.10. I keep trying to use the command "ftp localhost" but I keep getting connection refused. I have been searching and it looks like I need to install a ftp server??....
I am asking if this is what I should do, if so which one and where can I find it. Or am I just not using the ftp command right?
Source Code:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FTPserver
{
public static void main(String [] args)
{
if (args.length != 1)
throw new IllegalArgumentException( "Parameter(s): <Port>");
int threadPoolSize = 10;
int port = Integer.parseInt(args[0]);
final ServerSocket server;
try
{
server = new ServerSocket(port);
}
catch (IOException e1)
{
return;
}
ExecutorService exec = Executors.newFixedThreadPool(threadPoolSize);
while (true)
{
try
{
Socket sock = server.accept();
exec.submit(new FTPProtocol(sock));
}
catch (IOException e)
{
System.err.println(e.getMessage());
return;
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
class FTPProtocol implements Runnable
{
static String greeting = "220 Service Ready.\r\n";
static String needPassword = "331 User name ok, need password.\r\n";
static String closing = "421 Service not available, closing control connection.\r\n";
static byte[] reply220 = null;
static byte[] reply331 = null;
static byte[] reply421 = null;
Socket sock = null;
public FTPProtocol(Socket so)
{
sock = so;
reply220 = greeting.getBytes();
reply331 = needPassword.getBytes();
reply421 = closing.getBytes();
}
public void run()
{
handleFTPClient(sock);
}
void handleFTPClient(Socket sock)
{
InputStream is = null;
OutputStream os = null;
byte[] inBuffer = new byte[1024];
try
{
is = sock.getInputStream();
os = sock.getOutputStream();
os.write(reply220);
int len = is.read(inBuffer);
System.out.write(inBuffer, 0, len);
os.write(reply331);
len = is.read(inBuffer);
System.out.write(inBuffer, 0, len);
os.write(reply421);
sock.close();
}
catch (IOException e)
{
System.err.println(e.getMessage());
return;
}
}
}
Upvotes: 1
Views: 744
Reputation: 75426
Under Linux you cannot use port 21 unless you are root. Instead bind to e.g. 2121 and use a client that allow you to specify the port number.
Upvotes: 1