用java编写一个简单的可运行的聊天程序,带有简单的报告书

wjw520best 2009-12-10 05:45:47
用java编写,聊天程序,聊天工具都ok,简单写写报告书,本人对java不是很了解,希望各位高手帮帮忙,在下多谢各位高手了。
...全文
401 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zero20090101 2010-12-28
  • 打赏
  • 举报
回复
可以实现多用户聊天吗?
wjw520best 2009-12-11
  • 打赏
  • 举报
回复
不是毕业设计,是作业而已
BearKin 2009-12-10
  • 打赏
  • 举报
回复
毕设嘛 自己做吧。。
wjw520best 2009-12-10
  • 打赏
  • 举报
回复
各位高手,有谁能帮帮忙啊,急啊
wjw520best 2009-12-10
  • 打赏
  • 举报
回复
能写个简单的报告书吗 说说它具体具有哪些功能,帮帮忙
yang677888 2009-12-10
  • 打赏
  • 举报
回复
Client端


import java.net.*;
import java.io.*;
import java.util.*;

import javax.swing.*;
import java.awt.event.*;
public class QQClient {
String name;
BufferedReader in;
PrintWriter out;
JTextField jtf;
JTextArea jta;
JComboBox jcb;
public static void main(String[] args) {
if (args.length==0 || args[0].equals("All")
||args[0].indexOf(":")!=-1){
System.out.println("Error!");
System.exit(0);
}
Socket s=null;
try {
s = new Socket("127.0.0.1",7755);
QQClient client=new QQClient(s,args[0]);
client.prepare();
client.receive();
} catch (Exception e) {
e.printStackTrace();
}
}
public QQClient(Socket s,String name) throws Exception{
this.name=name;
this.in=new BufferedReader(new InputStreamReader(s.getInputStream()));
this.out=new PrintWriter(s.getOutputStream());
JFrame frame=new JFrame("Client "+name);
frame.setSize(400,300);
JPanel panel=new JPanel();
this.jcb=new JComboBox();
jcb.addItem("All");
this.jtf=new JTextField(25);
panel.add(jcb);
panel.add(jtf);
this.jta=new JTextArea();
this.jta.setEditable(false);
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(panel,"South");
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
end();
}
});

this.jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
send();
}
});
}
void prepare() throws Exception{
out.println("1:"+name+":");
out.flush();
}
void end(){
out.println("2:"+name+":");
out.flush();

System.exit(0);
}
void send(){
String receiver=(String)(this.jcb.getSelectedItem());
if (receiver==null) return;
String text=this.jtf.getText();
if ((text.indexOf(":"))!=-1){
this.jtf.setText("");
return;
}
out.println("3:"+receiver+":"+text);
out.flush();
this.jtf.setText("");
}
void receive(){
while(true){
try {
String str=in.readLine();
String[] info=parseString(str);
if (info[0].equals("1")){
this.jta.append(info[1]+" entered!"+"\n");
this.jcb.addItem(info[1]);
}
else if (info[0].equals("2")){
this.jta.append(info[1]+" exit!"+"\n");
this.jcb.removeItem(info[1]);
}
else{
this.jta.append(info[1]+" said:"+info[2]+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String[] parseString(String s){
String[] ss=new String[3];
int i=0;
StringTokenizer st=new StringTokenizer(s,":");
while(st.hasMoreTokens()){
ss[i]=st.nextToken();
i++;
}
return ss;
}

}




Server端

import java.net.*;
import java.io.*;
import java.util.*;
public class QQServer {
public static void main(String[] args) {
ServerSocket ss=null;
Map users=new HashMap();
try {
ss = new ServerSocket(7755);
} catch (IOException e) {
e.printStackTrace();
}
while(true){
try {
Socket s=ss.accept();
Thread t=new ServerThread(s,users);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

class ServerThread extends Thread{
Socket s;
String userName;
Map users;
BufferedReader in;
PrintWriter out;

public ServerThread(Socket s,Map m){
this.s=s;
this.users=m;
try {
this.in=new BufferedReader(new InputStreamReader(s.getInputStream()));
this.out=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void run(){
while(true){
try {
String str=in.readLine();
if (str==null) break;
String[] info=parseString(str);
if (info[0].equals("1")){
this.userName=info[1];
send(str,"All");
this.users.put(userName,out);
Iterator it=users.keySet().iterator();
while(it.hasNext()){
String user=(String)(it.next());
send("1:"+user+":",userName);
}
}
else if (info[0].equals("2")){
send(str,"All");
this.users.remove(info[1]);
break;
}
else{
String text="3:"+this.userName+":"+info[2];
send(text,info[1]);
}
} catch (Exception e) {
// e.printStackTrace();
return;
}
}

}
private String[] parseString(String s){
String[] ss=new String[3];
int i=0;
StringTokenizer st=new StringTokenizer(s,":");

while(st.hasMoreTokens()){
ss[i]=st.nextToken();
i++;
}
return ss;
}
private void send(String text,String receiver) throws Exception{
PrintWriter o;
if (receiver.equals("All")){
Iterator it=users.keySet().iterator();
while(it.hasNext()){
String s=(String)(it.next());
o=(PrintWriter)(users.get(s));
o.println(text);
o.flush();
}
}
else{
o=(PrintWriter)(users.get(receiver));
o.println(text);
o.flush();
}
}
}
SnowingXimen 2009-12-10
  • 打赏
  • 举报
回复
我的JICQ,仿QQ界面风格的,又设计文档
可以再网上搜到
Email:SnowingXimen@163.com

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧