| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 556 人关注过本帖
标题:最近写聊天室程序遇到麻烦了`有人来帮一把么?
取消只看楼主 加入收藏
chump345
Rank: 1
来 自:家里蹲大学
等 级:新手上路
帖 子:108
专家分:5
注 册:2007-11-24
结帖率:93.33%
收藏
已结贴  问题点数:20 回复次数:0 
最近写聊天室程序遇到麻烦了`有人来帮一把么?
Server:

程序代码:
import     *;
import java.util.Scanner;
    

public class Server extends Thread{
    public static String[] vs = new String[10];
    public static int i =0;
     Socket socket;
    
    public  void run(){
        try{
            ServerSocket server = new ServerSocket(1600);
            while(true){
                System.out.println("服务器启动中......");
                socket = server.accept();
                ClientLine client = new ClientLine(socket);
                client.start();
                }
            }catch(IOException e){
                System.out.println("程序关闭....");
                System.exit(0);
        }
    }
    
    public static synchronized void message(String msg){
        vs[i] = msg;
        i = (i+1)%10;
        vs[i] = "*";    
    }
    
    public static void main(String args[]){
        new Server().start();    
    }
    
}


class ClientLine extends Thread {
    
    Socket client;
    String msg;
    BufferedReader in;
    PrintWriter out;
    ClientLine(Socket socket){
        this.client = socket;
    }
    public void run(){
        try{
            
            in = new BufferedReader(new InputStreamReader(
                                              client.getInputStream()));
            out = new PrintWriter(client.getOutputStream());
            
            SendToAll send = new SendToAll();
            send.start();
            msg = in.readLine();
            while(!msg.equals("exit")){
                Server.message(msg);
                msg = in.readLine();
            }
            if(msg.equals("exit")){
                in.close();
                out.close();
                client.close();            
            }
            
            }catch(IOException e){
                System.out.println("出现异常!");        
        }
    }
    public class SendToAll extends Thread{
        private int j = 0;
        public void run(){
            while(true){
        
                try{
                sleep(500);
                }catch(InterruptedException e){
                    System.out.println("同步错误....");
                    
                }
                while(true){
                    if(!Server.vs[j].equals("*")){
                        out.println(Server.vs[j]);
                        out.flush();
                        j = (j+1)%10;
                    }
                
                }
            }
            
        }
    }
}
Client:
程序代码:
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import *;
import *;

class Client extends JFrame{
    JLabel l1;
    JTextField text1;
    JTextArea ta,tt;
    JButton b1,b2;
    JPanel p1,p2;
    Socket socket = null;
    BufferedReader in;
    PrintWriter out;
    String b,c;
    
    Client(){
        //设定窗口,并建立两个按钮事件`
            setSize(400,400);
            setLocation(300,300);
            setVisible(true);
            Container cp = getContentPane();
            cp.setLayout(new BorderLayout());
            l1 = new JLabel("请输入你的昵称:");    
            text1 = new JTextField(10);
            b1 = new JButton("建立连接");
            b1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){    //按钮事件` 
                    l1.setEnabled(false);
                    text1.setEditable(false);
                    b1.setEnabled(false);
                    repaint();
                    count();                            //调用连接方法` 开始连接`
                    
                }
            });
            b2 = new JButton("发送");
            b2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    b = ta.getText();
                    out.print(b);                    
                    ta.setText("");    
                }
            });
            p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
            p2 = new JPanel(new BorderLayout());
            ta = new JTextArea(5,1);
            tt = new JTextArea();
            tt.setEditable(false);
            cp.add(p2,BorderLayout.SOUTH);
            cp.add(p1,BorderLayout.NORTH);
            JScrollPane scroll1 = new JScrollPane(tt);
            cp.add(scroll1,BorderLayout.CENTER);
            p1.add(l1);
            p1.add(text1);
            p1.add(b1);    
            JScrollPane scroll = new JScrollPane(ta);
            p2.add(scroll,BorderLayout.CENTER);
            p2.add(b2,BorderLayout.EAST);
            repaint();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        
    }
    public void count(){                                //定义建立连接的方法
        try{
        socket = new Socket("127.0.0.1",1600);
        }catch(IOException a){System.out.println("找不到服务器");}

        try{
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream());
        new ClientThread().start();
        }catch(IOException Ee){ System.out.println("连接异常");} 
    }
                
    class ClientThread extends Thread{
        public void run(){
            String msg;
                try{
                    msg = in.readLine();
                    while(true){
                        tt.append(msg+"\n");
                        msg = in.readLine();
                    }
                }catch(IOException e){
                    System.out.println("连接异常!");}
        }
    }
    public static void main(String args[]){
        new Client();
    }
}

这是仿照网上的一个聊天室代码写的`
但为什么连接后主程序会出现空指针异常呢`
大体的部分都和那个程序差不多啊`
原程序见附件`
原程序.rar (3.31 KB)
搜索更多相关主题的帖子: 麻烦 聊天室 
2009-11-05 21:50
快速回复:最近写聊天室程序遇到麻烦了`有人来帮一把么?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022448 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved