5

Possible Duplicate:
how to implement TCP server and TCP client in java to transfer files

I write application which will send files to server through socket. Its very important to all files came correct from client to server with no errors, lost data and other like that. To do that I need to use TCP protocol I think but I dont know how to do that. Is socket in Java is default using TCP. If not how can I send data through TCP? Thanks for any help and tip.

0

3 Answers 3

9

Yes, you can use TCP for this and yes, Java sockets can do TCP.

If I were you, I'd start by following this tutorial: http://download.oracle.com/javase/tutorial/networking/sockets/

Sign up to request clarification or add additional context in comments.

Comments

6

And here's a good example from this thread Java TCP socket: data transfer is slow

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Transfer {

    public static void main(String[] args) {
        final String largeFile = "/home/dr/test.dat"; // REPLACE
        final int BUFFER_SIZE = 65536;
        new Thread(new Runnable() {
                public void run() {
                        try {
                                ServerSocket serverSocket = new ServerSocket(12345);
                                Socket clientSocket = serverSocket.accept();
                                long startTime = System.currentTimeMillis();
                                byte[] buffer = new byte[BUFFER_SIZE];
                                int read;
                                int totalRead = 0;
                                InputStream clientInputStream = clientSocket.getInputStream();
                                while ((read = clientInputStream.read(buffer)) != -1) {
                                        totalRead += read;
                                }
                                long endTime = System.currentTimeMillis();
                                System.out.println(totalRead + " bytes read in " + (endTime - startTime) + " ms.");
                        } catch (IOException e) {
                        }
                }
        }).start();
        new Thread(new Runnable() {
                public void run() {
                        try {
                                Thread.sleep(1000);
                                Socket socket = new Socket("localhost", 12345);
                                FileInputStream fileInputStream = new FileInputStream(largeFile);
                                OutputStream socketOutputStream = socket.getOutputStream();
                                long startTime = System.currentTimeMillis();
                                byte[] buffer = new byte[BUFFER_SIZE];
                                int read;
                                int readTotal = 0;
                                while ((read = fileInputStream.read(buffer)) != -1) {
                                        socketOutputStream.write(buffer, 0, read);
                                        readTotal += read;
                                }
                                socketOutputStream.close();
                                fileInputStream.close();
                                socket.close();
                                long endTime = System.currentTimeMillis();
                                System.out.println(readTotal + " bytes written in " + (endTime - startTime) + " ms.");
                        } catch (Exception e) {
                        }
                }
        }).start();
    }
}

3 Comments

Except Java + TCP is not slow. I can transfer 2.6 GB/s over sockets on my PC. (In a real application, not just a micro-benchmark) For testing purposes you can write a client and server sockets without starting any new threads.
it is currently physically impossible to transfer 2.5 GB/s (GiB) over standard LAN-equipment, your claim is plain-old bullshit. That would require industry-level hardware, which you are quite unlikely to posess.
@specializt You're totally wrong. Standard LAN equipment can transfer 2.5 GB/s over TCP. Think about it for a minute or two and you'll see the incorrect assumption you've made.
2

Yes, Java can read and write a file over TCP.

http://download.oracle.com/javase/tutorial/networking/sockets/ is a good place to start learning about Java Sockets.

You will also need to read the documentation of these two packages

java.io

java.net

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.