大家有简单的聊天室的代码我参考一下吗
那个,我想做一个简单的通信,我用了多线程,但是我输入一句话然后点击回车,然后服务器没有输出这句话,直到我输入结束标志over然后才打印,然后是把全部的打印出来,请问怎么解决
那个,我想做一个简单的通信,我用了多线程,但是我输入一句话然后点击回车,然后服务器没有输出这句话,直到我输入结束标志over然后才打印,然后是把全部的打印出来,请问怎么解决
2017-04-09
//服务器端 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) {} } } } 欢迎交流,谢谢。
举报