简单问题

Svny 2008-07-08 07:36:51
我有这样的聊天室代码,请问怎么执行它
我装了eclipse
不知道怎么执行这段代码,能详细地告诉我吗??
请不要告诉我去看书 我只是想看一下结果而已
//客户端的
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatClient {
public static void main(String[] args) {
ChatClient cc=new ChatClient();
cc.receive();
}
JTextField jtf;
JTextArea jta;
Socket s;
PrintWriter out;
BufferedReader in;
public ChatClient(){
JFrame frame=new JFrame("ChatClient");
frame.setSize(400,300);
jta=new JTextArea();
jta.setEditable(false);
jtf=new JTextField();
jtf.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
send();
}

});
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(jtf,"South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

try {
s=new Socket("127.0.0.1",9000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void receive(){
while(true){
try {
String text=in.readLine();
this.jta.append(text+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
public void send(){
String text=this.jtf.getText();
this.jtf.setText("");
out.println(text);
out.flush();
}

}


//服务器端的
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(9000);
List list=new ArrayList();
while(true){
Socket s=ss.accept();
list.add(s);
Thread t=new ServerThread(s,list);
t.start();
}
}

}
class ServerThread extends Thread{
Socket s;
List list;
BufferedReader in;
PrintWriter out;
public ServerThread(Socket s, List list) {
this.s = s;
this.list = list;
try {
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
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) return;
Iterator it=list.iterator();
while(it.hasNext()){
Socket socket=(Socket)(it.next());
PrintWriter o=new PrintWriter(socket.getOutputStream());
o.println(str);
o.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return;
}
}
}
}
...全文
94 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pastom 的回复:]
分别执行就好了撒,先服务器代码,在客户端代码
[/Quote]
againlove 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jishu_vip 的回复:]
javac
java
就可以了啊
[/Quote]
tanlijun37 2008-07-09
  • 打赏
  • 举报
回复
新建java工程,把代码copy进行,运行就可以了吧。
jishu_vip 2008-07-09
  • 打赏
  • 举报
回复
javac
java
就可以了啊
pastom 2008-07-08
  • 打赏
  • 举报
回复
分别执行就好了撒,先服务器代码,在客户端代码
craky 2008-07-08
  • 打赏
  • 举报
回复
这也太简单了吧,看书去!!!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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