如何编写发送字节数组的java客户端服务器

时间:2017-03-19 02:45:37

标签: java arrays sockets byte

我非常了解java套接字的工作原理。我已经制作了许多只发送纯文本的客户端 - 服务器应用程序。但是,我想知道如何建立一个能够反复发送字节数组的客户端 - 服务器关系。我正在制作文件共享程序(我已经将文件加载到字节数组中)您看到我当前的程序只接受客户端并发送文件。没有沟通。我希望客户端和服务器能够保持连接并在两者之间传输数据。例如:

*客户:我想要x文件

*服务器:好的......

*服务器:发送x文件

*客户:获取文件

*重复...

这是我目前的代码

客户端:

package Main;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class TransferClient {

    public static void main (String [] args ) throws IOException {
        int filesize = 500 * 1024 * 1024;
        int bytesRead;
        int currentTot = 0;
        Socket socket = new Socket("192.168.1.11",15123);//127.0.0.1
        byte[] bytearray = new byte [filesize];
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream("copy.mp4"); 
        BufferedOutputStream bos = new BufferedOutputStream(fos); 
        bytesRead = is.read(bytearray,0,bytearray.length);
        currentTot = bytesRead;
        long start = System.currentTimeMillis();
        System.out.println("Waiting for file... "+start);

        do{
            bytesRead = is.read(bytearray,currentTot,(bytearray.length-currentTot));
            if(bytesRead >= 0){currentTot += bytesRead;}
        } while(bytesRead > -1);
        bos.write(bytearray, 0, currentTot);
        bos.flush();
        bos.close();
        socket.close();
        long end = System.currentTimeMillis() - start;System.out.println("Got file "+end);
    }

}

服务器:

package Main;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TransferServer {
    public static void main (String [] args ) throws IOException { 
        ServerSocket serverSocket = new ServerSocket(15123); 

        while(true){
            Socket socket = serverSocket.accept(); 
            System.out.println("Accepted connection : " + socket); 
            File transferFile = new File ("send.mp4"); 
            byte [] bytearray = new byte [(int)transferFile.length()]; 
            FileInputStream fin = new FileInputStream(transferFile); 
            BufferedInputStream bin = new BufferedInputStream(fin); 
            bin.read(bytearray,0,bytearray.length); 
            OutputStream os = socket.getOutputStream(); 
            long startTime = System.currentTimeMillis();
            System.out.println("File Size: "+ bytearray.length);
            System.out.println("Sending Files..."); 
            os.write(bytearray,0,bytearray.length); 
            os.flush(); 
            socket.close(); 
            long totalTime  = (System.currentTimeMillis()-startTime)/1000;
            long mbps = (bytearray.length / 1024 /1024) / (totalTime+1);
            System.out.println("File transfer complete : "+totalTime+"/s" + " | Speed: "+mbps+"/mbps");
        }
    }

}

0 个答案:

没有答案