SERVER :
import java.io.*;
import java.net.*;
class FTPserver
{
public static void main(String[] args)
{
ServerSocket server;
Socket s;
BufferedReader fromsoc,fromfile;
PrintStream tosoc;
String line;
FileInputStream fis;
try
{
server=new ServerSocket(6020);
System.out.println("server waiting");
s=server.accept();
System.out.println("connected");
fromsoc=new BufferedReader(new InputStreamReader(s.getInputStream()));
line=fromsoc.readLine();
fis=new FileInputStream(line);
fromfile=new BufferedReader(new InputStreamReader(fis));
tosoc=new PrintStream(s.getOutputStream());
while((line=fromfile.readLine())!=null)
{
tosoc.println(line);
System.out.println(line);
} tosoc.println("exit");
server.close();
s.close();
fis.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
CLIENT :
import java.io.*;
import java.net.*;
class FTPclient
{
public static void main(String[] args)
{
Socket s;
BufferedReader fromsoc,fromuser;
PrintStream tosoc,toFile;
String line;
FileOutputStream fos;
try
{
s=new Socket(InetAddress.getByName("localhost"),6020);
System.out.println("enter filename:\n");
fromuser=new BufferedReader(new InputStreamReader(System.in));
line=fromuser.readLine();
tosoc=new PrintStream(s.getOutputStream());
tosoc.println(line);
fromsoc=new BufferedReader(new InputStreamReader(s.getInputStream()));
fos=new FileOutputStream("new.txt");
toFile=new PrintStream(fos);
while(!(line=fromsoc.readLine()).equals("exit"))
{
toFile.println(line);
System.out.println(line);
}
s.close();
fos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment