简体   繁体   English

NIO服务器/客户端发送映像问题

[英]NIO Server/Client sending image problems

Hey guys i have been trying to make a NIO server/client program. 大家好,我一直在尝试制作NIO服务器/客户端程序。 My problem is that the server only sends 14 bytes then it won't send anything more. 我的问题是服务器仅发送14个字节,然后将不再发送任何内容。 I've sat so long with this that i really can't see anything anymore and therefor decided to see if anyone here can see the problem 我已经坐了这么长时间,以至于我真的再也看不到任何东西,因此决定看看这里是否有人可以看到问题

Server Code: 服务器代码:

import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;

public class Testserver {
    public static void main(String[] args) throws java.io.IOException {

        ServerSocketChannel server = ServerSocketChannel.open();
        server.socket().bind(new java.net.InetSocketAddress(8888));

        for(;;) { 
            // Wait for a client to connect
            SocketChannel client = server.accept();  

            String file = ("C:\\ftp\\pic.png");


            ByteBuffer buf = ByteBuffer.allocate(1024);
            while(client.read(buf) != -1){

            buf.clear();
            buf.put(file.getBytes());
            buf.flip();

            client.write(buf);
            // Disconnect from the client
            }
            client.close();
        }
    }
}

Client: 客户:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class Testclient {

    public static void main(String[] args) throws IOException {



        SocketChannel socket = SocketChannel.open(new InetSocketAddress(8888));



        FileOutputStream out = new FileOutputStream("C:\\taemot\\picNIO.png");
        FileChannel file = out.getChannel();

        ByteBuffer buffer = ByteBuffer.allocateDirect(8192);

        while(socket.read(buffer) != -1) { 
          buffer.flip();       
          file.write(buffer); 
          buffer.compact();    
          buffer.clear();
        }
        socket.close();        
        file.close();         
        out.close();           
    }
}
  1. You're only sending the filename, not the file contents. 您只发送文件名,而不发送文件内容。 The filename is 14 bytes. 文件名是14个字节。

  2. You're sending that every time the client sends anything. 客户端每次发送任何内容时,您都在发送该消息。 However the client is never sending anything, so I'm surprised you're even getting 14 bytes. 但是客户端从不发送任何内容,因此令您惊讶的是,您甚至收到了14个字节。

  3. You're ignoring the result of write(). 您忽略了write()的结果。

If you want to send the file contents, open it with a FileChannel. 如果要发送文件内容,请使用FileChannel打开它。 Similarly, open the target file in the client with a FileChannel. 同样,使用FileChannel在客户端中打开目标文件。 Then the copy loop is the same in both places. 然后,复制循环在两个地方都相同。 The correct way to copy a buffer between channels is as follows: 在通道之间复制缓冲区的正确方法如下:

while (in.read(buffer) >= 0 || buffer.position() > 0)
{
  buffer.flip();
  out.write(buffer);
  buffer.compact();
}

Note that this takes care of partial reads, partial writes, and the final writes after reading EOS. 请注意,这将处理部分读取,部分写入以及读取EOS之后的最终写入。

Having said all that, I don't see any reason to use NIO here at all. 说了这么多,我根本没有理由在这里使用NIO。 It would all be much simpler with java.net sockets and java.io streams. 使用java.net套接字和java.io流将更加简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM