客户端服务器程序中用于衡量视频文件上传和下载时间的时间因素

时间:2017-04-15 17:40:03

标签: java sockets networking

我已经完成了下载和上传视频(媒体)文件的客户端服务器程序,但我无法弄清楚如何分别计算上传,下载时间并重复运行以及时测量改进?我将在一个IP和不同IP的服务器上运行客户端。

客户的源代码;

package wdc;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.net.*;

class TCPClient {
    public static void main(String args[]) {
        byte[] aByte = new byte[1];
        int bytesRead;
        Socket clientSocket = null;
        InputStream is = null;
        try {
            clientSocket = new Socket("127.0.0.1", 3248);
            is = clientSocket.getInputStream();
        } catch (IOException ex) {
            // Do exception handling
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (is != null) {
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            try {
                fos = new FileOutputStream("C:\\file1.avi");
                bos = new BufferedOutputStream(fos);
                bytesRead = is.read(aByte, 0, aByte.length);
                do {
                    baos.write(aByte);
                    bytesRead = is.read(aByte);
                } while (bytesRead != -1);
                bos.write(baos.toByteArray());
                bos.flush();
                bos.close();
                clientSocket.close();
            } catch (IOException ex) {
                // Do exception handling
            }
        }
    }
}

服务器端的源代码;

package wds;

import java.io.*;
import java.net.*;

class TCPServer {
    public static void main(String args[]) {
        while (true) {
            ServerSocket welcomeSocket = null;
            Socket connectionSocket = null;
            BufferedOutputStream outToClient = null;
            try {
                welcomeSocket = new ServerSocket(3248);
                connectionSocket = welcomeSocket.accept();
                outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
            } catch (IOException ex) {
                // Do exception handling
            }
            if (outToClient != null) {
                File myFile = new File("C:\\file2.avi");
                byte[] mybytearray = new byte[(int) myFile.length()];
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(myFile);
                } catch (FileNotFoundException ex) {
                    // Do exception handling
                }
                BufferedInputStream bis = new BufferedInputStream(fis);
                try {
                    bis.read(mybytearray, 0, mybytearray.length);
                    outToClient.write(mybytearray, 0, mybytearray.length);
                    outToClient.flush();
                    outToClient.close();
                    connectionSocket.close();
                    // File sent, exit the main method
                    return;
                } catch (IOException ex) {
                    // Do exception handling
                }
            }
        }
    }
}

0 个答案:

没有答案