Java 소켓 통신 중 질문 드립니다.
글쓴이: skyjunray / 작성시간: 수, 2010/01/20 - 4:44오후
서버(PC)와 클라이언트(안드로이드 에뮬)간 소켓 통신 구현 중입니다.
Client측
Socket socket = new Socket(serverIP, 19090);
mOutStream = new DataOutputStream(socket.getOutputStream());
mFileOut = new FileOutputStream("DataOutput.dat");
try {
mOutStream.writeShort(pt.cVersion);
mOutStream.write(EncodingUtils.getAsciiBytes(pt.cCmd));
mOutStream.write(EncodingUtils.getAsciiBytes(pt.cLength));
mOutStream.write(pt.cType);
mOutStream.close();
String s = (String)mOutStream.toString();
Log.d(TAG, "mOutStream: " + s.getBytes());
} catch (IOException e) {
Log.e(TAG, "Error: " + e);
// socket.close();
}
try {
<span>mInputStream = new DataInputStream(socket.getInputStream());</span>
String s = mInputStream.readLine();
Log.d(TAG, "result: " +s);
errResult = 400; } catch (Exception e) {
Log.e(TAG, "Error: " +e);
}Server측
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
while (true) {
System.out.println("S: waiting?");
Socket client = serverSocket.accept();
System.out.println("S: Receiving...");
byte[] sender = new byte[3];
DataOutputStream out = new DataOutputStream(client.getOutputStream());
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String s = in.readLine();
System.out.println("from client" + s);
} catch(Exception e) {
System.out.println("S: Error");
e.printStackTrace();
}
try {
pt.cVersion = 0x0001;
pt.cType[0] = 0x01;
pt.cMacVer[0] = 0x01;
System.out.println("S: Send to client");
out.writeShort(pt.cVersion);
out.write(pt.cType);
out.write(pt.cMacVer);
String s = (String)out.toString();
System.out.println("sent form sever: " +s);
out.close();
System.out.println("S: Sending complete");
} catch(Exception e) {
System.out.println("S: Error");
e.printStackTrace();
}Client에서 보낸 데이터를 서버에서 잘 받고, 그 후 다시 서버에서 생성한 데이터를 클라이언트에
보내려고 하는데요. 서버에 잘 전달된 것 확인하고 서버에서 만든 데이터를 다시 클라이언트로
보내려고 하는데..위에 소스 보면 빨갛게 칠해 놨는데요. 그 부분이 제 의도는 서버에서 보낸 걸
받는 건데..저기서 에러가 발생합니다.
Error: Java.net.SocketException: Socket is closed 라는 메세지가 나옵니다.
코드상으로 소켓 닫는 구문은 넣지 않았는데..서버에서 전달한 것을 제대로 못 가져오는 이유는
무엇인가요? 아니면 제가 모르는 구현상의 문제가 있는 것인지...답답하네요..
조언 부탁드립니다.
Forums:


SocketOutputStream.close()는
SocketOutputStream.close()는 Socket을 닫아버립니다.
스트림을 닫는 순서를 조절해보시면 될 것 같네요.
mOutStream.close(); 이
가 불려졌기 때문입니다.
으로 mOutStream 이 생성되었으므로, mOutStream.close() 는 socket 을 끊습니다.
--
말할 수 있는 것은 분명하게 말해질 수 있다;
말해질 수 없는 것에 대해서는 침묵해야한다.
논리철학논고 - 루드비히 비트겐슈타인
--
말할 수 있는 것은 분명하게 말해질 수 있다;
말해질 수 없는 것에 대해서는 침묵해야한다.
논리철학논고 - 루드비히 비트겐슈타인
댓글 달기