Java中的多线程聊天服务器

时间:2009-09-17 05:42:09

标签: java multithreading sockets

我正在尝试用Java实现一个可以支持多个客户端的服务器 - 客户端套接字程序,但是当我的客户端连接到我的服务器时,执行多线程的类总是会崩溃。

import java.io.*;
import java.net.*;
public class ClientWorker extends Thread{
    Socket cwsocket=null;

    public ClientWorker(Socket cwsocket){
        super("ClientWorker");
        cwsocket=cwsocket;
    }

    public void run(){
        try {
            PrintWriter out = new PrintWriter(cwsocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(cwsocket.getInputStream()));

            String serverinput, serveroutput="";

            out.println(serveroutput);

            while ((serverinput = in.readLine()) != null) {
                out.println(serveroutput);
                if (serveroutput.equals("Terminate"))
                    break;
            }
            out.close();
            in.close();
            cwsocket.close();

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

每当我创建一个PrintWriter对象时,都会抛出一个NullPointerException异常,我不确定它为什么会继续发生。以下是我的服务器和客户端类。我做错了什么?

import java.io.*;
import java.net.*;
public class Server {

    public static void main(String[]args)throws IOException{
        ServerSocket serversocket=null;
        final int PORT_NUM=4444;
        boolean flag=true;
        try{
            System.out.println("Listening for connection");
            serversocket=new ServerSocket(PORT_NUM);
        }catch(IOException e){
            System.out.println("Could not listen to port: "+PORT_NUM);
            System.exit(-1);    
        }
        while(flag){
            new ClientWorker(serversocket.accept()).start();
        }
        System.out.println("Terminating server...");
        serversocket.close();
    }
}

import java.io.*;
import java.net.*;
public class Client {

    public static void main(String[] args){
        Socket socket=null;
        PrintWriter out=null;
        BufferedReader in=null;
        BufferedReader userInputStream=null;
        String IP="127.0.0.1";
        try{
            socket = new Socket(IP, 4444);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.out.println("Unknown host:" + IP);
            System.exit(1);
        } catch  (IOException e) {
            System.out.println("Cannot connect to server...");
            System.exit(1);
        }
        String userInput, fromServer;
        try{        
            userInputStream = new BufferedReader(new InputStreamReader(System.in));
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Terminate"))
                    break;

                userInput = userInputStream.readLine();
                if (userInput != null) {
                        System.out.println("> " + userInput);
                        out.println(userInput);
                }
            }
        }catch(IOException e){
            System.out.println("Bad I/O");
            System.exit(1);
        }
        try{
            out.close();
            in.close();
            userInputStream.close();
            socket.close();
            System.out.println("Terminating client...");
        }catch(IOException e){
            System.out.println("Bad I/O");
            System.exit(1);
        }catch(Exception e){
            System.out.println("Bad I/O");
            System.exit(1);
        }
    }
}

1 个答案:

答案 0 :(得分:9)

    public ClientWorker(Socket cwsocket){
        super("ClientWorker");
        cwsocket=cwsocket;
    }

你需要做

        this.cwsocket=cwsocket;

或重命名参数,使其不会遮挡同名的成员。