nio服务器图片显示,NIO服务器/客户端发送图像问题
嗨,我一直在努力制作NIO服务器/客户端程序。 我的问题是,服务器只发送14个字节,那么它不会再发送任何东西。我坐在这,我真的什么都看不到了,并为此这么久决定,看是否有人在这里可以看到问题NIO服务器/客户端发送图像问题
服务器代码:
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();
}
}
}
客户:
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();
}
}
嗨,我一直在努力制作NIO服务器/客户端程序。 我的问题是,服务器只发送14个字节,那么它不会再发送任何东西。我坐在这,我真的什么都看不到了,并为此这么久决定,看是否有人在这里可以看到问题NIO服务器/客户端发送图像问题 服务器代码: 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(); } } } 客户: 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(); } }