亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

大家有簡單的聊天室的代碼我參考一下嗎

那個,我想做一個簡單的通信,我用了多線程,但是我輸入一句話然后點擊回車,然后服務器沒有輸出這句話,直到我輸入結束標志over然后才打印,然后是把全部的打印出來,請問怎么解決

正在回答

4 回答

//服務器端
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();
	}

}


0 回復 有任何疑惑可以回復我~
#1

進擊的猿 提問者

非常感謝!你的代碼很清晰,我看懂了
2017-05-07 回復 有任何疑惑可以回復我~

非常感謝啊

0 回復 有任何疑惑可以回復我~
服務器端:
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)?{}
????????}
????}
}


歡迎交流,謝謝。


0 回復 有任何疑惑可以回復我~

run方法里不是死循環嗎 我怎么有點不懂·

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

大家有簡單的聊天室的代碼我參考一下嗎

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號