서버 소스이다.

포트번호를 설정을한다. (기존의 사용하는 포트번호와 겹치지 않게 조심!)

스트림 객체 생성하고 스레드 시작한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
 
// To create UI and listen to client access
public class EightServer extends JFrame implements Runnable {
 
    static final int PORT = 8880// 서버프로그램의 포트번호
    Socket socket;
    ServerSocket serverSocket; // 서버소켓
    DataOutputStream dos;
    DataInputStream dis;
    ArrayList<User> userArray; // 서버에 접속한 사용자들
    ArrayList<Room> roomArray; // 서버가 열어놓은 채팅방들
 
    int sizeX = 600, sizeY = 600;
    Dimension whole, part;
    int xPos, yPos;
    JTextArea jta;
    ImageIcon icon;
    JPanel jp;
 
    EightServer() {
        userArray = new ArrayList<User>();
        roomArray = new ArrayList<Room>();
        setTitle("Octopus Server");
        setSize(sizeX, sizeY);
 
        icon = new ImageIcon("icon2.png");
        this.setIconImage(icon.getImage());//타이틀바에 이미지넣기
        
        jta = new JTextArea();
        jp = new JPanel();
 
        jp.setLayout(new GridLayout(12)); // 그리드 레이아웃
        jta.setEditable(false); // 에디팅 불가
        jta.setLineWrap(true); // 자동줄바꿈
 
        JScrollPane jsp = new JScrollPane(jta); // 텍스트에어리어에 스크롤 추가
        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jp.add(jsp);// 패널에 스크롤 붙임
        jta.setText("Server Start...1\n");
 
        add(jp); // 프레임에 패널 붙임
 
        // 윈도우 위치 계산
        whole = Toolkit.getDefaultToolkit().getScreenSize();
        part = this.getSize();
        xPos = (int) (whole.getWidth() / 2 - part.getWidth() / 2);
        yPos = (int) (whole.getHeight() / 2 - part.getHeight() / 2);
 
        setLocation(xPos, yPos);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        // Create server UI
        System.out.println("Server start...2");
        EightServer server = new EightServer();
        Thread thread = new Thread(server);
        thread.start();
    }
 
    @Override
    public void run() {
        // 클라이언트 대기 모드
 
        // 서버소켓 생성
        try {
            InetAddress addr = InetAddress.getLocalHost(); // 로컬호스트 주소
            serverSocket = new ServerSocket(PORT); // 서버소켓 생성
            jta.append(PORT + "번 포트로 정상적으로 소켓이 생성되었습니다.\n" + "현재 열린 서버의 IP 주소는 " 
                            + addr.getHostAddress().toString() + "입니다. \n");
        } catch (IOException e1) {
            e1.printStackTrace();
            jta.append("서버 소켓 생성에러\n");
        }
 
        while (true) {
            socket = null;
            dis = null;
            dos = null;
            try {
                // 무한반복, 입출력 에러가 나거나 프로그램이 종료될 때까지 실행
                socket = serverSocket.accept(); // 클라이언트 접속 대기
                jta.append("클라이언트 " + socket.getInetAddress().getHostAddress()    + "가 접속되었습니다.\n");
 
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                jta.append("클라이언트 접속에러\n");
            }
            try {
                // 스트림 객체 생성
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    dis.close();
                    dos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    jta.append("스트림 해제에러\n");
                }
                try {
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    jta.append("소켓 해제에러\n");
                }
                jta.append("스트림 생성에러\n");
            }
            User person = new User(dis, dos); // 가명의 사용자 객체 생성
            person.setIP(socket.getInetAddress().getHostName()); // 아이피주소 설정 부여
 
            Thread thread = new Thread(new ServerThread(jta, person, userArray,    roomArray));
            thread.start(); // 스레드 시작
        }
    }
}
cs


이러한 창을 띄워 잘못되는점이나 오류를 확인하게 하였다.

'* Project > Chatting_Pro' 카테고리의 다른 글

채팅 프로그램 마무리  (0) 2016.09.07
대기화면 소스(WaitRoomUI.java)  (0) 2016.09.07
회원 수정 DB / 회원 탈퇴 DB  (1) 2016.09.07
로그인 체크 DB  (1) 2016.09.07
회원가입 DB / 메시지박스  (1) 2016.09.07

+ Recent posts