import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TaskThread implements Runnable {
private Socket clientSocket;
private String lineBreak = System.getProperty("line.separator");
private StringBuffer log = new StringBuffer();
public void run() {
String messageForClient = "";
try {
saveLog("[Client Socket Address : " + clientSocket.getRemoteSocketAddress() + "]");
saveLog("[Client Request Time : " + getRequestTime() + "]");
String clientData = getData();
saveLog("[Client Data : " + clientData + "]");
messageForClient = Dispatcher.execute(clientData);
saveLog("[Return Data : " + messageForClient + "]");
}
catch(IOException i) {
saveErrorLog(i.getMessage());
}
catch(Exception e) {
saveErrorLog(e.getMessage());
}
finally {
try {
sendData(messageForClient);
}
catch (IOException e) {
saveErrorLog("클라이언트 전송 오류 : " + e.getMessage());
}
finally {
try {
clientSocket.close();
}
catch (IOException e) {
saveErrorLog("소켓 close 오류 : " + e.getMessage());
}
}
System.out.println(log.toString());
saveLogInDb();
}
}
private void saveLogInDb() {
}
private void sendData(String stringData) throws IOException {
OutputStream out = null;
try {
out = clientSocket.getOutputStream();
byte[] datas = stringData.getBytes("euc-kr");
out.write(datas);
}
catch(IOException e) {
}
finally {
if(out != null) {
out.close();
}
}
}
private void saveErrorLog(String errMsg) {
saveLog("[Error Message : " + errMsg + "]" + lineBreak);
}
private void saveLog(String content) {
log.append(content + lineBreak);
}
private String getRequestTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date now = new java.util.Date();
return sdf.format(now);
}
public TaskThread(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public String getData() throws IOException {
DataInputStream dis = null;
StringBuffer returnDataSb = new StringBuffer();
saveLog("------------------------------------------------");
try {
dis = new DataInputStream(clientSocket.getInputStream());
int count = 0;
String macID = "";
String userID = "";
String userPW = "";
int MAC_ID_BYTES = 50;
int USER_ID_BYTES = 30;
int USER_PW_BYTES = 64;
if(dis.available() > 0) {
byte[] macId = new byte[MAC_ID_BYTES];
byte[] userId = new byte[USER_ID_BYTES];
byte[] userPw = new byte[USER_PW_BYTES];
dis.read(macId);
dis.read(userId);
dis.read(userPw);
macID = new String(macId, 0, MAC_ID_BYTES).trim();
userID = new String(userId, 0, USER_ID_BYTES).trim();
userPW = new String(userPw, 0, USER_PW_BYTES).trim();
saveLog("[Mac ID(" + macID.length() + ")] : " + macID);
saveLog("[userID(" + userID.length() + ")] : " + userID);
saveLog("[userPW(" + userPW.length() + ")] : " + userPW);
/* dis.read(in,MAC_ID_BYTES,USER_ID_BYTES);
userID = new String(in,MAC_ID_BYTES,USER_ID_BYTES);
userID = userID.trim();
dis.read(in,MAC_ID_BYTES + USER_ID_BYTES, USER_PW_BYTES);
userPW = new String(in,MAC_ID_BYTES + USER_ID_BYTES, USER_PW_BYTES);
userPW = userPW.trim();
saveLog("[Mac ID] : " + macID);
saveLog("[userID] : " + userID);
saveLog("[userPW] : " + userPW);
returnDataSb.append(macID + " " + userID + " " + userPW);*/
returnDataSb.append(macID+"/"+userID+"/"+userPW+"/");
}
saveLog("------------------------------------------------");
return returnDataSb.toString();
}
catch(IOException i) {
throw i;
}
finally {
if(dis != null) dis.close();
}
}
}
':::: 개발 :::: > ::: JAVA :::' 카테고리의 다른 글
전자정부프레임웍 jfile 테스트 해보기 (2) | 2014.03.04 |
---|---|
JAVA 객체 출력 toString OVERRIDE (0) | 2014.02.26 |
map -> list 가져와서 vo 맵핑 (0) | 2013.10.28 |
스프링 액셀 업로드 poi URL (0) | 2013.10.17 |
간단한 vo / dao / service 테스트 (0) | 2013.05.03 |
자바 제네릭 심플 예제 (0) | 2013.04.11 |
netty 시작 ( 테스트 ) (0) | 2013.02.06 |
환경 변수 세팅 (0) | 2012.09.25 |