1

ソケットプログラミングアプリケーションの「サーバー」のこのコードがあり、このコードjava.net.SocketException行の次のコードの26行目にこの例外があります。

while((line=bReader.readLine())!=null){

この例外の原因は何ですか?

サーバ:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable{
    final public int PORT=1212;
    private Socket client;
    private ServerSocket server;
    private BufferedReader bReader;
    private PrintWriter pWriter;
    public void run(){
        try {
            System.out.println("Start server ...");
            server=new ServerSocket(PORT);
            while(true){
                System.out.println("Start listening on PORT "+PORT+" ...");
                client=server.accept();
                System.out.println("Connection with client["+client.getInetAddress().getHostName()+"]");
                bReader=new BufferedReader(new InputStreamReader(client.getInputStream()));
                pWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
                    String line="";
                    while((line=bReader.readLine())!=null){
                        RecoginzeMessage(line);
                    }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                client.close();
            //  server.close();
                bReader.close();
                pWriter.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    public void RecoginzeMessage(String msg){
        String msg_params[]=msg.split(":");
        switch(Integer.parseInt(msg_params[0])){
        case 0:// Authetication process 
            System.out.println("Authentication new client");
            AuthenticateUser(msg_params[1],msg_params[2]);
            break;
        }
    }
    public void AuthenticateUser(String username, String password){
        if(username.equals("adham")&&password.equals("adham")){
            pWriter.println("1");
            pWriter.flush();
        }
        else{
            pWriter.println("0");
            pWriter.flush();
        }
    }
}

そして、「クライアント」のこのコード..

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client implements Runnable {
    public final String SERVER_IP="127.0.0.1";
    public final int PORT=1212;
    private Socket client;
    private BufferedReader bReader;
    private PrintWriter pWriter;
    Scanner sc=new Scanner(System.in);
    public Client(){
        if(client==null)
            try {
                client=new Socket(SERVER_IP,PORT);
                bReader=new BufferedReader(new InputStreamReader(client.getInputStream()));
                pWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);

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


    }
    public void run(){

            System.out.print("Enter username: ");
            String username=sc.nextLine();

            System.out.print("Enter password: ");
            String password=sc.nextLine();

            if(login(username,password)){
                System.out.print(">logged in successfuly");             
            }else{
                System.out.print(">Inavlied username or password");                             
            }

    }
    private boolean login(String username, String password){
        try{
            pWriter.println("0:"+username+":"+password);
            pWriter.flush();
            while(true){
                String line="";
                while((line=bReader.readLine())!=null){
                    if(line.equals("1"))
                        return true;
                    else
                        return false;
                }

            }
        }catch(Exception ex){
            return false;
        }
    }
}

編集:クライアントスレッドを実行するクラス

public class RunClient {    
    public static void main(String args[]){
        Client cThread=new Client();
        cThread.run();
    }
}

編集:サーバースレッドを実行するクラス

public class RunServer {
    public static void main(String args[]){
        Server sThread=new Server();
        sThread.run();
    }
}
4

2 に答える 2

1

クライアントはスレッドで実行されているようですが、単に実装するだけでは正しくありません。 を作成して渡すRunnable必要があります。クライアントは無期限に実行されないため、クライアントはソケットを終了して暗黙的に (JVM)閉じ、サーバーはこの例外を取得してそれを示します (クライアントがソケットを閉じます)。ThreadRunnable

于 2011-08-28T19:23:33.773 に答える
1

クライアントアプリのメソッドの最後のステートメントとして何かSocketExceptionを追加したり、類似したものを試したりしないようにします。wait()run()

于 2011-08-28T20:06:30.200 に答える