/** Start talking to the server */ public void start() { String codehost = getCodeBase().getHost(); if (socket == null) { try { // Open the socket to the server socket = new Socket(codehost, port); // Create output stream out = new ObjectOutputStream(socket.getOutputStream()); out.flush(); // Create input stream and start background // thread to read data from the server in = new ObjectInputStream(socket.getInputStream()); new Thread(this).start(); } catch (Exception e) { // Exceptions here are unexpected, but we can't // really do anything (so just write it to stdout // in case someone cares and then ignore it) System.out.println("Exception! " + e.toString()); e.printStackTrace(); setErrorStatus(STATUS_NOCONNECT); socket = null; } } else { // Already started } if (socket != null) { // Make sure the right buttons are enabled start_button.setEnabled(false); stop_button.setEnabled(true); setStatus(STATUS_ACTIVE); } }
/** Stop talking to the server */ public void stop() { if (socket != null) { // Close all the streams and socket if (out != null) { try { out.close(); } catch (IOException ioe) { } out = null; } if (in != null) { try { in.close(); } catch (IOException ioe) { } in = null; } if (socket != null) { try { socket.close(); } catch (IOException ioe) { } socket = null; } } else { // Already stopped } // Make sure the right buttons are enabled start_button.setEnabled(true); stop_button.setEnabled(false); setStatus(STATUS_STOPPED); }
// メッセージ監視用のスレッド public void run() { try { InputStream input = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); while (!socket.isClosed()) { String line = reader.readLine(); String[] msg = line.split(" ", 2); String msgName = msg[0]; String msgValue = (msg.length < 2 ? "" : msg[1]); reachedMessage(msgName, msgValue); } } catch (Exception err) { } }
public void connect(String user, String pass) { if (!connected) { try { socket = new Socket(domain, 4446); out = new PrintWriter(socket.getOutputStream(), true); // in = new BufferedReader(new InputStreamReader(socket.getInputStream())); in = new ObjectInputStream(socket.getInputStream()); } catch (java.net.UnknownHostException e) { System.err.println("Don't know about host"); return; } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to"); return; } this.connected = true; rec = new Receiver(in); rec.start(); } }
// メッセージをサーバーに送信する public void sendMessage(String msg) { try { OutputStream output = socket.getOutputStream(); PrintWriter writer = new PrintWriter(output); writer.println(msg); writer.flush(); } catch (Exception err) { msgTextArea.append("ERROR>" + err + "\n"); } }
public void disconnect() { int cmdID = commID++; this.connected = false; try { out.println(cmdID + ";logout"); out.flush(); in.close(); out.close(); socket.close(); } catch (IOException ex) { System.err.println("Server stop failed."); } }
public void launchChess() { mainWindow = new JFrame("网络黑白棋 作者:张炀"); jpWest = new JPanel(); jpEast = new JPanel(); jpNorth = new JPanel(); jpSouth = new JPanel(); jpCenter = new JPanel(); turnLabel = new JLabel(); blackNumberLabel = new JLabel("黑棋:\n" + black + " "); whiteNumberLabel = new JLabel("白棋:\n" + white + " "); timeLabel = new JLabel("时间: " + time + " s"); noticeLabel = new JLabel(); noticeLabel = new JLabel("请选择:"); numberPane = new JPanel(); readyPane = new JPanel(); jt1 = new JTextField(18); // 发送信息框 jt2 = new JTextArea(3, 30); // 显示信息框 js = new JScrollPane(jt2); jt2.setLineWrap(true); jt2.setEditable(false); // jt1.setLineWrap(true); send = new JButton("发送"); cancel = new JButton("取消"); regret = new JButton("悔棋"); submit = new JButton("退出"); begin = new JButton("开始"); save = new JButton("存盘"); save.setEnabled(true); begin.setEnabled(true); fighter = new JRadioButton("下棋"); audience = new JRadioButton("观看"); group = new ButtonGroup(); group.add(audience); group.add(fighter); group.add(AIPlayer); submit.addActionListener(this); begin.addActionListener(this); save.addActionListener(this); send.addActionListener(this); cancel.addActionListener(this); regret.addActionListener(this); fighter.addActionListener(this); audience.addActionListener(this); jpNorth.setLayout(new BorderLayout()); jpNorth.add(turnLabel, BorderLayout.NORTH); jpNorth.add(jpCenter, BorderLayout.CENTER); jpSouth.add(js); jpSouth.add(jt1); jpSouth.add(send); jpSouth.add(cancel); jpEast.setLayout(new GridLayout(3, 1)); submit.setBackground(new Color(130, 251, 241)); panel x = new panel(); jpEast.add(x); jpEast.add(numberPane); jpEast.add(readyPane); numberPane.add(blackNumberLabel); numberPane.add(whiteNumberLabel); numberPane.add(timeLabel); numberPane.add(submit); numberPane.add(begin); numberPane.add(save); numberPane.add(regret); readyPane.add(noticeLabel); readyPane.add(fighter); readyPane.add(audience); // readyPane.add(save); // readyPane.add(regret); jpCenter.setSize(400, 400); jmb = new JMenuBar(); document = new JMenu("游戏 "); edit = new JMenu("设置 "); insert = new JMenu("棋盘 "); help = new JMenu("帮助 "); jmb.add(document); jmb.add(edit); jmb.add(insert); jmb.add(help); // mainWindow.setJMenuBar(jmb); mainWindow.setBounds(80, 80, 600, 600); mainWindow.setResizable(false); jsLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, jpNorth, jpSouth); jsBase = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, jsLeft, jpEast); mainWindow.add(jsBase); mainWindow.setVisible(true); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jsBase.setDividerLocation(0.7); jsLeft.setDividerLocation(0.78); jpCenter.setLayout(new GridLayout(8, 8, 0, 0)); ImageIcon key = new ImageIcon("chess1.jpg"); for (int i = 0; i < cell.length; i++) for (int j = 0; j < cell.length; j++) { cell[i][j] = new ChessBoard(i, j); cell[i][j].addMouseListener(this); jpCenter.add(cell[i][j]); } cell[3][3].state = cell[4][4].state = '黑'; cell[4][3].state = cell[3][4].state = '白'; cell[3][3].taken = cell[4][4].taken = true; cell[4][3].taken = cell[3][4].taken = true; RememberState(); new Thread(new TurnLabel(this)).start(); file = new File("D:/" + myRole); try { fout = new FileOutputStream(file); out = new ObjectOutputStream(fout); } catch (IOException e1) { e1.printStackTrace(); } Connect(); try { out99 = new ObjectOutputStream(socket99.getOutputStream()); in99 = new ObjectInputStream(socket99.getInputStream()); out66 = new ObjectOutputStream(socket66.getOutputStream()); in66 = new ObjectInputStream(socket66.getInputStream()); out88 = new DataOutputStream(socket88.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } new Thread(new Client9999(this)).start(); new Thread(new Client6666(this)).start(); }
// サーバーから切断する public void close() throws IOException { sendMessage("close"); socket.close(); }