如何从多个客户端接收文件?

时间:2014-07-11 04:24:43

标签: java sockets network-programming

我创建了一个程序,它将文件发送到服务器或客户端

我的问题是我有2个客户端,他们都需要将文件发送到服务器 会发生什么是服务器只能从1个客户端(首先发送文件的客户端)接收文件

我该如何解决这个问题?

这是我的代码:

服务器

private void sendFile(File file)throws IOException
{
    Socket socket = null;
    String host = "127.0.0.1";
    String receiver=txtReceiver.getSelectedItem().toString();
    int port=0;
    if(receiver=="Client1")
    {
        host="127.0.0.2";
        port=4441;
    }
    else if(receiver=="Client2")
    {
        port=4442;
        host="127.0.0.3";
    }
    else if(receiver=="Server")
    {
        port=4440;
        host="127.0.0.1";
    }

    socket = new Socket(host, port);

    //File file = new File("Client.txt");
    // Get the size of the file
    long length = file.length();
    if (length > Integer.MAX_VALUE) {
        System.out.println("File is too large.");
    }
    byte[] bytes = new byte[(int) length];
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count;

    while ((count = bis.read(bytes)) > 0) {
        out.write(bytes, 0, count);
    }

    out.flush();
    out.close();
    fis.close();
    bis.close();
    socket.close();
}

public static void main(String args[]) throws IOException {
ServerSocket serverSocket = null;

    try 
    {
        serverSocket = new ServerSocket(4440);
    } catch (IOException ex) 
    {
        System.out.println("Can't setup server on this port number. ");
    }

    Socket socket = null;
    InputStream is = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    int bufferSize = 0;

    try
    {
        socket = serverSocket.accept();
    } catch (IOException ex) 
    {
        System.out.println("Can't accept client connection. ");
    }

    try 
    {
        is = socket.getInputStream();
        bufferSize = socket.getReceiveBufferSize();
        System.out.println("Buffer size: " + bufferSize);
    } catch (IOException ex) 
    {
        System.out.println("Can't get socket input stream. ");
    }

    try 
    {
        fos = new FileOutputStream("C:\\Users\\Jake_PC\\Documents\\NetBeansProjects\\OJT2\\ServerReceivables\\file.txt");
        bos = new BufferedOutputStream(fos);

    } catch (FileNotFoundException ex)
    {
        System.out.println("File not found. ");
    }

    byte[] bytes = new byte[bufferSize];

    int count;

    while ((count = is.read(bytes)) > 0)
    {
        bos.write(bytes, 0, count);
    }

    bos.flush();
    bos.close();
    is.close();
    socket.close();
    serverSocket.close();

客户端

public static void main(String args[])throws IOException {
ServerSocket serverSocket = null;

    try 
    {
        serverSocket = new ServerSocket(4441);
    } catch (IOException ex) 
    {
        System.out.println("Can't setup server on this port number. ");
    }

    Socket socket = null;
    InputStream is = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    int bufferSize = 0;

    try
    {
        socket = serverSocket.accept();
    } catch (IOException ex) 
    {
        System.out.println("Can't accept client connection. ");
    }

    try 
    {
        is = socket.getInputStream();
        bufferSize = socket.getReceiveBufferSize();
        System.out.println("Buffer size: " + bufferSize);
    } catch (IOException ex) 
    {
        System.out.println("Can't get socket input stream. ");
    }
    //C:\Users\Jake_PC\Documents\NetBeansProjects\OJT2
    try 
    {
        fos = new FileOutputStream("C:\\Users\\Jake_PC\\Documents\\NetBeansProjects\\OJT2\\Client1Receivables\\file.txt");
        bos = new BufferedOutputStream(fos);

    } catch (FileNotFoundException ex)
    {
        System.out.println("File not found. ");
    }

    byte[] bytes = new byte[bufferSize];

    int count;

    while ((count = is.read(bytes)) > 0)
    {
        bos.write(bytes, 0, count);
    }

    bos.flush();
    bos.close();
    is.close();
    socket.close();
    serverSocket.close();
}

private void sendFile(File file)throws IOException
{
    Socket socket = null;
    String host = "127.0.0.1";
    String receiver=txtReceiver.getSelectedItem().toString();
    int port=0;
    if(receiver=="Client1")
    {
        port=4441;
    }
    else if(receiver=="Client2")
    {
        port=4442;
    }
    else if(receiver=="Server")
    {
        port=4440;
    }

    socket = new Socket(host, port);

    //File file = new File("Client.txt");
    // Get the size of the file
    long length = file.length();
    if (length > Integer.MAX_VALUE) {
        System.out.println("File is too large.");
    }
    byte[] bytes = new byte[(int) length];
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count;

    while ((count = bis.read(bytes)) > 0) {
        out.write(bytes, 0, count);
    }

    out.flush();
    out.close();
    fis.close();
    bis.close();
    socket.close();
}

1 个答案:

答案 0 :(得分:1)

您需要启动一个新线程来处理每个接受的套接字。例子比比皆是。请参阅Java教程中的自定义网络路径。