Sending files over sockets


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Sending files over sockets

  1. #1
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Question Sending files over sockets

    I am trying to write a simple file server and a client.

    I am able to send the file, but I want to be able to send information about the file I will be sending before I actually send it so it can be verified at the other end. Does anyone have any ideas, or code they can contribute?

    Here is my code

    Server
    Code:
    import java.io.*;
    import java.net.*;
    
    class Server
    {
    	public static void main(String args[])throws Exception
    	{
    		File transfer = new File("C://Users.txt");
    		InputStream in = new FileInputStream(transfer);
    		ServerSocket server = new ServerSocket(440);
    		Socket connection = server.accept();
    		
    		OutputStream output = connection.getOutputStream();
    		
    		byte[] buff = new byte[connection.getSendBufferSize()];
    		int bytesRead = 0;
    		
    		System.out.println(transfer.length()+ " bytes");
    		
    		while((bytesRead = in.read(buff))>0)
    		{
    			output.write(buff,0,bytesRead);
    		}
    		in.close();
    		server.close();
    		connection.close();
    		output.close();
    	}
    }
    Client
    Code:
    import java.io.*;
    import java.net.*;
    
    class Server
    {
    	public static void main(String args[])throws Exception
    	{
    		Socket connection = new Socket("127.0.0.1",440);
    		InputStream input = connection.getInputStream();
    		FileOutputStream wr = new FileOutputStream(new File("C://Test.txt"));
    		byte[] outBuffer = new byte[connection.getReceiveBufferSize()];
    		int bytesReceived = 0;
    		
    		while((bytesReceived = input.read(outBuffer))>0)
    		{
    			wr.write(outBuffer,0,bytesReceived);
    		}
    		wr.close();
    		input.close();
    		connection.close();
    	}
    }

  2. #2
    Join Date
    Oct 2005
    Posts
    107
    Come up with a simple protocol like maybe you could send the type, name and size of the file before it's sent to the client. Then when it's done verify the size of the file and rename it.

    I had some old C++ code that would do something similar. I would send '\0' (null) to seperate the commands. Here's some code snippets...

    Example how the server would read until null was at the end of the inputstream.
    Code:
        while((n = recv(sd2, buf, sizeof(buf), 0)) > 0){
          if(buf[n-1] == '\0'){
    	if(n > 1){
    	  write(textfd, buf, n -1);
    	}
    	break;
          }else{
     	write(textfd, buf, n);
          }
        }
    Example how the client would send the text to the server followed by null
    Code:
    /* text entered at prompt, send the buffers contents followed by NULL*/
          send(socket, tbuf, strlen(tbuf), 0);
          send(socket, "\0", 1, 0);
    Sorry it's in C++. But I think you were stuck at how to send more than one thing to the client. So you could send the file info followed by null then send the file. This way the client can seperate the info from the raw file bytes. Or you could always do it by making the the first x number of bytes contain the file info then the rest is the actual file. It's not as dynamic but it is simple.
    Last edited by Joe Beam; 12-12-2005 at 04:43 AM.

  3. #3
    Join Date
    May 2010
    Posts
    1

    Thumbs up sending larger files

    i am doing project.There i wanted to send larger files(less than 10MB).but i was only able to send very small files.but the solution I found from you.Thanks in advance.

  4. #4
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    Hi Wizard, you maybe intrested in this http://www.snippetbank.net/detail/sn...ng-Socket.html

Similar Threads

  1. Sending files over a network
    By blindasfcuk in forum Java
    Replies: 0
    Last Post: 04-11-2005, 05:11 PM
  2. uploading files over sockets
    By Alan Shiers in forum Java
    Replies: 1
    Last Post: 12-17-2001, 08:37 AM
  3. MSCOMM sending files with Ymodem
    By Duane Snelling in forum VB Classic
    Replies: 0
    Last Post: 12-06-2000, 12:26 PM
  4. Sending files through winsock
    By Brad Watkins in forum VB Classic
    Replies: 6
    Last Post: 11-03-2000, 11:47 AM
  5. Sending files through winsock
    By Brad Watkins in forum VB Classic
    Replies: 0
    Last Post: 11-02-2000, 06:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center