English 中文(简体)
java[复制]的袖珍和TCP
原标题:Socket and TCP in java [duplicate]
This question already has answers here:
Closed 11 years ago.

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

我写了申请,将档案通过袖珍发送服务器。 它对所有档案非常重要,从客户到服务器都是正确的,没有错误、数据丢失等等。 我认为,为了做到这一点,我需要使用《非加太议定书》,但我很想知道如何这样做。 贾瓦省正在使用TCP违约。 如果我无法通过结核病防治方案发送数据? 得到任何帮助和帮助。

最佳回答

这里有一个很好的例子:。 Java TCP的袖珍:数据转让缓慢

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();
    }
}
问题回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签