나는 클라이언트 서버를 만들고있다. 서버가 하드 코딩 된 파일을 보낼 수 있지만 클라이언트는 지정하지 않은 한 얻을 수 있습니다. 텍스트 파일 만 보내야합니다. 내가 아는 한 : 클라이언트가 먼저 파일 이름을 보낸 다음 서버가 아무 것도 복잡하게 보내지 만 모든 종류의 오류가 발생합니다.이 코드는 연결 재설정/소켓 닫힌 오류가 발생합니다. 주요 문제는 네트워킹을 연구 할 시간이별로 없다는 것입니다.자바 요청 파일, 파일 보내기 (클라이언트 - 서버)
악의를 가져다 주시면 감사하겠습니다.
EDIT. 스트림을 닫으면 소켓이 닫히고 그 이유는 무엇입니까? 그렇게해서는 안됩니다.
서버 측 :
InputStream sin=newCon.getInputStream();
DataInputStream sdata=new DataInputStream(sin);
location=sdata.readUTF();
//sdata.close();
//sin.close();
File toSend=new File(location);
byte[] array=new byte[(int)toSend.length()];
FileInputStream fromFile=new FileInputStream(toSend);
BufferedInputStream toBuffer=new BufferedInputStream(fromFile);
toBuffer.read(array,0,array.length);
OutputStream out=newCon.getOutputStream(); //Socket-closed...
out.write(array,0,array.length);
out.flush();
toBuffer.close();
newCon.close();
클라이언트 측 :
int bytesRead;
server=new Socket(host,port);
OutputStream sout=server.getOutputStream();
DataOutputStream sdata=new DataOutputStream(sout);
sdata.writeUTF(interestFile);
//sdata.close();
//sout.close();
InputStream in=server.getInputStream(); //socket closed..
OutputStream out=new FileOutputStream("data.txt");
byte[] buffer=new byte[1024];
while((bytesRead=in.read(buffer))!=-1)
{
out.write(buffer,0,bytesRead);
}
out.close();
server.close();
읽기 전에 System.out.println (sdata.available())을 사용해 보셨습니까? 어쩌면 아직 읽을 것이 없습니다. –
코드가 멀리까지 실행되지 않는다 :/ – MustSeeMelons
하지만 location = sdata.readUTF()에 연결이 설정되었는데 연결이 재설정되었다고 할 수 있습니까? 그 전에 말이야. –