서버는 UI 없음!!!!!!!!!!!!!!!!!!!! 느려짐!!!!!!!!!!!!!!!!!!!!!!!
public class ObjectCalculatorServer {
public static void main(String[] args) {
Socket sock = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
ServerSocket ss = new ServerSocket(10005);
System.out.println("클라이언트의 접속을 대기합니다.");
sock = ss.accept();
oos = new ObjectOutputStream(sock.getOutputStream());
ois = new ObjectInputStream(sock.getInputStream());
Object obj = null;
while((obj = ois.readObject()) != null) {
SendData sd = (SendData) obj;
int op1 = sd.getOp1();
int op2 = sd.getOp2();
String opcode = sd.getOpcode();
if(opcode.equals("+")) {
oos.writeObject(op1 + "+" + op2 + "=" + (op1 + op2));
} else if(opcode.equals("-")) {
oos.writeObject(op1 + "-" + op2 + "=" + (op1 - op2));
} else if(opcode.equals("*")) {
oos.writeObject(op1 + "*" + op2 + "=" + (op1 * op2));
} else if(opcode.equals("/")) {
if(op2 == 0) {
oos.writeObject("0으로 나눌 수 없습니다.");
} else {
oos.writeObject(op1 + "/" + op2 + "=" + (op1 / op2));
}
} // end if
oos.flush();
oos.reset();
System.out.println("결과를 전송하였습니다.");
} // while
} catch(Exception e) {
System.out.println(e);
} finally {
try {
if(oos != null) {
oos.close();
}
} catch(Exception e) {}
try {
if(ois != null) {
ois.close();
}
} catch(Exception e) {}
try {
if(sock != null) {
sock.close();
}
} catch(Exception e) {}
} // finally
} // main
}
public class ObjectCalculatorClient {
public static void main(String[] args) {
String ip = JOptionPane.showInputDialog("IP를 입력하세요");
Socket sock = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
sock = new Socket(ip, 10005);
oos = new ObjectOutputStream(sock.getOutputStream());
ois = new ObjectInputStream(sock.getInputStream());
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in)
);
String line = null;
while(true) {
System.out.println(
"첫번째 숫자를 입력하여 주세요. (잘못 입력된 숫자는 0으로 처리합니다.)"
);
line = keyboard.readLine();
int op1 = 0;
try {
op1 = Integer.parseInt(line);
} catch(NumberFormatException nfe) {
op1 = 0;
}
System.out.println(
"두번째 숫자를 입력하여 주세요. (잘못 입력된 숫자는 0으로 처리합니다.)"
);
line = keyboard.readLine();
int op2 = 0;
try {
op2 = Integer.parseInt(line);
} catch(NumberFormatException nfe) {
op2 = 0;
}
System.out.println(
"+, -, *, / 중 하나를 입력하세요. (잘못 입력하면 +로 처리됩니다.)"
);
line = keyboard.readLine();
String opcode = "+";
if(line.equals("+") || line.equals("-") || line.equals("*") || line.equals("/")) {
opcode = line;
} else {
opcode = "+";
}
SendData s = new SendData(op1, op2, opcode);
oos.writeObject(s);
oos.flush();
oos.reset();
String msg = (String)ois.readObject();
System.out.println(msg);
System.out.println("계속 계산하시겠습니까? (Y/n)");
line = keyboard.readLine();
if(line.equals("n")) {
break;
}
System.out.println("다시 계산을 시작합니다.");
} // while
System.out.println("프로그램을 종료합니다.");
} catch(Exception e) {
System.out.println(e);
} finally {
try {
if(oos != null) {
oos.close();
}
} catch(Exception e) {}
try {
if(ois != null) {
ois.close();
}
} catch(Exception e) {}
try {
if(sock != null) {
sock.close();
}
} catch(Exception e) {}
} // finally
} // main
}
public class SendData implements Serializable {
private int op1;
private int op2;
private String opcode;
public SendData(int op1, int op2, String opcode) {
this.op1 = op1;
this.op2 = op2;
this.opcode = opcode;
}
public int getOp1() {
return op1;
}
public int getOp2() {
return op2;
}
public String getOpcode() {
return opcode;
}
}
Session
클라이언트의 정보를 유지하는 부분