首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

java聊天程序

2012-04-25 
java聊天程序求助客户端:import java.awt.*import java.awt.event.*import java.io.*import java.net.*

java聊天程序求助
客户端:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class client1 extends JFrame {
private JButton btn_send;
private JLabel label_notice;
private JTextField jtf_send;
private JTextArea jta_message;
private JPanel panel2;
private JScrollPane jsp;
private Socket socket;
DataInputStream in;
  DataOutputStream out;
private JButton btn_connect;
private JPanel panel;
//private Thread thread;
Message m;
public client1()
{
/////////////////////////关于界面的开始//////////////////////////////////
btn_connect=new JButton("连接服务器");
btn_send=new JButton("发送");
m=new Message();
label_notice=new JLabel("请输入发送信息");
jtf_send=new JTextField(35);
panel=new JPanel();
panel.add(label_notice);
panel.add(jtf_send);
panel.add(btn_send);
add(panel,BorderLayout.NORTH);
jta_message=new JTextArea();
jsp=new JScrollPane(jta_message);//////JTextArea时需要用这个容器//////为什么
add(jsp,BorderLayout.CENTER);
add(btn_connect,BorderLayout.SOUTH);
jta_message.setEnabled(true);
jta_message.setEditable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setTitle("用户");
  setLocation(200,200);
  setSize(630,400);
  setVisible(true);
   
   
  btn_connect.addActionListener(new ActionListener ()/////////监听连接////////
  {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try
{
socket =new Socket("127.0.0.1",6666);
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
btn_connect.setVisible(false);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("连接失败……,请重新连接……");
}
}
 
  });
 
 
btn_send.addActionListener(new ActionListener()//////////////监听发送键/////////
  {
  public void actionPerformed(ActionEvent e) 
  {
  try
  {
  System.out.println("监听发送键成功");
  String sendmessage=jtf_send.getText();
  jtf_send.setText("");
  out.writeUTF(sendmessage);
  System.out.println(sendmessage);
  //out.flush();
  }
  catch(Exception e1)
  {
  e1.printStackTrace();
  }

}
  }
  );
new Thread(m).start();
}




class Message implements Runnable///////////////////////////内部类的好处//////////////////
{

public void run()
{
System.out.println("进入client的run函数,输出用户写的内容");
String str;
try
{
while(true)
{
System.out.println("准备接受数据");
str=in.readUTF();
if(str!=null)
{
System.out.println("已经收到数据");
System.out.println(str);
jta_message.append(str+'\n');
}

}
}
catch(Exception e)
{
e.printStackTrace();
}

}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new client1();
}



}
/////////////////////////////////////////////////////////////////////////


服务端:

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class server 
{
List<Message> clients = new ArrayList<Message>();
ServerSocket ss;

public void begin()
{
try
{
ss=new ServerSocket(6666);
while(true)
{
Socket c=ss.accept();
Message m=new Message(c);
clients.add(m);
new Thread(m).start();
System.out.println("用户连接成功");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new server().begin();

}
class Message implements Runnable
{
Socket s;
DataInputStream in;
DataOutputStream out;
public Message(Socket s)
{
this.s=s;
try
{
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void send(String str)
{
try
{
out.writeUTF(str);
//out.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
while(true)
{
try
{
System.out.println("服务端准备接受客户端的信息");
String str=in.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++)
{
Message m=clients.get(i);
m.send(str);
System.out.println("已经返回客户端");
}
}
catch(Exception e)
{
e.printStackTrace();
}

}
}
}

}


[解决办法]

Java code
btn_connect.addActionListener(new ActionListener()// ///////监听连接////////                {                    public void actionPerformed(ActionEvent arg0){                        // TODO Auto-generated method stub                        try {                            socket = new Socket("127.0.0.1", 6666);                            out = new DataOutputStream(socket.getOutputStream());                            in = new DataInputStream(socket.getInputStream());                            btn_connect.setVisible(false);                            new Thread(m).start();//放这里                        } catch (Exception e) {                            e.printStackTrace();                            System.out.println("连接失败……,请重新连接……");                        }                    }                }); 

热点排行