================서버================
package server;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
class Server {
ArrayList<Guest> list;
HashMap<String, ArrayList<Guest>> map;
ArrayList<Guest> arraylist;
ArrayList<Guest> peoplelist;
void initNet() throws Exception {
map = new HashMap<String, ArrayList<Guest>>();
arraylist = new ArrayList<Guest>();
list = new ArrayList<Guest>();
ServerSocket ss = new ServerSocket(8888);
while (true) {
Socket s = ss.accept();
Guest g = new Guest(this, s); //내 아이피
g.start();
addGuest(g);
}
}
///////////////////////////////////////
void removeRoom(String rn){ //방에 아무도 없어서 사라짐
if(map.get(rn).size()==0){
map.remove(rn);
}
}
void removeGuestRoom(String rn , Guest guest) throws Exception{ // 방에서 사람 빠져나가는거
map.get(rn).remove(guest);
map.get(rn);
broadcastRoom(rn, "out/"+guest.id);
}
// /////////////////////////////////////////////
void addGuestRoom(String rn, Guest guest) {
ArrayList<Guest> list2 = map.get(rn);
list2.add(guest);
System.out.print("방제" + rn + " ,");
System.out.println("사람수 :" + list2.size());
}
void addRoom(String roomname, Guest guest) {
ArrayList<Guest> arraylist2 = new ArrayList<Guest>();
arraylist2.add(guest);
map.put(roomname, arraylist2);
System.out.println("개설된방 :" + roomname);
System.out.println("사용자수 :" + arraylist2.size());
}
void addGuest(Guest g) {
list.add(g);
System.out.println("접속자수:" + list.size());
}
void makeRoomlist() throws Exception {
Set<String> roomlist = map.keySet();
StringBuffer buffer = new StringBuffer("roomlist/");
for (String t : roomlist) {
buffer.append(t +"/"); /////사람수 표시해야할꺼같은데
}
broadcast(buffer.toString());
Roomnumber(roomlist);
}
void Roomnumber(Set<String> roomlist) throws Exception{
StringBuffer buffer2 = new StringBuffer("roomnum/"); //방에 사람수
for(String t : roomlist){
buffer2.append(map.get(t).size()+"/");
}
broadcast(buffer2.toString());
}
/////////////////////////////////////////////////////////////
public void talkMsg(String talk, String talk2, String talk3) {
// talk 보낸놈
// 2 받을놈
// 3 할말
for (Guest g : list) {
if (g.id.equals(talk2)) {
try {
g.sendMsg("귓속말/" + talk + "&" + talk2 + "&" + talk3);
} catch (Exception e) {
System.out.println("게스트에서 귓말보내다가 에러" + e.getMessage());
}
}
}
}
void removeGuest(Guest g) {
list.remove(g);
System.out.println("접속자수:" + list.size()); //1명
}
void broadcast(String msg) throws Exception {
try {
for (Guest g : list) { //msg/[이름]안녀아세요 사람입니다.
g.sendMsg(msg);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
void broadcastRoom(String rn, String msg) throws Exception { // 해당방 메시지 날라옴
ArrayList<Guest> list2 = map.get(rn); // arrayList에 등록되어있는 게스트들
for (Guest g : list2) {
g.sendMsg(msg); // 이름-guestlistRoom/이름/이름2 이름2-guestlistRoom/이름/이름2
}
}
void makeGuestlist() throws Exception { // guestlist/홍길동/김길동/이길동/
StringBuffer buffer = new StringBuffer("guestlist/"); //guestlist/
for (Guest g : list) {
buffer.append(g.id + "/");
}
broadcast(buffer.toString());
}
void makeGuestlistRoom(String rn) throws Exception { // guestlist/홍길동/김길동/이길동/
ArrayList<Guest> list2 = map.get(rn);
StringBuffer buffer = new StringBuffer("guestlistRoom/"); //guestlistRoom/이름/이름2
peoplelist=list2;
for (Guest g : list2) {
buffer.append(g.id + "/");
}
broadcastRoom(rn, buffer.toString()); // 다방 , guestlistRoom/이름/이름2
}
public static void main(String args[]) throws Exception {
Server server = new Server();
server.initNet();
}
}
============대기방================
package server;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Random;
class WaitingRoom extends Frame implements ActionListener,MouseListener {
List list1, list2;
TextField tf;
Button b;
Socket s;
BufferedWriter bw;
BufferedReader br;
String id;
Room room;
WaitingRoom(String id) {
super(id+"님 환영합니다");
this.id = id;
list1 = new List();
list2 = new List();
tf = new TextField();
b = new Button("방만들기");
b.addActionListener(this);
list1.addMouseListener(this);
Panel p1 = new Panel();
p1.setLayout(new GridLayout(1, 2));
Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
p1.add(list1);
p1.add(list2);
p2.add(tf);
p2.add(b, "East");
add(p1);
add(p2, "South");
}
void stop() throws IOException{
s.close();
}
void initNet() throws Exception {
s = new Socket("localhost", 8888);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(os);
bw = new BufferedWriter(osw);
sendMsg("enter/" + id);
}
public void actionPerformed(ActionEvent e) {
try {
setVisible(false);
dispose();
room = new Room(id,tf.getText(),this);
room.setBounds(200,200,400,300);
room.setVisible(true);
sendMsg("mkroom/"+tf.getText()); // mkroom/다방
} catch (Exception e1) {
e1.printStackTrace();
}
}
void sendMsg(String msg) throws Exception {
try{
bw.write(msg + "\n");
bw.flush();
}
catch(Exception ee){
ee.printStackTrace();
}
}
public void readMsg() {
try {
while (true) {
String line = br.readLine();
System.out.println(line);
String[] array = line.split("/");
switch (array[0]) {
case "guestlist":
list2.removeAll();
int len = array.length;
for (int i = 1; i < len; i++)
list2.add(array[i]);
break;
case "roomlist" :
list1.removeAll();
len = array.length;
for (int i = 1; i < len; i++)
list1.add(array[i]);
break;
case "roomnum" :
len = array.length;
for (int i = 1; i < len; i++)
list1.replaceItem(list1.getItem(i-1)+"//"+array[i]+"명", i-1);
break;
case "guestlistRoom":
room.list.removeAll();
int len2 = array.length;
for (int i = 1; i < len2; i++){
room.list.add(array[i]);
}
break;
case "out" :
room.list.remove(array[1]);
break;
case "msg" :
System.out.println("메시지 받음");
room.ta.append("["+array[1]+"]"+array[2]+"\n");
break;
}
}
} catch (Exception e) {
System.out.println("읽다가에러남~"+e.getMessage());
e.printStackTrace();
}
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
try {
String roomname[] = list1.getSelectedItem().split("/");
setVisible(false);
dispose();
room = new Room(id,roomname[0],this);
room.setBounds(200,200,400,300);
room.setVisible(true);
sendMsg("roomjoin/"+id+"/"+roomname[0]);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String args[]) throws Exception {
Random rc = new Random();
WaitingRoom client = new WaitingRoom(args[0]+rc.nextInt(10)*1);
client.initNet();
client.setBounds(200, 200, 400, 300);
client.setVisible(true);
client.readMsg();
}
}
자바스크립트 이미지 마우스 올려두면 이미지 따로 보여주기 (0) | 2015.02.12 |
---|---|
구루비에서 본 오라클 소계 정렬하는 쿼리문 (0) | 2015.02.11 |
자바 채팅프로그램 ,대기실 포함 (0) | 2015.02.10 |
스크립트 기타 함수들 외우기 (0) | 2015.02.10 |
스크립트 innerHTML을 이용해 구구단 뽑기 (0) | 2015.02.10 |