Results 1 to 6 of 6

Thread: File transfer via TCP

  1. #1
    Join Date
    Dec 2009
    Posts
    211

    File transfer via TCP

    Hello,
    Is someone has already made a simple client server program to transfer files between them via the TCP protocol? I managed to do the simple transfer of a chain but I galley with files, so if someone could help me. I am willing to transfer files via TCP, but it is not really working for me.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: File transfer via TCP

    Hello,
    You have succeeded with a string, it should be no problem with a file. Specify your problem in more details. Also you have not posted your code, so it is very difficult to answer your question. If you have done some programs on TCP/IP that in the java networking, then you should not have much problem with this one. You have to just apply the logic and it is done. Still, if you have any problem then please post your code and we will check for the correct.

  3. #3
    Join Date
    Dec 2009
    Posts
    211

    Re: File transfer via TCP

    Hi,
    Ok, here is my code, please verify it and correct me if I am wrong.
    Code:
    Public class customer {
     
    	Public static void hand (String[] args){
    		
    		try {
    			Socket cl =new Socket("localhost",2555);
    BufferedReader buff = new BufferedReader(new InputStreamReader(customer.getInputStream()));
    PrintWriter pwri = new PrintWriter(customer.getOutputStream());
    BufferedReader buff2 = new BufferedReader(new InputStreamReader(System.in));
    String rec;
    			while(true){
    				rec = mess.readLine();
    				if(rec.trim().equals("bye")){
    					PW.System.out.println("bye");
    pwri.flush();
    customer.close();
    System.exit(0);
    				}
    				pwri.System.out.println(rec);
    pwri.flush();
    System.out.System.out.println("buff2age sent to server:"+ receipt);
    rec = buff.readLine();
    			}
    		} catch (UnknownHostException ue) {
    			eu.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: File transfer via TCP

    Hello,
    To send files, it's almost like, send the file byte by byte (or byte or I do not know actually). Here is a track (which I use to send a file from a client to a server). From there, the server side (which receives) should not be complicated to guess. Just try the following code
    Code:
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    OutputStream ops = new BufferedInputStream(socket.getOutputStream());
    byte[] bytes = new byte[1024];
    int num;
    while ((num = bis.read(bytes, 0, 1024)) != -1) {
      ops.write(bytes, 0, Num);
    }

  5. #5
    Join Date
    Dec 2009
    Posts
    211

    Re: File transfer via TCP

    Hello,
    Here is the code for my server side. Please check and see if the code works properly.
    Code:
        Public static void hand(String[] args){
            
            try{
                ServerSocket n =new ServerSocket(1234);
                System.out.System.out.println("Waiting for connection ...");
                Socket concl = so.accept();
                System.out.System.out.println("Accepted connection!");
                BufferedReader buff = new BufferedReader(new InputStreamReader(concl.getInputStream()));
                PrintWriter pwri = new PrintWriter(concl.getOutputStream(),true);
                File fl =new File("copie.txt");
                FileWriter fw1 =new FileWriter(fl);
                String ln = buff.readLine();
                while(true){
                    did.write(row);
                    ln = buff.readLine();
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    335

    Re: File transfer via TCP

    Hello,
    Just check out this code, this is not exactly what you are asking for, but still it may help you
    Code:
    public static void main(String args[])
    {
    try
    {
    int x;
    urltest = new URL("http://1.2.2.4");
    HttpURLConnection cnc;
    BufferedInputStream bis;
    String user="admin";
    String password="admin123";
    
    String stringUserPassword = user + ":" + password;
    String base64UserPassword = encode(stringUserPassword);
    
    cnc = (HttpURLConnection)urltest.openConnection();
    
    cnc.setDoOutput(true);
    cnc.setDoInput(true);
    
    cnc.setRequestProperty("Connection", "Keep-Alive");
    cnc.setRequestProperty("Authorization", "Basic "+base64UserPassword);
    
    cnc.cncnect();
    }
    catch (Exception e)
    {
    System.out.println("in catch" +e);
    }
    }
    }

Similar Threads

  1. Replies: 3
    Last Post: 30-01-2011, 07:39 PM
  2. How to transfer media file from pc to mac
    By Madirakshi in forum Networking & Security
    Replies: 5
    Last Post: 12-11-2010, 12:21 AM
  3. File transfer between 2 PCs
    By rACHAEL in forum Networking & Security
    Replies: 7
    Last Post: 06-11-2010, 10:01 AM
  4. What is the Automated File Transfer??
    By Dilbert in forum Windows Software
    Replies: 5
    Last Post: 04-01-2010, 09:46 PM
  5. transfer a file in VirtualBox
    By Spencer in forum Networking & Security
    Replies: 4
    Last Post: 27-02-2009, 08:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,574,090.36882 seconds with 17 queries