소켓 프로그래밍을 사용하여 두 대의 컴퓨터간에 통신을 시도하고 있습니다.Java 소켓 프로그래밍을 사용하여 두 대의 컴퓨터간에 파일을주고받습니다.
기본적으로 두 컴퓨터 모두 파일을 보내고받을 수 있어야합니다. 아래에 붙여 넣을 코드는 오류를 표시하지 않지만 서버 측 프로그램은 무기한으로 실행중인 것처럼 보입니다. 즉, 종료되지 않습니다. 여기에 붙어있는 주석으로 표시된 줄에 붙어 있습니다.
이 코드에서 처음에는 서버가 "file.txt"라는 파일을 보내고 클라이언트가 파일을 받고 이름을 "copy.txt"로 저장합니다. 나중에 클라이언트가 "file2.txt"라는 파일을 보내고 서버가 "copy2.txt"라는 이름으로 파일을 받고 저장하고 있습니다.
누군가 오류를 알려주고 개선점을 제안 할 수 있습니까?
//server side code
import java.net.*;
import java.io.*;
public class server
{
public static void main (String [] args) throws IOException
{
//sending file started
ServerSocket serverSocket = new ServerSocket(16167);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
File transferFile = new File ("/Users/abhishek/desktop/file.txt");
byte [] bytearray = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files...");
os.write(bytearray,0,bytearray.length);
os.flush();
System.out.println("File transfer complete");
//socket.close();
//sending comleted
//receiving file started
int filesize=1022386;
int bytesRead=0;
int currentTot = 0;
byte [] bytearray1 = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy2.txt");
//fos.flush();
BufferedOutputStream bos = new BufferedOutputStream(fos);
//bos.flush();
System.out.println("not moving ahead!!!");//program stucked here
bytesRead = is.read(bytearray1,0,bytearray1.length);
currentTot = bytesRead;
System.out.println("current"+currentTot);
do
{
bytesRead = is.read(bytearray1, currentTot, (bytearray1.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
System.out.println("current"+currentTot);
} while(bytesRead > -1);
System.out.println("outside current"+currentTot);
bos.write(bytearray1, 0 , currentTot);
bos.flush();
//receiving complete
System.out.println("Receving file completed");
socket.close();
}
}
//client side code
import java.net.*;
import java.io.*;
public class client
{
public static void main (String [] args) throws IOException
{
int filesize=1022386;
int bytesRead=0;
int currentTot = 0;
Socket socket = new Socket("localhost",16167);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do
{
bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
} while(bytesRead > -1);
System.out.println("current"+currentTot);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
System.out.println("receiving first file completed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//sending file
System.out.println("sending second file started!");
File transferFile = new File ("/Users/abhishek/desktop/file2.txt");
byte [] bytearray2 = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray2,0,bytearray2.length);
OutputStream os = socket.getOutputStream();
os.flush();
os.write(bytearray2,0,bytearray2.length);
os.flush();
System.out.println("sending second file completed!");
//sending complete
socket.close();
}
}
댓글에 대한 느낌이 들었습니다. – tilpner
@StackOverflowException 질문 된 질문에 답변하고 제안 된 조언을 포함하도록 업데이트했습니다. – ControlAltDel
네, 그게 내가 갇혀있는 곳입니다. 처음으로 소켓 프로그래밍을하고 있기 때문에이 상황을 피할 수있는 방법을 알려 주시기 바랍니다.또한, 내 문제에 대한 이해를 돕기 위해 아래의 @Nekojimi 답장에 대한 내 의견을 읽으십시오. – user3245573