Java 实训简单聊天室制作

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。


}
• });
•}
• public void actionPerformed(ActionEvent e){

if(e.getSource()==button){

if(!field.getText().equals("")){

try{

out.writeUTF(field.getText());

}catch(IOException e){}

}



public void send(String str){

try {

out.writeUTF(str);

} catch (IOException e) {
• try{

serverSock=new ServerSocket(6000);
• }catch(IOException e){

JOptionPane.showMessageDialog(null,"服务器启动失败!");

return;
•}
• try{

sock=serverSock.accept();

}catch(Exception ee){}
•}
•}
• 客户端代码: • import java.net.*; • import java.io.*; • import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import java.util.*; • public class Client{ • public static void main(String args[]) { • frame mainFrame = new frame(); •} •} • class frame extends JFrame implements Runnable,ActionListener{ • Socket sock; • JTextArea area=new JTextArea(); • • JTextField field=new JTextField(20); • JButton button=new JButton("发送"); • JButton button1=new JButton("连接服务器"); • DataOutputStream out;


addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
• try{

out.writeUTF("bye");

}catch(Exception ee){}
• dispose();
• System.exit(0);

DataOutputStream out=null;

String str=null;

clients(Socket you){

this.you=you;

try{

in=new DataInputStream(you.getInputStream());

out=new DataOutputStream(you.getOutputStream());

th.start();

}catch(IOException ee){

JOptionPane.showMessageDialog(null,"连接服务器失败!");

return;

}


}
•}
• public void run(){

String msg=null;

while(true){


field.setText(null);

}catch(Exception ee){}

}
•}
• public void run(){

String msg=null;

while(true){

try {

msg=in.readUTF();

if(msg.equals("bye")){

area.append("客户已经离开\n");

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
• try{

out.writeUTF("bye");

}catch(Exception ee){}
• dispose();
• System.exit(0);

}

• try{

out.close();

in.close();
• sock.close();

}catch(Exception ee){}
•}
•}
程序运行结果
(2)客户端与客户端对话
• 源程序: • 服务器端:

public static void main(String[] args){

p1.add(field);

p1.aΒιβλιοθήκη Baidud(button);

JPanel p2=new JPanel();

p2.add(button1);
• getContentPane().add(p2,BorderLayout.NORTH);
• getContentPane().add(p1,BorderLayout.SOUTH);

area.append(new Date().toString() +"客户"+"\n"+field.getText()+"\n");

field.setText(null);

}catch(Exception ee){}

}

}

if(e.getSource()==button1){

try{

• Socket sock;

JTextArea area=new JTextArea();
• JTextField field=new JTextField(20); • JButton button=new JButton("发送"); • DataOutputStream out; • DataInputStream in; • String cname=null; • Thread th; •

Socket s = server.accept();

clients c = new clients(s);

System.out.println("a client connected!");
• •
个线程,并将线程加入集合中
new Thread(c).start(); ss.add(c); // 每当一个客户端连接后,分配一个Socket类,建立一
• DataInputStream in;

Thread th;
• frame() {

area.setEditable(false);
• getContentPane().add(new JScrollPane(area),BorderLayout.CENTER);

JPanel p1=new JPanel();
// InetAddress address=InetAddress.getLocalHost();

//String str=address.getHostAddress();

//sock=new Socket(str,6000);

sock=new Socket("127.0.0.1",6000);
try{

msg=in.readUTF();

if(msg.equals("bye")){//如果客户退出

area.append("服务器已经停止\n");

break;

}

area.append(new Date().toString() +"服务器"+"\n"+msg+"\n");

}catch(Exception ee){break;}

out=new DataOutputStream(sock.getOutputStream());

in=new DataInputStream(sock.getInputStream());

out.writeUTF("你连接服务器成功");


th.start();

}catch(Exception e){}

}

} catch (IOException e) {

e.printStackTrace();

}



}
• private class clients implements Runnable{

private Socket you=null;

DataInputStream in=null;
Java 实训简单聊天室制作
• 姓名:汪春俊 • 学号:201004070108 • 班级:10471
(1)服务器与客户端聊天
• 源程序: • 服务器端:import java.net.*; • import java.io.*; • import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import java.util.*; • public class Server{ • public static void main(String args[]){ • frame mainFrame = new frame(); •} •} • class frame extends JFrame implements Runnable,ActionListener{ • ServerSocket serverSock;

OutputStream os=sock.getOutputStream();

• out=new DataOutputStream(os);

InputStream is=sock.getInputStream();

in=new DataInputStream(is);

button1.setEnabled(false);

th=new Thread(this);

button.addActionListener(this);

button1.addActionListener(this);

setTitle("客户端");

setSize(340,200);

setLocation(100,50);

setVisible(true);

break;

}

}catch(Exception ee){break;}

}
• area.append(new Date().toString() +"客户"+"\n"+msg+"\n");
• try{

out.close();

in.close();

sock.close();

serverSock.close();

}
• });
•}
• public void actionPerformed(ActionEvent eee){

if(!field.getText().equals("")){

try{

out.writeUTF(field.getText());

area.append(new Date().toString() +"服务器"+"\n"+field.getText()+"\n");

new Server().start();

}

• public void start() {

try {

server = new ServerSocket(4331);


} catch (IOException e) {

e.printStackTrace();

}

try {

while (true) {
• frame(){ • area.setEditable(false); • getContentPane().add(new JScrollPane(area),BorderLayout.CENTER);
• JPanel p1=new JPanel(); • p1.add(field); • p1.add(button); • getContentPane().add(p1,BorderLayout.SOUTH); • button.addActionListener(this); • th=new Thread(this); • setTitle("服务器"); • setSize(340,200); • setLocation(500,50); • setVisible(true • );
相关文档
最新文档