java版的QQ小程序
转载自:http://blog.csdn.net/lihongxun945/article/details/6114290
这是一个简单的java版的QQ小程序。
包括一个简单的服务器和一个简单的客户端。
运行时,先运行服务器,然后在运行客户端,就可以进行聊天了。
默认的配置是localhost,端口4545,更改ip就可以在两天电脑上进行聊天了。
目前不支持内网和外网之间的访问,也不支持多人聊天。
因为这只是一个简单的例子,感兴趣的同学可以通过改进,实现多人聊天和内外网之间的访问。
效果图:
下载地址:http://download.csdn.net/source/2958843
源代码:
QQServer.java
- //axun @copy right
- package axun.com;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class QQServer{
- private JFrame f=new JFrame("QQ服务器端");
- private JPanel pleft=new JPanel(new BorderLayout());
- private JPanel pright=new JPanel();
- private List list=new List();
- private TextArea t1=new TextArea();
- private TextArea t2=new TextArea();
- private Button b=new Button("发送");
- //一下是 网络通信用的变量
- DataOutputStream dos=null;
- BufferedReader br=null;
- DataInputStream dis=null;
- public QQServer(){
- f.setSize(400,300);
- f.setLayout(new BorderLayout());
- f.add(pleft,BorderLayout.WEST);
- f.add(pright,BorderLayout.CENTER);
- pleft.add(list);
- pright.setLayout(new GridLayout(3,1));
- pright.add(t1);
- pright.add(t2);
- pright.add(b);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- b.addActionListener(new bListener());
- }
- public void Addt1(String s){
- t1.append(s);
- }
- public void addList(String s){
- list.addItem(s);
- }
- public static void main(String[] args) throws Exception{
- QQServer server=new QQServer();
- InputStream in=null;
- OutputStream out=null;
- String string=null;
- ServerSocket ss=new ServerSocket(4545);
- Socket s=null;
- s=ss.accept();
- server.addList(s.toString());
- in=s.getInputStream();
- out=s.getOutputStream();
- server.dis=new DataInputStream(in);
- server.dos=new DataOutputStream(out);
- Listen1 l=new Listen1(server,server.dis);
- Thread t=new Thread(l);
- t.start();
- }
- class bListener implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- try{
- dos.writeUTF(t2.getText());
- Addt1("发送:"+"/n");
- Addt1(" "+t2.getText()+"/n");
- t2.setText("");
- }catch(Exception ep){
- Addt1("消息发送失败!/n");
- }
- }
- }
- }
- class Listen1 implements Runnable{
- private QQServer server=null;
- private DataInputStream dis=null;
- private String s=null;
- Listen1(QQServer server,DataInputStream dis){
- this.server=server;
- this.dis=dis;
- }
- public void run() {
- // TODO Auto-generated method stub
- try{
- while(true){
- s=dis.readUTF();
- server.Addt1("收到:"+"/n");
- server.Addt1(" "+s+"/n");
- }
- }catch(Exception e){
- server.Addt1("Error!:"+s+"/n");
- }
- }
- }
QQClient.java
- //axun @copy right
- package axun.com;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class QQClient {
- private JFrame f=new JFrame("QQ客户端");
- private TextArea t1=new TextArea();
- private TextArea t2=new TextArea();
- private Button b=new Button("发送");
- //一下是 网络通信用的变量
- DataOutputStream dos=null;
- BufferedReader br=null;
- DataInputStream dis=null;
- public void Addt1(String s){
- t1.append(s);
- }
- public QQClient(){
- f.setSize(400,300);
- f.setLayout(new GridLayout(3,1));
- t1.setEditable(false); //不可编辑
- f.add(t1);
- f.add(t2);
- f.add(b);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- b.addActionListener(new bListener());
- }
- public static void main(String[] args) throws Exception {
- QQClient client=new QQClient();
- InputStream in=null;
- OutputStream out=null;
- String string=null;
- Socket s=new Socket("localhost",4545);
- out=s.getOutputStream();
- in=s.getInputStream();
- client.dis=new DataInputStream(in);
- client.dos=new DataOutputStream(out);
- Listen2 l=new Listen2(client,client.dis);
- Thread t=new Thread(l);
- t.start();
- }
- class bListener implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- try{
- dos.writeUTF(t2.getText());
- Addt1("发送:"+"/n");
- Addt1(" "+t2.getText()+"/n");
- t2.setText("");
- }catch(Exception ep){
- }
- }
- }
- }
- class Listen2 implements Runnable{
- private QQClient client=null;
- private DataInputStream dis=null;
- private String s=null;
- Listen2(QQClient client,DataInputStream dis){
- this.client=client;
- this.dis=dis;
- }
- public void run() {
- // TODO Auto-generated method stub
- try{
- while(true){
- s=dis.readUTF();
- client.Addt1("收到:"+"/n");
- client.Addt1(" "+s+"/n");
- }
- }catch(Exception e){
- }
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。