aspose file tools*
The moose likes I/O and Streams and the fly likes Sending File from Server to Client..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Sending File from Server to Client....." Watch "Sending File from Server to Client....." New topic
Author

Sending File from Server to Client.....

Francis John
Ranch Hand

Joined: Sep 09, 2003
Posts: 33
Hello to all....
Can anyone favor me to help about how i can send file from server to client(RMI)...
ok
Bye


What is defeat? Nothing but education, Nothing but the first step to something better.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Sure. Here's the RMI tutorial. Your assignment sounds pretty easy, so a simple interface that passes a chunk of data should work fine. Then look over the IO Tutorial. Again, all you need is the File Streams part.
Don't try to do it all at once. Once you have a program that sends a message over RMI and one program that can copy a file from one directory to another it shouldn't take much to put those two together and make a file transfer program.
[ December 17, 2003: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Not to belabor the obvious, but... Open the file, read the data, pass it as an argument of some kind to one or more RMI calls, close the file. On the client end, receive the calls, save the data to a file.
If you were looking for a method "sendFileToClient()", there isn't one; but once you've already got RMI set up, doing what I've just described is simple enough.


[Jess in Action][AskingGoodQuestions]
Francis John
Ranch Hand

Joined: Sep 09, 2003
Posts: 33
Hello Sir...
I ve understood a lot but i m facing problems in implementing it if u will help me by describing with an example then it will a great help for me....
ok
Take Care
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Have you gone through the tutorials that Joe described? If not, do. Otherwise, let's see what code you've got so far.
Francis John
Ranch Hand

Joined: Sep 09, 2003
Posts: 33
Hello!!!
I will first tell u the mechanism then it will b ez for u to understand.......
Here,4 classes related to RMICallbackSever,RMICallbackServerImpl,RMICallbackClient & RMICallbackClientImpl work in the same manner as RMI Calls work.....but the problem which i m facing is that I ve sequences in one file then i m reading that sequences from a file n break into 3 equal files named as client1,...,client3, by using SequenceReader class ...
But i ve to implement distribute method by server so that it will distribute the different files to different registered clients...n then registered client used algorithm by using dosimMatch n then in the last result will be collected........
So plz help me in this regard..How i can implement distribute method by sending different files to different registered clients........
Take care..
Here are the all five classes till yet..
import java.rmi.*;
// It's an interface used by server
public interface RMICallbackServer extends Remote{
public static final String registry_name="CallbackServer";
public abstract void register(RMICallbackClient client) throws RemoteException; // This is the method for registration of clients..
public abstract void deregister(RMICallbackClient client) throws RemoteException; // This is the method for deregistration of clients...
public abstract void say(String message) throws RemoteException;
//public abstract void distribute(String st1,String st2) throws RemoteException; // This is the method for distribution for different files to registered clients....
// This is the implementation class of RMICallbackServer
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.util.*;
import java.io.*;
public class RMICallbackServerImpl extends UnicastRemoteObject implements RMICallbackServer{
public RMICallbackServerImpl() throws RemoteException
{
tables= new Hashtable();
count=0;
str1="ACKETKDISAAXZIML";
str2="ADKKLAKLKLIWEOEWL";

}
public void register(RMICallbackClient client)
{
try{
count=count+1;
key= String.valueOf(count);
tables.put(key,getClientHost());
say(getClientHost() + " has Joined");
}
catch(ServerNotActiveException ignored){
}
clients.addElement(client);
System.out.println("Total Clients:" + count);
System.out.println(tables.toString());
clients=tables.keys();
while(clients.hasMoreElements())
{
st=(String) clients.nextElement();
System.out.println(st + " " + tables.get(st));
}

}
public void deregister(RMICallbackClient client) {
try{
say(getClientHost() + " has left.");
count=count-1;
System.out.println("Total Clients:" + count);
tables.remove(key);
}
catch(ServerNotActiveException ignored){}
}
public void say(String message)
{
System.out.println(message);
}
public void distribute(String str1,String str2)
{
//System.out.println("HASHTABLE:"+tables);
//SequenceReader sr=new SequenceReader();
//sr.loadBalance();

}
public static void main(String[] args) throws RemoteException
{
RMICallbackServerImpl callbackServer=new RMICallbackServerImpl();
Registry registry=LocateRegistry.getRegistry();
registry.rebind(registry_name,callbackServer);
}

protected static Hashtable tables;
protected static int count;
protected static Object key;
protected static String result;
protected static String str1,str2,st;
protected long time,times;
}
}
// This is the interface for Client....
import java.rmi.*;
public interface RMICallbackClient extends Remote{
public abstract String doSimMatch(String s1,String s2) throws RemoteException;
}
// This is the class for Client........
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class RMICallbackClientImpl extends UnicastRemoteObject
implements RMICallbackClient
{
protected String host;
protected double result;
protected int[][] sim;
protected int gap = -1;
protected int match = 1;
protected int mismatch = -1;
protected int len1, len2;
protected String s1, s2,output;
protected RMICallbackServer server;

public RMICallbackClientImpl(String Host) throws RemoteException
{
this.host=host;
}
public synchronized void start() throws RemoteException,NotBoundException
{
if(server== null){
Registry registry=LocateRegistry.getRegistry(host);
server=(RMICallbackServer) registry.lookup (RMICallbackServer.registry_name);
server.register(this);
sleep(500);
server.distribute(server.str1,server.str2);
}
}
public synchronized void stop() throws RemoteException
{
RMICallbackServer server=this.server;
this.server=null;
if(server !=null)
server.deregister(this);
}

public String doSimMatch(String s1,String s2)
{
len1 = s1.length();
len2 = s2.length();
this.s1 = s1;
this.s2 = s2;
sim = new int[len1+1][len2+1];
int i,j;
for ( i=0; i<=len1; i++)
sim[i][0] = i*gap;
for ( j=0; j<=len2; j++)
sim[0][j] = j*gap;
for (i = 1; i<=len1; i++)
for (j = 1; j<=len2; j++)
if (s1.charAt(i-1) == s2.charAt(j-1))
sim[i][j] = maximum(sim[i-1][j]+gap,sim[i-1][j-1]+match,
sim[i][j-1]+gap);
else
sim[i][j] = maximum(sim[i-1][j]+gap,sim[i-1][j-1]+mismatch,
sim[i][j-1]+gap);
return(" "+sim[len1][len2]);
}
static int maximum(int i, int j, int k)
{
return Math.max(i,Math.max(j,k));
}
public static void main(String[] args) throws RemoteException,NotBoundException
{
if(args.length != 1)
throw new IllegalArgumentException
("Syntax:RMICallbackClientImpl <host>");
RMICallbackClientImpl callbackClient=new RMICallbackClientImpl("127.0.0.1");
callbackClient.start();
}
}
// This is the class for Reading Sequence..........
import java.util.StringTokenizer;
import java.io.*;
import java.util.*;
public class SequenceReader
{

public void loadBalance()
{
//String str1="DKDAIELS";
try
{

BufferedReader in = new BufferedReader(new FileReader("se.txt"));
BufferedWriter bw;
String currLine= new String();
String name;
String fileNameRoot = "CLIENT";
SequenceReader sr=new SequenceReader();
while ( (currLine = in.readLine()) != null )
{
StringTokenizer tokens=new StringTokenizer(currLine);

num=tokens.countTokens();
//System.out.println(num);
count=num/3;
//System.out.println("String For Matching "+ str1);
for(int i=0;i<3;i++)
{
name = fileNameRoot + (i+1) + ".TXT";
bw = new BufferedWriter(new FileWriter(name));
System.out.println("Client" + (i+1));
while (tokens.hasMoreTokens() && count!=0) {
//System.out.println(tokens.nextToken());

String tn=tokens.nextToken();
bw.write(tn);
bw.write(" ");
count=count-1;

}
bw.close();
count=num/3;
//System.out.println(num);
}

}
in.close();
}
catch (IOException e) { System.err.println(e); }
}
}
Francis John
Ranch Hand

Joined: Sep 09, 2003
Posts: 33
Hello Mr.Ernest Friedman-Hill
U ven't give me the answer of my question.plz reply it i m waiting for ur response...I have very less time remaining for completing my work..so plz help me in this regar....
Thanx a lot
Bye
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
OK I'll put spcl cll 4 'im 2 cum prtty dmm quck bk 'ear to fx it 4 u.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I stk cde in eclpse, it dnt cmple. Lotsa cmpl errs. Instnce vbls not dfned. Lda crp I rkn. Srry mst go, bss is lkng ovr me shldr...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

As Barry telegraphically relates, this code isn't even close to compiling.
It's complex and confusing and too much work for me to try to understand. In the future, please use the UBB "CODE" tag to post your code, so that indentation is preserved
It never makes sense to write a whole bunch of broken code, then work to get it to compile, and then finally work to debug it. Start small, take small steps, compile and test at every point, make sure you understand what the code is doing. You'll add new code more slowly, but you'll end up with working code in much less time.
Here's what I'd recommend: throw all this code out. Start over from the RMI tutorial code. Make the minimum modifications such that you have a server that can send a specified file to a single client, and the client can save the file.
When that works, and you've verified that the identical file is received OK on the client, then put this code aside start working on a completely separate class that breaks one of your files up into sub-files. Again, start small, and add to the code a little at a time, testing as you go (automated testing is best, using something like JUnit).
When your file-breaker-upper works, then it's time to start combining the two. I really don't understand your requirements, but you do, so you will know how to combine these two chunks of code into one application.
Remember: take it slow. Do one step at a time. Test a lot. Make sure everything works before moving on to the next step. Make sure you understand every line of code you write.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
y.g.s.a.t.y.p.b!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Francis John
Ranch Hand

Joined: Sep 09, 2003
Posts: 33
Hello to All....
I know it's difficult to understand the code which is written by others and i m sorry that i cant explain u in right manner.
Now, I m again explaining my whole mechanism. Ok,help me in this regard i ve done by myself how to send file from server/client....dont worry abt that
but read it out that mechanism and help me in these things...
I m using RMI as for Client/Server connection in which client will give the request of some string which is basically a protein sequence....and server had a very large database..suppose one sequence of 500 length and having more than 100 sequences....so it should be divided in files and then those files are distributed over clients with the algorithm for matching....in my case it's dosimMatch(). Anyway, then after completing their tasks clients will return the result again to Server and the requested client will get the last result about which sequence is closet....
Now, tell me how i can implement it over Wan.....bcoz i m testing this system over Lan....so tell me in respect of Wan...i know the performance will degrade but i want to check this thing..so tell me Is RMI capable to do that work in Wan and This thing we refer to Grid Computing or Distributed Computing.....
i m waiting for ur response...
ok
Bye
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There's no formal difference between a LAN and a WAN; neither do you need to do anything special to work in one environment or the other unless there are firewalls, NAT boxes, or other special hardware. In most cases, these just place restrictions on what you can do; there's no real way to work around them.
The best thing to do is just don't even worry about it until you've got the application working on your desktop.
Francis John
Ranch Hand

Joined: Sep 09, 2003
Posts: 33
Hello Mr.Friedman-Hill
Now, it gives me a little bit relaxation from few confusions...but u ven't tell me about that what will be the mechanism to use in Wan i mean passing data from firewalls by using RMI....
and secondly, what we call it? it's a grid computing or Distributed computing....???
I just want to make my confusions clear..plz guide me ...
Take Care,
Bye
 
jQuery in Action, 2nd edition
 
subject: Sending File from Server to Client.....