課程
/后端開發
/Java
/Java Socket應用---通信是這樣練成的
那個,我想做一個簡單的通信,我用了多線程,但是我輸入一句話然后點擊回車,然后服務器沒有輸出這句話,直到我輸入結束標志over然后才打印,然后是把全部的打印出來,請問怎么解決
2017-04-09
源自:Java Socket應用---通信是這樣練成的 6-2
正在回答
//服務器端 package?Main; import?java.io.BufferedReader; import?java.io.IOException; import?java.io.InputStreamReader; import?java.io.PrintWriter; import?java.net.ServerSocket; import?java.net.Socket; import?java.util.Scanner; public?class?TCPServer?{ public?static?void?main(String[]?args)?throws?IOException?{ ServerSocket?server?=?new?ServerSocket(8899); Socket?socket?=?server.accept(); System.out.println("鏈接成功。"); Speak?speak?=?new?Speak(socket); speak.start(); Listen?listen?=?new?Listen(socket); listen.start(); } } class?Speak?extends?Thread?{ Socket?socket?=?null; PrintWriter?speak?=?null; String?Data?=?null; Scanner?hand?=?new?Scanner(System.in); public?Speak(Socket?socket)?throws?IOException?{ this.socket?=?socket; this.speak?=?new?PrintWriter(socket.getOutputStream(),?true); } public?void?run(){ while(true){ Data?=?hand.next(); speak.println(Data); } } } class?Listen?extends?Thread?{ Socket?socket?=?null; BufferedReader?reader?=?null; public?Listen(Socket?socket)?throws?IOException{ this.socket?=?socket; this.reader?=?new?BufferedReader(new?InputStreamReader(socket.getInputStream())); } public?void?run(){ while(true){ try?{ System.out.println(reader.readLine()); }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } } } //客戶端 package?Main; import?java.io.IOException; import?java.net.Socket; import?java.net.UnknownHostException; public?class?TCPClient?{ public?static?void?main(String[]?args)?throws?UnknownHostException,?IOException?{ //?TODO?Auto-generated?method?stub Socket?socket?=?new?Socket("127.0.0.1",?8899); System.out.println("鏈接成功。。"); Speak?speak?=?new?Speak(socket); speak.start(); Listen?listen?=?new?Listen(socket); listen.start(); } }
進擊的猿 提問者
非常感謝啊
服務器端: package?QiYanhui; import?java.net.*; import?java.io.*; import?java.util.*; /** ?*?@author?聊天軟件的服務器端程序 ?*/ public?class?TalkServer?{ ????public?static?ArrayList<Client>?allclient?=?new?ArrayList<Client>();?//存放所有通信線程 ????public?static?int?clientnum?=?0;??//統計客戶連接的計數變量 ???? ????public?static?void?main(String?args[]){ ????????try?{ ????????????ServerSocket?s?=?new?ServerSocket(5432); ????????????while(true){ ????????????????Socket?s1?=?s.accept();??//等待客戶連接 ????????????????DataOutputStream?dos?=?new?DataOutputStream(s1.getOutputStream()); ????????????????DataInputStream?din?=?new?DataInputStream(s1.getInputStream()); ????????????????Client?x?=?new?Client(clientnum,?dos,?din);??//創建與客戶對應的通信線程 ????????????????allclient.add(x);??//將線程加入ArrayList中 ????????????????x.start(); ????????????????clientnum++; ????????????} ????????}?catch?(IOException?e)?{?} ????} } //通信線程處理與對應的客戶通信,將來自客戶數據發往其他客戶 class?Client?extends?Thread?{ ????int?id;???//客戶的標識 ????DataOutputStream?dos;??//去往客戶的輸出流 ????DataInputStream?din;??//來自客戶的輸入流 ???? ????public?Client(int?id,?DataOutputStream?dos,?DataInputStream?din){ ????????this.id?=?id; ????????this.dos?=?dos; ????????this.din?=?din; ????} ???? ????public?void?run(){????//循環讀取客戶數據轉發給其他客戶 ????????while(true){ ????????????try?{ ????????????????String?message?=?"客戶"?+?id?+?":"?+?din.readUTF();???//讀客戶數據 ????????????????for(int?i=0;?i<TalkServer.clientnum;?i++){ ????????????????????//將消息轉發給其他客戶 ????????????????????if(i?!=?id){ ????????????????????????TalkServer.allclient.get(i).dos.writeUTF(message); ????????????????????}else{ ????????????????????????String?remessage?=?""; ????????????????????????remessage?=?message.replace("客戶"?+?id,?"自己"); ???????????????????????? ????????????????????????//使自己發送的信息顯示在屏幕的右邊 ????????????????????????int?aa?=?35?-?remessage.length(); ????????????????????????StringBuffer?space=?new?StringBuffer(); ????????????????????????//輸出指定的空格 ????????????????????????for(int?j=?0;j<(aa-1);j++) ????????????????????????{ ???????????????????????????space.append("?");//這里是空格 ????????????????????????} ????????????????????????TalkServer.allclient.get(id).dos.writeUTF((space.toString())?+?remessage); ????????????????????} ????????????????} ????????????}?catch?(IOException?e)?{} ????????} ????} } 客戶端: package?QiYanhui; import?java.net.*; import?java.io.*; import?java.awt.event.*; import?java.awt.*; public?class?TalkClient?{ ????public?static?void?main(String[]?args)?throws?IOException{ ????????Socket?s1?=?new?Socket("localhost",?5432);????//連接服務器 ????????DataInputStream?dis?=?new?DataInputStream(s1.getInputStream()); ????????final?DataOutputStream?dos?=?new?DataOutputStream(s1.getOutputStream()); ????????Frame?myframe?=?new?Frame("簡易聊天室"); ????????Panel?panelx?=?new?Panel(); ????????final?TextField?input?=?new?TextField(35); ????????TextArea?display?=?new?TextArea(19,?35); ????????panelx.add(display); ????????panelx.add(input); ????????myframe.add(panelx); ????????new?receiveThread(dis,?display);????//創建啟動接收消息的線程 ????????input.addActionListener(new?ActionListener()?{ ????????????public?void?actionPerformed(ActionEvent?e)?{ ????????????????try?{ ????????????????????dos.writeUTF(input.getText());????//發送數據 ????????????????????input.setText("");??//清空輸入框內容 ????????????????}?catch?(IOException?e2)?{????} ????????????} ????????}); ???????? ????????myframe.setSize(350,?400); ????????myframe.setVisible(true); ????} } //接收消息線程循環讀取網絡消息,顯示在文本域 class?receiveThread?extends?Thread{ ????DataInputStream?dis; ????TextArea?displayarea; ???? ????public?receiveThread(DataInputStream?dis,?TextArea?m){ ????????this.dis?=?dis; ????????displayarea?=?m; ????????this.start(); ????} ???? ????public?void?run(){ ????????for(;;){ ????????????try?{ ????????????????String?str?=?dis.readUTF();???//讀來自服務器的消息 ????????????????displayarea.append(str?+?"\n");???//將消息添加到文本域顯示 ????????????}?catch?(IOException?e)?{} ????????} ????} } 歡迎交流,謝謝。
run方法里不是死循環嗎 我怎么有點不懂·
舉報
分享的是 Java 中的網絡編程,使用Socket實現網絡聊天通信
1 回答udp創建聊天室
1 回答參考一下老師的demo
3 回答我想基于老師的代碼,實現聊天的功能,請問該怎么寫啊?
1 回答客戶與客戶聊天
2 回答有沒有大神會做簡單的web server,用java來實現的
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-04-11
2019-02-05
非常感謝啊
2017-06-11
2017-05-22
run方法里不是死循環嗎 我怎么有點不懂·