在套接字中发送停止消息

时间:2012-02-28 00:32:22

标签: java file sockets networking tcp

我想要一种有效且快速的方法来在套接字中发送停止消息。

我有一种方法可以将文件从一台电脑发送到另一台电脑。来自发件人的所有文件都出现在接收器的PC上。但是,所有数据都被写入第一个文件(仅限)。其他文件存在,但是为空。这是因为接收方法不知道何时开始写入下一个文件。

发件人

public static void sendFile (final Socket sock, File source)
{
    FileInputStream fileIn = null;

    try
{
        //Read bytes from the source file
        fileIn = new FileInputStream(source);

        //Write bytes to the receive
        //No need to use a buffered class, we make our own buffer.
        OutputStream netOut = sock.getOutputStream();

        byte[] buffer = new byte[BUFFER_SIZE];
        int read;

        while ((read = fileIn.read(buffer)) != -1)
        {
            netOut.write(buffer, 0, read);
            netOut.flush ();
        }
        //Send some stop message here
}
    catch (Exception e)
{
        e.printStackTrace ();
}
    finally
    {
        if (fileIn != null)
        {
            try
            {
                fileIn.close ();
            }
            catch (IOException e)
            {
                e.printStackTrace ();
            }
        }
    }
}

//Send files via socket
public static void sendFile (final Socket sock, File[] source)
{
    for (int i = 0; i < source.length; i++)
        sendFile (sock, source[i]);
}

接收器:

public static void receiveFile (final Socket sock, File destination)
{
    BufferedOutputStream out = null;

    try
    {
        //Receive data from socket
        InputStream clientInputStream = sock.getInputStream();

        //Write bytes to a file
        out = new BufferedOutputStream (new FileOutputStream (destination));

        byte[] buffer = new byte[BUFFER_SIZE];
        int read;
        while (true)
        {
            read = clientInputStream.read(buffer);


            out.write(buffer, 0, read);
            out.flush ();
        }
    }
    catch (IOException e)
    {
        e.printStackTrace ();
    }
    finally
    {
        if (out != null)
        {
            try
            {
                out.close ();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

//Receive files via socket
public static void receiveFile (final Socket sock, File[] destination)
{
    for (int i = 0; i < destination.length; i++)
        receiveFile (sock, destination[i]);
}

2 个答案:

答案 0 :(得分:2)

您需要修改发送/接收协议,以便在发送文件之前至少包含最小标头。您的标题应至少包含要遵循的数据大小以及您可能需要的任何其他内容(例如文件名)。

答案 1 :(得分:0)

我尝试使用你建议的标题,但它不起作用。接收器仍然不知道何时停止(因此我得到EOFException)。所有收到的数据都写入第一个文件。

public static void sendFile (Socket sock, File source)
{
    FileInputStream fileIn = null;

    try
    {
        //Read bytes from the source file
        fileIn = new FileInputStream(source);

        //Write bytes to the receive
        //No need to use a buffered class, we make our own buffer.
        OutputStream netOut = sock.getOutputStream();

        byte[] buffer = new byte[BUFFER_SIZE];
        int readBytes = 0;
        long fileSize = source.length();
        long counter = 0;

        //Send the file size
        DataOutputStream objOut = new DataOutputStream (netOut);
        System.out.println ("Writing: " + source.length ());
        objOut.writeLong (fileSize);
        objOut.flush ();

        while ((counter += readBytes) < fileSize)
        {
            readBytes = fileIn.read(buffer);
            netOut.write(buffer, 0, readBytes);
            netOut.flush ();
        }
        fileIn.close();
    }
    catch (Exception e)
    {
        e.printStackTrace ();
    }
    finally
    {
        if (fileIn != null)
        {
            try
            {
                fileIn.close ();
            }
            catch (IOException e)
            {
                e.printStackTrace ();
            }
        }
    }
}

[]

public static void receiveFile (Socket sock, File destination)
{
    BufferedOutputStream fileOut = null;

    try
    {
        //Receive data from socket
        InputStream netIn = sock.getInputStream();

        //Write bytes to a file
        fileOut = new BufferedOutputStream (new FileOutputStream (destination));

        byte[] buffer = new byte[BUFFER_SIZE];
        int readBytes = 0;
        long fileSize;
        long counter = 0;

        //Receive the file size
        DataInputStream objIn = new DataInputStream (netIn);
        fileSize = objIn.readLong ();
        System.out.println ("Receiving: " + fileSize);

        while (true)
        {
            readBytes = netIn.read (buffer);
            fileOut.write (buffer, 0, readBytes);
            fileOut.flush ();

            counter += readBytes;
            if (counter > fileSize)
                break;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace ();
    }
    finally
    {
        if (fileOut != null)
        {
            try
            {
                fileOut.close ();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    System.out.println ("Ending method");
}