Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-06-2007, 09:13 PM
Member
 
Join Date: Nov 2007
Posts: 1
Rep Power: 0
lada.r is on a distinguished road
Default New Protocol?
Hello, Iīm trying to send more files than 1 file from client to server via TCP. Server has to save the retrieved file to disc.
Code:
public class Client {
           connect(port, ip);
        doCopy(vstup_k);
 
    }
    public static void doCopy(  Scanner scann){
            while(file_number>0 ){
             if(accepted){
            System.out.println("Type absolute Path to the file");
            path=scann.nextLine();
        if( test_souboru(cesta)){
            copy(path,os); //os is outputstream
           file_number--;}
        }
            }
    
    }
  
    
    public static void connect(int portik, String ip_ad){
        try{
            socket = new Socket(ip_ad, portik);
            os = socket.getOutputStream();            
        } catch(IOException a){
            System.out.println(a.toString());
            System.exit(1);
        }
        
    }
    public static void copy(String path,OutputStream a) {
        accepted=false;
 
        try{
            File file = new File(path);
            
            file_in = new FileInputStream(file);
            DataOutputStream data_o=new DataOutputStream(a);
            data_in=new DataInputStream(socket.getInputStream());
            int lLength;
                
       
            data_o.writeUTF(file.getName());
                  while ((lLength =  file_in.read(buffer)) > 0) {
                a.write(buffer, 0, lLength);
            }
            String mess=data_in.readUTF();
            if(mess=="Accepted"){
            accepted=true;
            }
                 try{
         file_in .close();
            os.close();
        } catch(IOException ta){
                System.out.println(ta.toString());
            }
        } catch(IOException b){
            System.out.println(b.toString());
        }
  
    }
    
    
}
And server part
Code:
public class Server implements Runnable {
    
       public static void main(String[] _) {
        try {
            ServerSocket listener = new ServerSocket(port);
            
            while (true) {
                Server  file_rec = new Server();
                file_rec.socket = listener.accept();
                
                new Thread( file_rec ).start();
            }
        } catch (java.lang.Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
    
    public void run() {
        int asa=4; //this is variable which tells the server that 4 files will be transfer, I havenīt fixed the problem with sending server the number of files which are transfering from client
        while(asa>0){
            acceptAndSave();
            asa--;
            buffer=new byte[128];
        }
        try{
            out.close();
            in.close();
        }catch(IOException wq){
            System.out.println(wq.toString());
        }
    }
    public  void acceptAndSave(){
        String file_name="";
        boolean name=false;
        
        try {
            File file=new File("server_copy");
            in = socket.getInputStream();
            out=new FileOutputStream(file);
            data_i=new DataInputStream(socket.getInputStream());
            data_o=new DataOutputStream(socket.getOutputStream());
            int length;
            
            if(!name){
                file=new File(data_i.readUTF());
                System.out.println(file.getName());
                out = new FileOutputStream(file);
                file_name=file.getName();
                name=true;
            }
            
            while ((length = in .read(buffer)) >=0) {
                out.write(buffer,0, length);
            }
            name=false;
            System.out.println("Byl prijat soubor klientem a nazvan "+file_name);
            data_o.writeUTF("Accepted");
            
        } catch (java.lang.Exception ex) {
            ex.printStackTrace(System.out);
            
        }
        
    }
}
And now client sends 1 file and server accepts it and saves it on disk but after this both (client and server) are waiting for somethig (probably client doesnīt notice that server has sent message "Accepted" but I donīt know why, or mayebe Server hasnīt sent the messge ). Please could you help me?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with OSCAR protocol lenny Networking 1 08-07-2007 06:30 AM
how to use SIP protocol katie Advanced Java 1 08-06-2007 10:22 PM


All times are GMT +2. The time now is 03:41 AM.



VBulletin, Copyright Đ2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright Đ2006 - 2007, www.java-forums.org