-
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();
}
}
-
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.
-
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.
-
Similar Threads
-
By blindasfcuk in forum Java
Replies: 0
Last Post: 04-11-2005, 04:11 PM
-
By Alan Shiers in forum Java
Replies: 1
Last Post: 12-17-2001, 08:37 AM
-
By Duane Snelling in forum VB Classic
Replies: 0
Last Post: 12-06-2000, 12:26 PM
-
By Brad Watkins in forum VB Classic
Replies: 6
Last Post: 11-03-2000, 11:47 AM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|