클라이언트 - 서버 파일 전송 프로젝트에서 로컬 호스트 테스트를 거의 완료했지만 현재 클라이언트와 서버의 소스 코드는 다음과 같습니다.java 파일 전송 소켓 쓰기 오류
클라이언트 측이
public class Client {
public static void main(String args[]) {
String receiverIP = null;
int serverPort = 0;
receiverIP = args[0];
serverPort = Integer.parseInt(args[1]);
String fileToSend = args[2];
byte[] aByte = new byte[1];
int bytesR;
Socket clientSocket = null;
BufferedOutputStream bos = null;
InputStream is = null;
try {
clientSocket = new Socket(receiverIP, serverPort);
bos = new BufferedOutputStream(clientSocket.getOutputStream());
is = clientSocket.getInputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
if (is != null) {
FileOutputStream fos = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
File myFile = new File(fileToSend);
System.out.println("The file chosen is being sent...");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, mybytearray.length);
bis.close();
return;
}catch (IOException ex) {
ex.printStackTrace();
}
File file = new File("C:\\copy.jpg");
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bytesR = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesR = is.read(aByte);
} while (bytesR != -1);
System.out.println("File transfer successful");
bos.write(baos.toByteArray());
bos.flush();
bos.close();
clientSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
여기
public class Server {
public static void main(String args[]) {
while (true) {
ServerSocket welcomeSocket = null;
BufferedOutputStream ToClient = null;
try {
welcomeSocket = new ServerSocket(3249);
System.out.println("The port " + welcomeSocket.getLocalPort() + " is opened and ready for use.");
Socket connectionSocket = welcomeSocket.accept();
ToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
서버 측 인 내가 얻을 오류
The file chosen is being sent...
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
at Client.main(Client.java:44)
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at Client.main(Client.java:55)
나는이 오류가 모든 데이터가 전송되기 전에 서버 소켓을 닫는에 대한 아닙니다 거의 확신하고,이 ByteArray하지만 내 모든 수정 시도에 읽기와 쓰기 과정에 대해 아마도, 헛된했다 내가 의도 한대로 작동하지 않도록 스트림을 잘못 배치했습니다. (copy.jpg 파일이 생성되었지만 스트림이 없습니다.) 어떤 도움도 받으실 수 있습니다.
편집 : 현재 언급 한 것을 잊어 버렸습니다. 무선 인터넷 연결 및 무선 네트워크에 대한 테스트가 신뢰할 수없는 소켓 porgramming에 대해 조금 읽었습니다