안녕하세요, 저는 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();
}
}
내가 정말 더 이상 아무것도 볼 수없는이 및 그와 너무 오래 앉아했습니다 여기에 사람이 문제를
서버 코드를 볼 수 있는지 확인하기로 결정