java中的套接字和TCP

时间:2011-10-14 13:12:09

标签: java sockets tcp

  

可能重复:
  how to implement TCP server and TCP client in java to transfer files

我编写的应用程序将通过套接字将文件发送到服务器。它对所有文件非常重要,从客户端到服务器都是正确的,没有错误,丢失数据等等。要做到这一点,我需要使用TCP协议我想,但我不知道如何做到这一点。 Java中的套接字是否使用TCP是默认的。如果不是,我如何通过TCP发送数据?感谢您的帮助和提示。

3 个答案:

答案 0 :(得分:9)

是的,您可以使用TCP进行此操作,是的,Java套接字可以执行TCP。

如果我是你,我将从遵循本教程开始:http://download.oracle.com/javase/tutorial/networking/sockets/

答案 1 :(得分:6)

这是一个很好的例子来自这个帖子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();
    }
}

答案 2 :(得分:2)

是的,Java可以通过TCP读写文件。

http://download.oracle.com/javase/tutorial/networking/sockets/是开始学习Java套接字的好地方。

您还需要阅读这两个软件包的文档

java.io

java.net