我想通過從客戶端向服務器發送密鑰和隨機數來驗證用戶身份。我能做些什麼來避免NullPointerException?
我的代碼沒有顯示客戶端的響應。當我執行我的代碼時,我得到一個空指針異常。
import java.io.*;
import java.net.*;
import java.lang.*;
class Client
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the key value");
int key=Integer.parseInt(br.readLine());
double random=Math.random()*50;
System.out.println(random);
int response=key%((int)random);
System.out.println(key);
System.out.println("Authentication begins");
Socket echoSocket = new Socket("localhost", 2000);
FileOutputStream fout=null;
DataOutputStream clientout=null;
clientout.writeDouble(random);
clientout.writeInt(key);
clientout.writeInt(response);
fout.flush();
System.out.println("client is"+response);
echoSocket.close();
}
}
import java.io.*;
import java.net.*;
import java.lang.*;
class Server
{
public static void main(String args[]) throws IOException
{
int response2;
double random2;
int key2;
FileInputStream fin=null;
DataInputStream clientin=null;
ServerSocket s= new ServerSocket(2000);
Socket echoSocket=s.accept();
random2=clientin.readDouble();
key2=clientin.readInt();
response2=clientin.readInt();
response2=key2%((int)random2);
System.out.println("server is"+response2);
s.close();
echoSocket.close();
}
}
請妥善縮進代碼。這是不可能的。 –
@JB Nizet:ohh對不起,我會做 –
@Parth:「正確縮進」並不意味着所有的東西都有相同的縮進。您的編輯器/ IDE應該有一個縮進代碼的功能,在複製之前使用它。 (我現在爲你做了。) –