




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、服務(wù)器代碼:WindowServer.javaimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import javax.swing.JButton;import javax.swing
2、.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.border.TitledBorder;public class WindowServer public static JFrame window;public static JTextArea tex
3、tMessage;/聊天記錄public static JList<String> user;/用戶列表public static int ports;JButton start,send,exit;JTextField portServer,message,name;/主函數(shù)入口public static void main(String args) new WindowServer();/初始化窗體public WindowServer() init();/初始化內(nèi)容public void init()/采用絕對(duì)布局window = new JFrame("服務(wù)端&q
4、uot;);window.setLayout(null);window.setBounds(200, 200, 500, 400);window.setResizable(false);/不可改變大小JLabel label1 = new JLabel("端口號(hào):");label1.setBounds(10, 8, 50, 30);window.add(label1);portServer = new JTextField();portServer.setBounds(60, 8, 100, 30);portServer.setText("30000")
5、;window.add(portServer);JLabel names = new JLabel("用戶名:");names.setBounds(180, 8, 55, 30);window.add(names);name = new JTextField();name.setBounds(230, 8, 60, 30);name.setText("服務(wù)端");window.add(name);start = new JButton("啟動(dòng)");start.setBounds(300, 8, 80, 30);window.add(s
6、tart);exit = new JButton("關(guān)閉");exit.setBounds(390, 8, 80, 30);window.add(exit);JLabel label2 = new JLabel("用戶列表");label2.setBounds(40, 40, 80, 30);window.add(label2);user = new JList<String>();JScrollPane scrollPane = new JScrollPane(user);/添加滾動(dòng)條scrollPane.setBounds(10, 70,
7、 120, 220);window.add(scrollPane);textMessage = new JTextArea();textMessage.setBounds(135, 70, 340, 220);textMessage.setBorder(new TitledBorder("聊天記錄");/設(shè)置標(biāo)題textMessage.setEditable(false);/不可編輯/文本內(nèi)容換行的兩個(gè)需要配合著用textMessage.setLineWrap(true);/設(shè)置文本內(nèi)容自動(dòng)換行,在超出文本區(qū)域時(shí),可能會(huì)切斷單詞textMessage.setWrapStyl
8、eWord(true);/設(shè)置以自動(dòng)換行,以單詞為整體,保證單詞不會(huì)被切斷JScrollPane scrollPane1 = new JScrollPane(textMessage);/設(shè)置滾動(dòng)條scrollPane1.setBounds(135, 70, 340, 220);window.add(scrollPane1);message = new JTextField();message.setBounds(10, 300, 360, 50);window.add(message);send = new JButton("發(fā)送");send.setBounds(380,
9、 305, 70, 40);window.add(send);myEvent();/添加監(jiān)聽(tīng)事件window.setVisible(true);public void myEvent()window.addWindowListener(new WindowAdapter() /關(guān)閉窗體public void windowClosing(WindowEvent e)/如果有客戶端存在,發(fā)信息給客戶端,并退出if(StartServer.userList != null && StartServer.userList.size() != 0)try new SendServer(S
10、tartServer.userList, "" , "4");/4代表服務(wù)端退出 catch (IOException e1) e1.printStackTrace();System.exit(0);/退出窗體);exit.addActionListener(new ActionListener() /退出連接public void actionPerformed(ActionEvent e) if(StartServer.ss = null | StartServer.ss.isClosed()/如果已退出,彈窗提醒JOptionPane.showMe
11、ssageDialog(window, "服務(wù)器已關(guān)閉");else /發(fā)信息告訴客戶端,要退出if(StartServer.userList != null && StartServer.userList.size() != 0)try new SendServer(StartServer.userList, "" , 4 + ""); catch (IOException e1) e1.printStackTrace();try start.setText("啟動(dòng)");exit.setText(
12、"已關(guān)閉");StartServer.ss.close();/關(guān)閉服務(wù)端StartServer.ss = null;StartServer.userList = null;StartServer.flag = false;/改變服務(wù)端循環(huán)標(biāo)記 catch (IOException e1) e1.printStackTrace(););/開(kāi)啟服務(wù)端start.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) /如果服務(wù)端已經(jīng)開(kāi)啟,彈窗提醒服務(wù)端已開(kāi)啟if(Star
13、tServer.ss != null && !StartServer.ss.isClosed()JOptionPane.showMessageDialog(window, "服務(wù)器已經(jīng)啟動(dòng)");else ports = getPort();/獲取端口號(hào)if(ports != 0)try StartServer.flag = true;/改變服務(wù)端接收循環(huán)標(biāo)記new Thread(new StartServer(ports).start(); /開(kāi)啟服務(wù)端接收線程start.setText("已啟動(dòng)");exit.setText("
14、;關(guān)閉"); catch (IOException e1) JOptionPane.showMessageDialog(window, "啟動(dòng)失敗"););/點(diǎn)擊按鈕發(fā)送消息send.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) sendMsg(););/按回車(chē)發(fā)送消息message.addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) if(e.getKeyCode(
15、) = KeyEvent.VK_ENTER)sendMsg(););/發(fā)送消息方法public void sendMsg()String messages = message.getText();/判斷內(nèi)容是否為空if("".equals(messages)JOptionPane.showMessageDialog(window, "內(nèi)容不能為空!");else if(StartServer.userList = null | StartServer.userList.size() = 0)/判斷是否已經(jīng)連接成功JOptionPane.showMessa
16、geDialog(window, "未連接成功,不能發(fā)送消息!");else try /將信息發(fā)送給所有客戶端new SendServer(StartServer.userList, getName() + ":" + messages, 1 + "");/將信息添加到客戶端聊天記錄中WindowServer.textMessage.append(getName() + ":" + messages + "rn");message.setText(null);/消息框設(shè)置為空 catch (IO
17、Exception e1) JOptionPane.showMessageDialog(window, "發(fā)送失?。?quot;);/獲取端口號(hào)public int getPort()String port = portServer.getText();if("".equals(port)/判斷端口號(hào)是否為空J(rèn)OptionPane.showMessageDialog(window, "端口號(hào)為口");return 0;else return Integer.parseInt(port);/返回整形的端口號(hào)/獲取服務(wù)端名稱public Strin
18、g getName()return name.getText();StartServer.javaimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .ServerSocket;import .Socket;import java.util.ArrayList;import java.util.Vector;import javax.swing.JOptionPane;/啟動(dòng)服務(wù)端接收客戶端的線程pu
19、blic class StartServer implements Runnableprivate int port;public static ArrayList<Socket> userList = null;public static Vector<String> userName = null;public static ServerSocket ss = null;public static boolean flag = true;/傳入端口號(hào)public StartServer(int port) throws IOExceptionthis.port =
20、port;public void run() Socket s = null;userList = new ArrayList<Socket>();/客戶端端口容器userName = new Vector<String>();/用戶名稱容器/System.out.println("啟動(dòng)服務(wù)端");try ss = new ServerSocket(port);/啟動(dòng)服務(wù)端 catch (IOException e1) e1.printStackTrace();while (flag) /開(kāi)啟循環(huán),等待接收客戶端try s = ss.accept()
21、;/接收客戶端userList.add(s);/將客戶端的socket添加到容器中/打印客戶端信息String id = s.getInetAddress().getHostName();System.out.println(id + "-connected");System.out.println("當(dāng)前客戶端個(gè)數(shù)為:" + userList.size();/啟動(dòng)與客戶端相對(duì)應(yīng)的信息接收線程new Thread(new ReceiveServer(s,userList,userName).start(); catch (IOException e) J
22、OptionPane.showMessageDialog(WindowServer.window, "服務(wù)端退出!");/服務(wù)端信息接收線程class ReceiveServer implements Runnableprivate Socket socket;private ArrayList<Socket> userList;private Vector<String> userName;public ReceiveServer(Socket s,ArrayList<Socket> userList,Vector<String&
23、gt; userName) this.socket = s;this.userList = userList;this.userName = userName;public void run() try /讀取信息流BufferedReader brIn = new BufferedReader(new InputStreamReader(socket.getInputStream();while(true)char info = (char)brIn.read();/先讀取信息流的首字符,并判斷信息類(lèi)型String line = brIn.readLine();/讀取信息流的信息內(nèi)容if(i
24、nfo = '1')/1代表收到的是信息WindowServer.textMessage.append(line + "rn");/將信息添加到服務(wù)端聊天記錄中/設(shè)置消息顯示最新一行,也就是滾動(dòng)條出現(xiàn)在末尾,顯示最新一條輸入的信息WindowServer.textMessage.setCaretPosition(WindowServer.textMessage.getText().length();new SendServer(userList, line, "1");/將信息轉(zhuǎn)發(fā)給客戶端if(info = '2')/2代
25、表有新客戶端建立連接userName.add(line);/將新客戶端用戶名添加到容器中WindowServer.user.setListData(userName);/更新服務(wù)端用戶列表new SendServer(userList, userName, "2");/將用戶列表以字符串的形式發(fā)給客戶端if(info = '3')/3代表有用戶端退出連接userName.remove(line);/移除容器中已退出的客戶端用戶名userList.remove(socket);/移除容器中已退出的客戶端的socketWindowServer.user.setL
26、istData(userName);/更新服務(wù)端用戶列表new SendServer(userList, userName, "3");/將用戶列表以字符串的形式發(fā)給客戶端socket.close();/關(guān)閉該客戶端的socketbreak;/結(jié)束該客戶端對(duì)于的信息接收線程 catch (IOException e) e.printStackTrace();/服務(wù)端發(fā)送信息class SendServer SendServer(ArrayList<Socket> userList,Object message,String info) throws IOExce
27、ptionString messages = info + message;/添加信息頭標(biāo)記PrintWriter pwOut = null;for(Socket s : userList)/將信息發(fā)送給每個(gè)客戶端pwOut = new PrintWriter(s.getOutputStream(),true);pwOut.println(messages);客戶端代碼:WindowClient.javaimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.Key
28、Adapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import .Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.s
29、wing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.border.TitledBorder;public class WindowClient JTextField port,name,ip,message;JButton send;public static JFrame window;public static JButton link,exit;public static JTextArea textMessage;public static Sock
30、et socket = null;public static JList<String> user;/主函數(shù)入口public static void main(String args) new WindowClient();/初始化窗體public WindowClient() init();/窗體初始化內(nèi)容public void init()/采用絕對(duì)布局window = new JFrame("客戶端");window.setLayout(null);window.setBounds(200, 200, 500, 400);window.setResizab
31、le(false);JLabel label = new JLabel("主機(jī)IP:");label.setBounds(10, 8, 50, 30);window.add(label);ip = new JTextField();ip.setBounds(55, 8, 60, 30);ip.setText("127.0.0.1");window.add(ip);JLabel label1 = new JLabel("端口號(hào):");label1.setBounds(125, 8, 50, 30);window.add(label1);
32、port = new JTextField();port.setBounds(170, 8, 40, 30);port.setText("30000");window.add(port);JLabel names = new JLabel("用戶名:");names.setBounds(220, 8, 55, 30);window.add(names);name = new JTextField();name.setBounds(265, 8, 60, 30);name.setText("客戶端1");window.add(name)
33、;link = new JButton("連接");link.setBounds(335, 8, 75, 30);window.add(link);exit = new JButton("退出");exit.setBounds(415, 8, 75, 30);window.add(exit);JLabel label2 = new JLabel("用戶列表");label2.setBounds(40, 40, 80, 30);window.add(label2);user = new JList<String>();JSc
34、rollPane scrollPane = new JScrollPane(user);/設(shè)置滾動(dòng)條scrollPane.setBounds(10, 70, 120, 220);window.add(scrollPane);textMessage = new JTextArea();textMessage.setBounds(135, 70, 340, 220);textMessage.setEditable(false);/文本不可編輯textMessage.setBorder(new TitledBorder("聊天記錄");/設(shè)置標(biāo)題/文本內(nèi)容換行的兩個(gè)需要配合著用t
35、extMessage.setLineWrap(true);/設(shè)置文本內(nèi)容自動(dòng)換行,在超出文本區(qū)域時(shí),可能會(huì)切斷單詞textMessage.setWrapStyleWord(true);/設(shè)置以自動(dòng)換行,以單詞為整體,保證單詞不會(huì)被切斷JScrollPane scrollPane1 = new JScrollPane(textMessage);/設(shè)置滾動(dòng)條scrollPane1.setBounds(135, 70, 340, 220);window.add(scrollPane1);message = new JTextField();message.setBounds(10, 300, 360
36、, 50);message.setText(null);window.add(message);send = new JButton("發(fā)送");send.setBounds(380, 305, 70, 40);window.add(send);myEvent();/添加監(jiān)聽(tīng)事件window.setVisible(true);/設(shè)置窗體可見(jiàn)public void myEvent()/事件監(jiān)聽(tīng)window.addWindowListener(new WindowAdapter() /退出窗體public void windowClosing(WindowEvent e)/如果
37、仍在連接,發(fā)信息給服務(wù)端,并退出if(socket != null && socket.isConnected()try new SendClient(socket, getName(), 3 + ""); catch (IOException e1) e1.printStackTrace();System.exit(0););/關(guān)閉連接exit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) /如果仍在連接,將信息發(fā)給服務(wù)端if(socket
38、 = null)JOptionPane.showMessageDialog(window, "已關(guān)閉連接");else if(socket != null && socket.isConnected()try new SendClient(socket, getName(), "3");/發(fā)送信息給服務(wù)端link.setText("連接");exit.setText("已退出");socket.close();/關(guān)閉socketsocket = null; catch (IOException e1
39、) e1.printStackTrace(););/建立連接link.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) /判斷是否已經(jīng)連接成功if(socket != null && socket.isConnected()JOptionPane.showMessageDialog(window, "已經(jīng)連接成功!");else String ipString = ip.getText(); /獲取IP地址String portClinet =
40、port.getText();/獲取端口號(hào)if("".equals(ipString) | "".equals(portClinet)/判斷獲取內(nèi)容是否為空J(rèn)OptionPane.showMessageDialog(window, "ip或端口號(hào)為空!");else try int ports = Integer.parseInt(portClinet);/將端口號(hào)轉(zhuǎn)為整形socket = new Socket(ipString,ports);/建立連接link.setText("已連接");/更改button顯示
41、信息exit.setText("退出");new SendClient(socket, getName(), 2 + "");/發(fā)送該客戶端名稱至服務(wù)器new Thread(new ReceiveClient(socket).start();/啟動(dòng)接收線程 catch (Exception e2) JOptionPane.showMessageDialog(window, "連接未成功!可能是ip或端口號(hào)格式不對(duì),或服務(wù)器未開(kāi)啟。"););/點(diǎn)擊按鈕發(fā)送信息send.addActionListener(new ActionListen
42、er() public void actionPerformed(ActionEvent e) sendMsg(););/按回車(chē)發(fā)送信息message.addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) if(e.getKeyCode() = KeyEvent.VK_ENTER)sendMsg(););/發(fā)送信息的方法public void sendMsg()String messages = message.getText();/獲取文本框內(nèi)容if("".equals(messages)/判
43、斷信息是否為空J(rèn)OptionPane.showMessageDialog(window, "內(nèi)容不能為空!");else if(socket = null | !socket.isConnected()/判斷是否已經(jīng)連接成功JOptionPane.showMessageDialog(window, "未連接成功,不能發(fā)送消息!");else try /發(fā)送信息new SendClient(socket,getName() + ":" + messages,"1");message.setText(null);/文本框內(nèi)容設(shè)置為空 catch (IOException e1) JOptionPane.showMessageDialog(window, "信息發(fā)送失敗!");/獲取客戶端名稱public String getName()return name.getText();StartClient.javaimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.Pri
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)院招聘護(hù)士合同范例
- 公共器材租賃合同范本
- 合同范本 合作
- 公司勞動(dòng)用工合同范本簡(jiǎn)版
- 售后回租合同范本
- 出租野餐工具合同范本
- 合同范本些可以修改
- 化妝造型合同范本
- 華能電廠合同范本
- 員工內(nèi)部合同范本
- 安全生產(chǎn)法律法規(guī)匯編(2025版)
- 義務(wù)教育化學(xué)課程標(biāo)準(zhǔn)(2022年版)解讀
- 生產(chǎn)加工型小微企業(yè)安全管理考試(含答案)
- 2《幼苗長(zhǎng)大了》課件
- 日常監(jiān)督檢查表
- 隊(duì)列訓(xùn)練教程ppt課件(PPT 86頁(yè))
- 第三章-農(nóng)村公共管理組織課件
- 注塑員工培訓(xùn)
- JMP操作簡(jiǎn)要培訓(xùn)
- 勝利油田壓驅(qū)技術(shù)工藝研究進(jìn)展及下步工作方向
- 研究生復(fù)試匯報(bào)ppt
評(píng)論
0/150
提交評(píng)論