Object 소켓 서버.zip
클라는 압축파일안에.
package PC;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
public static void main(String[] args) throws IOException{
int i=1;
ServerSocket sSocket = new ServerSocket(3000);
System.out.println("PC방 개업 시작...");
while(true){
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
Socket socket = sSocket.accept();
System.out.println(socket + "손님 입장");
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
ChatRunner cr = new ChatRunner(socket,ois,oos,i);
cr.start();
i++;
}
}
}
package PC;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
public class ChatRunner extends Thread {
private boolean flag = false;
private Socket socket = null;
private Data data = null;
private int client=0;
private int count=0;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
public ChatRunner(Socket socket,ObjectInputStream ois, ObjectOutputStream oos, int client) throws IOException {
this.socket = socket;
this.ois = ois;
this.oos = oos;
this.client = client;
}
public void run() {
try {
while (!flag) {
// 가격 리스트 측정
ArrayList<String> glist = new ArrayList<String>();
ArrayList<String> flist = new ArrayList<String>();
int[] gamemoney = new int[10];
int[] foodmoney = new int[10];
glist.add("서든어택"); gamemoney[0]=2000; glist.add("리니지"); gamemoney[1]=3000;
glist.add("LOL"); gamemoney[2]=4000; glist.add("넷마블"); gamemoney[3]=1500;
glist.add("스타크래프트"); gamemoney[4]=1000;
flist.add("피자"); foodmoney[0]=12000; flist.add("냠냠과자"); foodmoney[1]=1000;
flist.add("햄버거"); foodmoney[2]=6000; flist.add("짜장면"); foodmoney[3]=5500;
flist.add("뽀글이"); foodmoney[4]=2200;
// Data Object에 가격 셋팅 하기
data = new Data();
data.setClientNum(client);
data.setCount(-1);
data.setGame(glist);
data.setGamemoney(gamemoney);
data.setFood(flist);
data.setFoodmoney(foodmoney);
if(count==0){
oos.writeObject(data);
System.out.println(client + " : 번 손님의 PC 실행!");
count++;
}
else{
flag=true;
}
}
data = (Data)ois.readObject();
int time = data.getTotaltime();
int money = data.getTotalmoney();
int num = data.getClientNum();
System.out.println(num +" 의 손님의 총 플레이시간 : " + time + " 총 금액 : " + money);
System.out.println("안녕히가세요!");
oos.close();
ois.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}