开发者

File transfer through Socket in java

I'm making a Network File Transfer System for transfering any kind of file over a network in java. The size also could be of any kind. Therefore I've used UTF-8 protocol for the task.

I'm providing the codes which I've made but the problem is some times the file gets transfered as it is, with no problem at all. But sometimes few kb's of data is just skipped at the receiving end, which actually restricts the mp3/video/image file to be opened correctly. I think the problem is with BUFFER. I'm not creating any buffer which, right now, I think may be of some use to me.

I would really appreciate if anyone could provide any help regarding the problem, so that the file gets transferred fully.

Client side : --->> File Sender

Socket clientsocket = new Socket(host,6789);                        // host contains the ip address of the remote server
DataOutputStream outtoserver = new DataOutputStream(clientsocket.getOutputStream());
try
{
    int r=0;
    FileInputStream fromFile1 = new FileInputStream(path);                  // "path" is the of the file being sent.
    while(r!=-1)
    {   
         r = fromFile1.read();
         outtoserver.writeUTF(r+"");
    }
}
catch(Exception e)
{
    System.out.println(e.toString());
}
clientsocket.close();

Server side: --->> File Receiver

ServerSocket welcome = new ServerSocket(6789);
Socket conn = welcome.accept();
try
{
    String r1 = new String();
    int r=0;
    FileOutputStream toFile1 = new FileOutputStream(path);                  // "path" is the of the file being received.
    BufferedOutputStream toFile= new BufferedOutputStream(toFile1);
    DataInputStream recv = new DataInputStream(conn.getInputStream());
    while(r!=-开发者_高级运维1)
    {   
        r1 = recv.readUTF();
        r = Integer.parseInt(r1);
        toFile.write(r);
    }
}
catch(Exception e)
{
    System.out.println(e.toString());
}


I don't understand why you are encoding binary data as text.

Plain sockets can send and receive streams of bytes without any problems. So, just read the file as bytes using a FileInputStream and write the bytes to the socket as-is.

(For the record, what you are doing is probably sending 3 to 5 bytes for each byte of the input file. And you are reading the input file one byte at a type without any buffering. These mistakes and others you have made are likely to have a significant impact on file transfer speed. The way to get performance is to simply read and write arrays of bytes using a buffer size of at least 1K bytes.)


I'm not sure of this, but I suspect that the reason that you are losing some data is that you are not flushing or closing outtoserver before you close the socket on the sending end.

FOLLOW UP

I also noticed that you are not flushing / closing toFile on the receiver end, and that could result in you losing data at the end of the file.


The first problem I see is that you're using DataInputStream and DataOutputStream. These are for reading/writing primitive Java types (int, long etc), you don't need them for just binary data.

Another problem is that you're not flushing your file output stream - this could be causing the lost bytes.


An explicit flush might help the situation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜