java服務(wù)器與客戶端源碼

上傳人:文*** 文檔編號:62438425 上傳時間:2022-03-14 格式:DOC 頁數(shù):21 大小:75KB
收藏 版權(quán)申訴 舉報 下載
java服務(wù)器與客戶端源碼_第1頁
第1頁 / 共21頁
java服務(wù)器與客戶端源碼_第2頁
第2頁 / 共21頁
java服務(wù)器與客戶端源碼_第3頁
第3頁 / 共21頁

下載文檔到電腦,查找使用更方便

0 積分

下載資源

還剩頁未讀,繼續(xù)閱讀

資源描述:

《java服務(wù)器與客戶端源碼》由會員分享,可在線閱讀,更多相關(guān)《java服務(wù)器與客戶端源碼(21頁珍藏版)》請在裝配圖網(wǎng)上搜索。

1、文檔供參考,可復制、編制,期待您的好評與關(guān)注! 服務(wù)器(server) 1 類ServerTest 1 類ChatClient 2 類MyQueue 3 類ReceiveFromClient 4 類SendToClientThread 11 客戶端(client) 12 類StudentWindow 12 類StudentDuQu 15 類ClientTest 19 類BaoCunJiLu 20 服務(wù)器(server) 類ServerTest package Server_System; import java.io.IOException; i

2、mport .ServerSocket; import .Socket; import java.util.Vector; import Client_System.TeacherWindow; public class ServerTest extends Thread { private int number; private Vector allClient = new Vector(); private MyQueue chatCon = new MyQueue();

3、public ServerTest(int number) { this.number=number; this.start(); } public void run() { ServerSocket server =null; try { server = new ServerSocket(number); } catch (IOException e1) { // TODO Auto-generated catch block e1.printSta

4、ckTrace(); } new SendToClientThread(chatCon,allClient); while(true) { try { Socket ss=server.accept(); ChatClient cc=new ChatClient(ss); //System.out.println(cc.toString()); allClient.add(cc); //String name=ss.getInetAddres

5、s().toString(); chatCon.offer(cc.IP+"來聊天了。。。"); new ReceiveFromClient(cc,chatCon,allClient ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 類ChatClient package Server_System; import jav

6、a.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; import .Socket; import Mysql_System.User; public class ChatClient { public Socket so; public String IP; public Pri

7、ntStream out; public BufferedReader in; public ChatClient(Socket so) throws Exception { this.so=so; Reader r = new InputStreamReader(so.getInputStream()); in= new BufferedReader(r); OutputStream os = so.getOutputStream(); out = new PrintStream(os); IP= so.getInetAddr

8、ess().toString(); } //把聊天信息發(fā)到客戶端 public void sendToClient(String word) { out.println(word);out.flush(); } //從客戶端讀聊天信息 public String readFromClient() throws Exception { String str = null; try { str = in.readLine(); } catch (Exception e) { // TODO Au

9、to-generated catch block e.printStackTrace(); } //System.out.println(str); return str; } } 類MyQueue package Server_System; import java.util.LinkedList; public class MyQueue { private LinkedList l=new LinkedList(); public boolean offer(Object e) {

10、 return l.offer(e); } public Object poll() { return l.poll(); } public int size() { return l.size(); } } 類ReceiveFromClient package Server_System; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; impor

11、t java.io.IOException; import java.util.Vector; public class ReceiveFromClient extends Thread{ MyQueue chatCon; ChatClient cc; BufferedReader br; Vector allClient; int nua1 = 0; int nua2 = 0; int nua3 = 0; public ReceiveFromClient(ChatClient cc,MyQueue

12、chatCon,Vector allClient ) throws Exception { this.cc=cc;this.chatCon=chatCon;this.allClient=allClient; this.start(); } public void run() { String line ; while(true) { try { line=cc.readFromClient

13、(); if(line==null) { //count++; allClient.remove(cc); chatCon.offer(cc.IP+"逃跑了。。。。"); return; } else if(line.equals("getAll")) { int a=allClient.size(); String renshu=String.valueOf(a); cc.sendToClient("#/當前登錄的人數(shù):"+renshu

14、); for(ChatClient v:allClient) { cc.sendToClient("IP:#"+v.IP); } } else if(line.equals("keywindow##")) { for(ChatClient v:allClient) { if(cc.IP.equals(v.IP)) { cc.sendToClient("鎖屏指令已經(jīng)發(fā)出。。"); } else {

15、 v.sendToClient("keywindow##"); } } } else if(line.equals("releseWindow##")) { for(ChatClient v:allClient) { if(cc.IP.equals(v.IP)) { cc.sendToClient("解鎖指令已經(jīng)發(fā)出。。"); } else { v.sendToClient("releseWindow##");

16、 } } } else if(line.startsWith("SendFile##")) { String [] str = line.split("##"); //文件的絕對路徑 String filename = str[1]; String [] str1 = filename.split("/"); //文件名 String name =str1[str1.length-1]; //cc.sendToClient("SendFile##"+fil

17、ename); for(ChatClient v:allClient) { if(cc.IP.equals(v.IP)) { cc.sendToClient("文件共享指令已經(jīng)發(fā)出。。"); } else { v.sendToClient("SaveFile###"+name); } } new SendFile(filename,cc,allClient); } //鎖屏

18、 else if(line.startsWith("Personkeywindow##")) { String [] str = line.split("##IP:"); String str1 = str[1]; for(ChatClient v:allClient) { if(v.IP.equals(str1)) { v.sendToClient("keywindow##"); } } } //解鎖 else if(l

19、ine.startsWith("PersonreleseWindow##")) { String [] str = line.split("##IP:"); String str1 = str[1]; for(ChatClient v:allClient) { if(v.IP.equals(str1)) { v.sendToClient("releseWindow##"); } } } //監(jiān)控全部人員的窗口

20、 else if(line.equals("wo##yaojian##kongAll")) { String str2 = cc.IP; for(ChatClient v:allClient) { if(cc.IP.equals(v.IP)) { cc.sendToClient("監(jiān)控所有人的指令已經(jīng)發(fā)出。。"); } else { v.sendToClient("wo##yaojian##kongImage"+str2

21、); } } } //監(jiān)控某個人的窗口 else if(line.startsWith("wo##yaojian##kongOne")) { String [] str = line.split("OneIP:"); String str1 = str[1]; String str2 = cc.IP; for(ChatClient v:allClient) { if(v.IP.equals(str1)) { v.sendToC

22、lient("wo##yaojian##kongImage"+str2); } } } //投票的操作 else if(line.equals("Teacher##liaojie##StudenT")) { for(ChatClient v:allClient) { if(cc.IP.equals(v.IP)) { cc.sendToClient("投票系統(tǒng)的指令已經(jīng)發(fā)出。。"); } else {

23、 v.sendToClient("Teacher##liaojie##StudenT"+cc.IP); } } } //投票系統(tǒng) else if(line.startsWith("Student##laiojie##")) { int count = allClient.size()-1; String [] sr = line.split("##laiojie##"); String sr1 = sr[1]; String []sr2 =sr1

24、.split("###"); String IP = sr2[0]; String xinxi = sr2[1]; String str = null; if(xinxi.equals("budong")) { nua1++; str = "不懂"; } if(xinxi.equals("youdiandong")) { nua2++; str = "有點懂"; } if(xinxi.equals("wanquando

25、ng")) { nua3++; str = "完全懂"; } int nua = nua1+nua2+nua3; if(nua==count) { chatCon.offer("投票的結(jié)果:"+"不懂的人數(shù):"+nua1+"人;"+"有點懂的人數(shù):"+nua2+"人;"+"完全懂的人數(shù):"+nua3+"人。"); nua1=0; nua2=0; nua3=0; } for(ChatClient v:allCli

26、ent) { if(v.IP.equals(IP)) { v.sendToClient("#lai#3Taxi#2wgt###"+cc.IP+"說:"+str); } } } //私人聊天信息的接受 else if(line.startsWith("#singleone#IP:")) { String str2; String [] str = line.split("#Pe

27、rson##"); String str1 = str[0]; if(str.length==1) { str2 = null; } else { str2 = str[1]; } String [] str3 = str1.split("one#IP:"); String str4 = str3[1]; for(ChatClient v:allClient) { if(v.IP.equals(str4))

28、 { v.sendToClient(cc.IP+"對你的悄悄話:"+str2); cc.sendToClient("你悄悄對"+cc.IP+"說:"+str2); } } } else {chatCon.offer(cc.IP + "說:"+line);} //System.out.println(who+"說:"+line); } catch (Exception e) { // TODO Auto-generated catc

29、h block allClient.remove(cc); chatCon.offer(cc.IP+"逃跑了。。。。"); } } } } 類SendToClientThread package Server_System; import java.util.Vector; public class SendToClientThread extends Thread{ public static MyQueue chatCon; publi

30、c static Vector allClient; public SendToClientThread(MyQueue chatCon,Vector allClient) { this.chatCon=chatCon; this.allClient=allClient; this.start(); } public void run() { System.out.println("發(fā)送線程啟動。。。。"); while(t

31、rue) { if(chatCon.size()==0) { try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { String word=(String)chatCon.poll(); for(

32、ChatClient aa:allClient) { aa.sendToClient(word); } } } } } 客戶端(client) 類StudentWindow package Client_System; public StudentWindow(String username) { super(username+"同學,歡迎進入此界面"); jp.setLayout(new FlowLay

33、out(FlowLayout.CENTER,8,1)); jp.add(ip);jp.add(ipt);jp.add(duankou);jp.add(duankout);jp.add(lianjie); // menu1.add(Item1);menu1.add(Item2);menu2.add(Item3);menu2.add(Item4); // menuBar.add(menu1);menuBar.add(menu2);menuBar.add(jb1);menuBar.add(jb2);menuBar.add(jb3); // this.setJMenuBar(

34、menuBar); jp1.setLayout(new BorderLayout(5, 20)); jp2.setLayout(new FlowLayout(FlowLayout.CENTER,16,1)); // jp2.add(jb1);jp2.add(jb2);jp2.add(jb3); jp1.add(jp2,"North"); jp3.setLayout(new BorderLayout(5, 20)); jta.setFont(f);jta.setEditable(false); jtta.setFont(f1); jp3.add(js

35、p1,"Center");jp3.add(jtta,"South"); jp1.add(jp3,"Center"); jp8.setLayout(new FlowLayout(FlowLayout.CENTER,16,1)); jp8.add(jb4);jp8.add(jb5); jp1.add(jp8,"South"); jp4.setLayout(new BorderLayout(5, 10)); jp5.setLayout(new GridLayout(3,1)); jp5.add(jb);jp5.add(jl);jp5.add(jl1)

36、; jp4.add(jp5,"North"); jp4.add(jsp3,"Center"); jp6.setLayout(new FlowLayout(FlowLayout.CENTER,8,1)); jp6.add(jb6); jp4.add(jp6,"South"); this.add(jp,"North");this.add(jp1,"Center");this.add(jp4,"East"); this.setSize(600,600); this.setResizable(false); this.setLocat

37、ion(400, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lianjie.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str1 = ipt.getText(); String str2 = duankout.g

38、etText(); if(!str1.equals("") && !str2.equals("")) { Integer num = new Integer(str2); try { so=new Socket(str1,num); new StudentDuQu(so,jta); } catch (Exception e1) { jta.append("你輸入的服務(wù)器IP地址或端口不存在!"+"\r\n"); } } }

39、}); jb5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str = jtta.getText(); //System.out.println(str); new ClientTest(str,so); jtta.setText(""); } }); jb.addActionListen

40、er(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { l.removeAll(); String srt = "getAll"; new ClientTest(srt,so); } }); jb4.addActionListener(new ActionListener() { @Override public void actionPerfo

41、rmed(ActionEvent e) { String text = jta.getText(); System.out.println(text); JFrame jf1 = new JFrame("保存文件"); FileDialog gd = new FileDialog(jf1,"保存文件",FileDialog.SAVE); //jf1.setSize(400,400); gd.setVisible(true); String filename = gd.getDirectory()+gd.getFile();

42、 new BaoCunJiLu(text,filename); } }); //私人聊天系統(tǒng) jb6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str1 = l.getSelectedItem(); String str2 = jtta.getText(); String str3 = "

43、#singleone#"+str1+"#Person##"+str2; new ClientTest(str3,so); jtta.setText(""); } }); } } 類StudentDuQu import javax.swing.JTextArea; public class StudentDuQu extends Thread{ int i=0; private Socket so; private InputStream in; private B

44、ufferedReader bf; private JTextArea jt; String line =null; StudentWindow tw; String name; //JFrame jf = new JFrame(); JDialog jd = new JDialog(); public StudentDuQu(Socket so,JTextArea jt) throws IOException { this.so=so; this.jt=jt; in=so.get

45、InputStream(); Reader r=new InputStreamReader(in); bf=new BufferedReader(r); this.start(); } public void run() { jt.append("從服務(wù)器端讀取的線程已經(jīng)成功起動"+"\r\n"); jd.setUndecorated(true); while(true){ try { line = bf.readLine(); if(line==n

46、ull) { jt.append("警告:服務(wù)器已經(jīng)關(guān)閉!"); break; } else if(line.startsWith("#/")) { String [] str = line.split("/"); tw.jl.setText(str[1]); } else if(line.startsWith("IP:#")) { String [] str = line.split("#"); tw.l.add(str[0]

47、+str[1]); } else if(line.equals("keywindow##")) { boolean falg = true; new SuoPing(jd,falg); } else if(line.equals("releseWindow##")) { new SuoPing(jd,false); } //文件的共享 else if(line.startsWith("SaveFile")) { if(l

48、ine.startsWith("SaveFile###")) { String[] s1 = line.split("###"); name = s1[1]; } if(line.startsWith("SaveFile#H#H##")) { String s2 = "/home/soft01/"+name; String [] s3 = line.split("#H#H##"); String s4 = s3[1]+"\r\n";

49、 OutputStream out = new FileOutputStream(s2,true); byte [] b = s4.getBytes(); out.write(b); out.close(); } } else if(line.startsWith("wo##yaojian##kongImage")) { Robot r = new Robot();//用于自動化測試的程序類 //表示矩形 長1024,寬768 Rectan

50、gle ret = new Rectangle(1024,768); ///createScreenCapture抓 取自己的桌面圖片,大小為rec指定的矩形的大小 BufferedImage img = r.createScreenCapture(ret); //將截圖寫入到某一文件中 File fa = new File("/home/soft01/"+i+".gif"); i++; ImageIO.write(img, "gif", fa); String filename = fa.getAbsol

51、utePath(); System.out.println(line); String [] str = line.split("Image/"); String IP = str[1]; new FaSongImage(filename,IP); } else if(line.startsWith("Teacher##liaojie##StudenT")) { String[] s1 = line.split("##StudenT"); final String s2 =

52、 s1[1]; final JFrame jf = new JFrame("老師發(fā)來消息"); JButton jb1 = new JButton("不懂"); JButton jb2 = new JButton("有點懂"); JButton jb3 = new JButton("完全懂了"); JLabel jl1 = new JLabel("信息:",JLabel.CENTER);jl1.setForeground(Color.red); JLabel jl2 = new JLabel("

53、老師問你剛才講的你懂了沒?(點擊按鈕)",JLabel.CENTER);jl2.setForeground(Color.blue); JPanel jp1 = new JPanel(); jf.setLayout(new GridLayout(3,1,0,10)); jf.add(jl1);jf.add(jl2); jp1.setLayout(new FlowLayout(FlowLayout.CENTER,10,1)); jp1.add(jb1);jp1.add(jb2);jp1.add(jb3); j

54、f.add(jp1); jf.setSize(300,300); jf.setResizable(false); jf.setLocation(400, 300); jf.setDefaultCloseOperation(jf.HIDE_ON_CLOSE); jf.setVisible(true); jb1.addActionListener(new ActionListener() { @Override public void actionPerfor

55、med(ActionEvent e) { String str1 = "Student##laiojie##"+s2+"###"+"budong"; new ClientTest(str1,so); jf.setVisible(false); } }); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {

56、 String str1 = "Student##laiojie##"+s2+"###"+"youdiandong"; new ClientTest(str1,so); jf.setVisible(false); } }); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str1 = "Stude

57、nt##laiojie##"+s2+"###"+"wanquandong"; new ClientTest(str1,so); jf.setVisible(false); } }); } else { jt.append(line+"\r\n"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }

58、 } } } 類ClientTest package Client_System; import javax.swing.JTextArea; public class ClientTest extends Thread{ String str;Socket so; public ClientTest(String str,Socket so) { this.str=str;this.so=so; this.start(); } public void

59、run() { while(true) { try { OutputStream out = so.getOutputStream(); PrintStream ps = new PrintStream(out); Scanner sc = new Scanner(System.in); while(str!=null) { ps.println(str); ps.flush(); str=sc.nextLine()

60、; } ps.close();so.close(); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("還沒有鏈接服務(wù)器!"); } } } } 類BaoCunJiLu package Client_System; public class BaoCunJiLu extends Thread{ String text;

61、 String filename; public BaoCunJiLu(String text,String filename) { this.text=text; this.filename = filename; this.start(); } public void run() { try { BufferedWriter bw = new BufferedWriter(new FileWriter(filename)); bw.write(text); bw.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } 21 / 21

展開閱讀全文
溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號:ICP2024067431-1 川公網(wǎng)安備51140202000466號


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請立即通知裝配圖網(wǎng),我們立即給予刪除!