Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多个文件块通过套接字传输到多个客户端_Java_Sockets - Fatal编程技术网

Java 多个文件块通过套接字传输到多个客户端

Java 多个文件块通过套接字传输到多个客户端,java,sockets,Java,Sockets,我必须使用一台服务器将文件块传输到不同的客户端。 当我运行服务器文件并提供文件名时,它会成功地生成块。当我第一次运行第一个客户机时,它可以工作,但是当我再次为客户机运行它时(我的意思是当我作为第二个客户机连接时),它无法将块传输到第二个客户机。服务器和客户端的完整代码如下所示。 错误是第二个客户端开始读取文件名时的文件内容,程序终止。 提供一个大文本文件(1MB)作为服务器的输入 服务器代码: import java.io.*; import java.net.ServerSocket; imp

我必须使用一台服务器将文件块传输到不同的客户端。 当我运行服务器文件并提供文件名时,它会成功地生成块。当我第一次运行第一个客户机时,它可以工作,但是当我再次为客户机运行它时(我的意思是当我作为第二个客户机连接时),它无法将块传输到第二个客户机。服务器和客户端的完整代码如下所示。 错误是第二个客户端开始读取文件名时的文件内容,程序终止。 提供一个大文本文件(1MB)作为服务器的输入

服务器代码:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

public class server {

    private static final int sPort = 8000;   //The server will be listening on this port number
    public static String str;
    public static int c;

    public static void main(String[] args) throws Exception {
        System.out.println("The server is running."); 
        ServerSocket listener = new ServerSocket(sPort);
        int clientNum = 1;
        System.out.println("Enter the name of the file: ");
        Scanner in = new Scanner(System.in);
        str = in.nextLine();
        String path = System.getProperty("user.dir");
        String filepath = path +"/"+ str;
        in.close();
        try {
            c=splitFile(new File(filepath));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
            try {
                    while(true) {
                        new Handler(listener.accept(),clientNum,c).start();
                System.out.println("Client "  + clientNum + " is connected!");
                clientNum++;
                        }
            } finally {
                    listener.close();
            } 

        }

    /**
        * A handler thread class.  Handlers are spawned from the listening
        * loop and are responsible for dealing with a single client's requests.
        */
        private static class Handler extends Thread {

        private Socket connection;
        private int chunkcount;
            private ObjectInputStream in;   //stream read from the socket
            private ObjectOutputStream out;    //stream write to the socket
        private int no;     //The index number of the client

            public Handler(Socket connection, int no,int c) {
                this.connection = connection;
                this.no = no;
                this.chunkcount=c;
            }

        public void run() {
        try{
            //initialize Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            try{
                String path = System.getProperty("user.dir");
                path=path+"/"+"chunks"+ "/";

                System.out.println(path);

                System.out.println("Total chunks: "+chunkcount);

                int i=no;
                int j=i;
                int k=0;
                 OutputStream op=connection.getOutputStream();
                 DataOutputStream d = new DataOutputStream(op);

                 d.writeInt(no);
                 d.flush();
                 System.out.println("value of j or clientnum: "+j);
                 while(j<chunkcount)
                 {
                     k++;
                     j=j+5;
                 }

                 System.out.println(k);
                 d.writeInt(k);
                 d.flush();
                 //d.close();

                 while(i<chunkcount)
                    {
                         String pathname= path+Integer.toString(i)+str;
                         System.out.println(i+str);

                         sendFile(connection,pathname);
                         i=i+5;

                    }
            }
            catch(Exception e){
                    e.printStackTrace();
                }
        }
        catch(IOException ioException){
            System.out.println("Disconnect with Client " + no);
        }
        finally{
            //Close connections
            try{
                in.close();
                out.close();
                connection.close();
            }
            catch(IOException ioException){
                System.out.println("Disconnect with Client " + no);
            }
        }
    }

        }       

        public static int splitFile(File f) throws IOException {
            int partCounter = 1;//I like to name parts from 001, 002, 003, ...
                                //you can change it to 0 if you want 000, 001, ...

            int sizeOfFiles = 102400;// 1MB
            byte[] buffer = new byte[sizeOfFiles];

            try (BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(f))) {//try-with-resources to ensure closing stream
                String name = f.getName();
                String path = f.getParent();
                long sizefile = f.getTotalSpace();
                String newpath = path + "/" + "chunks";
                File dir = new File(newpath);
                dir.mkdir();

                int tmp = 0;
                while ((tmp = bis.read(buffer)) > 0) {
                    //write each chunk of data into separate file with different number in name
                    File newFile = new File(dir, String.format("%d", partCounter++) + name );
                    //System.out.println(f.getParent());
                    try (FileOutputStream out = new FileOutputStream(newFile)) {
                        out.write(buffer, 0, tmp);//tmp is chunk size
                    }
                }
                System.out.println("File details are : "+name+"  "+sizefile);
                System.out.println("Number of chunks: "+ (partCounter-1));
            }
            return (partCounter-1);
        }



         public static void sendFile(Socket conn,String fileName) throws IOException
            {

                 File myFile = new File(fileName);
                 byte[] mybytearray = new byte[(int) myFile.length()];

                 FileInputStream fis = new FileInputStream(myFile);
                 BufferedInputStream bis = new BufferedInputStream(fis);

                 DataInputStream dis = new DataInputStream(bis);
                 dis.readFully(mybytearray, 0, mybytearray.length);

                 OutputStream os = conn.getOutputStream();

                 DataOutputStream dos = new DataOutputStream(os);

                 dos.writeUTF(myFile.getName());
                 dos.writeLong(mybytearray.length);
                 dos.write(mybytearray, 0, mybytearray.length);
                 dos.flush();
                 dis.close();

            }

}
import java.io.*;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.util.*;
公共类服务器{
private static final int sPort=8000;//服务器将侦听此端口号
公共静态字符串str;
公共静态INTC;
公共静态void main(字符串[]args)引发异常{
System.out.println(“服务器正在运行”);
ServerSocket侦听器=新的ServerSocket(运动型);
int clientNum=1;
System.out.println(“输入文件名:”);
扫描仪输入=新扫描仪(系统输入);
str=in.nextLine();
字符串路径=System.getProperty(“user.dir”);
字符串filepath=path+“/”+str;
in.close();
试一试{
c=拆分文件(新文件(文件路径));
}捕获(IOE1异常){
e1.printStackTrace();
}
试一试{
while(true){
新处理程序(listener.accept(),clientNum,c.start();
System.out.println(“客户端”+clientNum+“已连接!”);
clientNum++;
}
}最后{
listener.close();
} 
}
/**
*处理程序线程类。处理程序是从侦听线程派生的
*循环并负责处理单个客户机的请求。
*/
私有静态类处理程序扩展线程{
专用插座连接;
私有整数块计数;
private ObjectInputStream in;//从套接字读取的流
private ObjectOutputStream out;//流写入套接字
private int no;//客户端的索引号
公共处理程序(套接字连接,int-no,int-c){
这个连接=连接;
这个。否=否;
this.chunkcount=c;
}
公开募捐{
试一试{
//初始化输入和输出流
out=newObjectOutputStream(connection.getOutputStream());
out.flush();
in=newObjectInputStream(connection.getInputStream());
试一试{
字符串路径=System.getProperty(“user.dir”);
路径=路径+“/”+“块”+“/”;
System.out.println(路径);
System.out.println(“总块数:+chunkcount”);
int i=否;
int j=i;
int k=0;
OutputStream op=connection.getOutputStream();
DataOutputStream d=新的DataOutputStream(op);
d、 书面(否);
d、 冲洗();
System.out.println(“j或clientnum的值:+j”);

虽然(j'It fails to transfer'不是一个问题描述。为什么创建对象流而不使用它们?为什么分块?程序包含的逻辑比描述中的多。客户端应该做什么?它应该只传输一个分块吗?如果是,神奇变量雅加达
的意思是什么?混淆是什么意思对于服务器中的常量
5
,“传输失败”不是一个问题描述。为什么要创建对象流而不使用它们?为什么要分块?程序包含的逻辑比描述中的多。客户端应该做什么?是否只传输一个分块?如果是,神奇变量
jakarta
是什么意思?服务器中常数
5
的混淆是什么意思?
import java.net.*;
import java.io.*;



public class Client {
    Socket requestSocket;           //socket connect to the server
    ObjectOutputStream out;         //stream write to the socket
    ObjectInputStream in;          //stream read from the socket


    public Client() {}


    void run()
    {

        try{
            //create a socket to connect to the server
            requestSocket = new Socket("localhost", 8000);
            System.out.println("Connected to localhost in port 8000");
            //initialize inputStream and outputStream
            out = new ObjectOutputStream(requestSocket.getOutputStream());
            out.flush();
            in = new ObjectInputStream(requestSocket.getInputStream());





             System.out.println("Ready to receive files ( Enter QUIT to end):");



            BufferedInputStream in1 = new BufferedInputStream(requestSocket.getInputStream());
            DataInputStream d = new DataInputStream(in1);

            int clientnum=d.readInt();
             String path = System.getProperty("user.dir");
            String oppath = path + "/" + "Client" + clientnum;
            File dir = new File(oppath);
            dir.mkdir();


            int numchunk=d.readInt();
            System.out.println(numchunk);
            int jakarta=1;
            while(jakarta<=numchunk ){
                    jakarta++;



                    String newpath=oppath+"/";
                    File f = new File(newpath);
                    f.createNewFile();


                   receiveFile(requestSocket,newpath);
                    System.out.println("File Received");
            }

        }
        catch (ConnectException e) {
                System.err.println("Connection refused. You need to initiate a server first.");
        } 

        catch(UnknownHostException unknownHost){
            System.err.println("You are trying to connect to an unknown host!");
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
        finally{
            //Close connections
            try{
                in.close();
                out.close();
                requestSocket.close();
            }
            catch(IOException ioException){
                ioException.printStackTrace();
            }
        }
    }
    //send a message to the output stream
    public static void receiveFile(Socket s1,String oppath) throws IOException
    {
        String fileName;

        try {
            int bytesRead;
            InputStream in = s1.getInputStream();

            DataInputStream clientData = new DataInputStream(in);

            fileName = clientData.readUTF();
            OutputStream output = new FileOutputStream(oppath+fileName);
            long size = clientData.readLong();
            byte[] buffer = new byte[1024];
            while (size > 0
                    && (bytesRead = clientData.read(buffer, 0,
                            (int) Math.min(buffer.length, size))) != -1) {
                output.write(buffer, 0, bytesRead);
                size -= bytesRead;
            }

            output.flush();
            output.close();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }



    }
    //main method
    public static void main(String args[])
    {
        Client client = new Client();
        client.run();
    }

}