-----------문자열 전송 ------------> -------입력파일(A.txt)전송 ---------->
입력파일을 읽어 연산
<-----------문자열 전송 ------------
<-------출력파일(B.txt)전송 ----------
하는 서버를 만들고있는데 클라이언트쪽 파일 전송까지 정상적으로 되고 그뒤로는 소켓이 끊겨버리면서 서버에서 전송한 결과를 클라쪽에서 받을 수가 없네요
진짜 이것저것 바꿔보고 해도 도저히 답이 안나와서 ㅠㅠㅠㅠ
저번에 올렸었는데 아직 해결이 안되서 이번에 소스도 함께 올려봅니다 계속 이부분에서 막혀서 진행이 안되네요 ㅠㅠㅠㅠ
도와주세용 ㅠㅠㅠ
---------------Server--------------
public class BioToolServer {
public static void main(String[] args) {
new BioToolServer().runServer();
}
public void runServer() {
int SERVER_PORT = 7777; // 임시포트
ServerSocket serverSocket = null;
Socket socket = null;
try {
System.out.println("\n\n\n=======>>>>>>> 서버 시작\n");
serverSocket = new ServerSocket(SERVER_PORT);
while (true) {
// 클라이언트 접속 대기 accept()
socket = serverSocket.accept();
System.out.println("[" + socket.getInetAddress() //
+ ":" + socket.getPort() + "]" + "접속");
//BToolServer
BToolServer bioServer = new BToolServer(socket);
bioServer.start();
}// while
} catch (Exception e) {
e.printStackTrace();
} finally {
// SocketUtil.close(serverSocket);
}
}
}
class BToolServer extends Thread {
private Socket socket;
private DataOutputStream dsOut;
private DataInputStream dsIn;
BToolServer(Socket socket) {
this.socket = socket;
try {
dsIn = new DataInputStream(socket.getInputStream());
dsOut = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
String strInput = dsIn.readUTF();
System.out.println("strInput : " + strInput);
if (strInput.equals("serverEchoTest")) { // Echo
dsOut.writeUTF(strInput);
} else if (strInput.equals("runBio")) { // BioTool
// File Receive
String rFileName = dsIn.readUTF();
System.out.println("sFileName : " + rFileName);
String rFilePath = "C:\\Users\\Administrator\\Desktop\\temp\\ServerIn\\"
+ rFileName;
File inFile = new File(rFilePath);
FileOutputStream fOutStream = new FileOutputStream(inFile);
BufferedOutputStream bufferOut = new BufferedOutputStream(
fOutStream);
int inLen;
int inSize = 4096;
byte[] inData = new byte[inSize];
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
while ((inLen = dsIn.read(inData)) != -1) {
bufferOut.write(inData, 0, inLen);
}
bufferOut.flush();
bufferOut.close();
fOutStream.close();
dsIn.close();
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("결과전송!");
dsOut.writeUTF("결과전송테스트");
dsOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class BioToolClient {
private String serverIp;
private int nPort;
private static String TEMP_DIR = "C:\\Users\\Administrator\\Desktop\\temp\\";
BioToolClient(String serverIp, int nPort) {
this.nPort = nPort;
this.serverIp = serverIp;
}
public boolean serverEchoTest() {
try {
Socket socket = new Socket(serverIp, nPort);
DataOutputStream dsOut = new DataOutputStream(socket.getOutputStream());
DataInputStream dsIn = new DataInputStream(socket.getInputStream());
boolean isInputData = true;
//String InputData = "runBio";
String InputData = "serverEchoTest";
String returnData = "";
dsOut.writeUTF(InputData);
while (isInputData) {
returnData = dsIn.readUTF();
isInputData = false;
}
System.out.println("insert : " + returnData);
if (InputData.trim().equals(returnData)) {
dsOut.close();
dsIn.close();
socket.close();
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public void runBioTool() {
Socket socket;
try {
socket = new Socket(serverIp, nPort);
ClientSender sender = new ClientSender(socket);
ClientReceiver receiver = new ClientReceiver(socket);
receiver.start();
sender.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientReceiver extends Thread {
private Socket socket;
private DataInputStream dsIn;
ClientReceiver(Socket socket) {
try {
this.socket = socket;
dsIn = new DataInputStream(socket.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
System.out.println("\n----------Receiver------------\n");
String strInput = "";
while(true)
{
strInput = dsIn.readUTF();
System.out.println(strInput);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientSender extends Thread {
private Socket socket;
private DataOutputStream dsOut;
ClientSender(Socket socket) {
try {
this.socket = socket;
dsOut = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
System.out.println("\n----------Sender------------\n");
String sFileName = "clientSend.txt";
String sFilePath = "C:\\Users\\Administrator\\Desktop\\temp\\" + sFileName;
dsOut.writeUTF("runBio"); //분석 툴 실행 구분
dsOut.writeUTF(sFileName);
File readFile = new File(sFilePath);
FileInputStream fInStream = new FileInputStream(readFile);
BufferedInputStream bufferIn = new BufferedInputStream(fInStream);
int len = 0;
int size = 4096;
byte[] data = new byte[size];
while ((len = bufferIn.read(data)) != -1) {
dsOut.write(data, 0, len);
}
bufferIn.close();
fInStream.close();
dsOut.flush();
dsOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
///////////////////////////////////////////////
클라이언트 콘솔
///////////////////////////////////////////////
insert : serverEchoTest
정상
----------Receiver------------
----------Sender------------
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.net.SocketInputStream.read(SocketInputStream.java:182)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:320)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at com.ClientReceiver.run(BioToolClient.java:97)
///////////////////////////////////////////////
서버 콘솔
///////////////////////////////////////////////
=======>>>>>>> 서버 시작
[/127.0.0.1:57247]접속
strInput : serverEchoTest
[/127.0.0.1:57248]접속
strInput : runBio
sFileName : clientSend.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
결과전송!
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeUTF(Unknown Source)
at java.io.DataOutputStream.writeUTF(Unknown Source)
at com.BToolServer.run(BioToolServer.java:104)
아 진짜 답답해서 돌아버리겠습니다 ㅠㅠ 데이터를 주고 받고 close하는 부분때문인거 같은데 여기저기 옮겨보고 없애봐도 에러가나거나 무한루프를 도네요... 도와주세요 ㅠㅠ